//
//  ShareInstance.m
//  ALand
//
//  Created by Z on 16/4/28.
//  Copyright © 2016年 Z. All rights reserved.
//

#import "ShareInstance.h"
#import "CoverShadowView.h"
#import "ShareContentView.h"
#import "CommotityListBrand.h"
#define kWindow [UIApplication sharedApplication].keyWindow
@interface ShareInstance ()
@property (nonatomic, strong) CoverShadowView *cover;
@property (nonatomic, strong) ShareContentView *shareContentView;
@end

@implementation ShareInstance
+ (ShareInstance *)shareInstace
{
    static ShareInstance *instance;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        instance = [[ShareInstance alloc] init];
        
    });
    return instance;
}

- (void)showWithTitle:(NSString *)title content:(NSString *)content url:(NSString *)url image:(UIImage *)image
{
    
    [self.cover show];
    
    WS(weakSelf)
    self.cover.tapAction = ^{
        [weakSelf hide];
    };
    self.shareContentView.clickBlock = ^{
        [weakSelf hide];
    };
    
    self.shareContentView.title = title;
    self.shareContentView.content = content;
    self.shareContentView.url = url;
    self.shareContentView.image = image;
    [MyTools animateFromBottomDuration:0.5 view:self.shareContentView viewHeight:140];

}

- (void)hide
{
    [self.cover hide];
    [UIView animateWithDuration:0.3 animations:^{
        self.shareContentView.frame = CGRectMake(0, kHeight, kWidth, 140);
    }];

}

- (CoverShadowView *)cover
{
    if (!_cover) {
        _cover = [[CoverShadowView alloc] initOnTopWithHeight:kHeight];
        [kWindow addSubview:_cover];
    }
    return _cover;
}

- (ShareContentView *)shareContentView
{
    if (!_shareContentView) {
        _shareContentView = [ShareContentView viewWithNibName:@"ShareContentView"];
        _shareContentView.frame = CGRectMake(0, kHeight, kWidth, 140);
        [kWindow addSubview:_shareContentView];

    }
    
    return _shareContentView;
}


/**
 *  分享店铺
 *
 *  @param title           标题
 *  @param shopDescription 描述
 *  @param url             链接
 *  @param image           图片
 *  @param imageUrl        图片链接（现在暂时没有用处，可用于判断用户是否上传头像）
 */
- (void)shareShopWithTitle:(NSString *)title shopDescription:(NSString *)shopDescription url:(NSString *)url image:(UIImage *)image imageUrl:(NSString *)imageUrl
{
    if (!title || title.length == 0) {
        [MBProgressHUD Javen_showError:@"请填写店铺名！" onView:kWindow delay:0.7 complete:nil];
    }else if(!shopDescription || shopDescription.length == 0){
        [MBProgressHUD Javen_showError:@"请填写店铺简介！" onView:kWindow delay:0.7 complete:nil];
    }
//    else if (!imageUrl || imageUrl.length < 5) {
//        [MBProgressHUD Javen_showError:@"请上传店铺头像！" onView:kWindow delay:0.7 complete:nil];
//    }
    else{
        [self showWithTitle:title content:shopDescription url:url image:image];
    }
}

/** 邀请合伙人 */
- (void)invitePartner
{
    
    NSString *urlStr = [NSString stringWithFormat:@"%@ShopUser/gpregister/invitationCode/%@.html", [UserInfo shareInstance].appConfig.webShopBaseUrl, [UserInfo shareInstance].reseller.invitationCode];
    [self showWithTitle:@"欢迎加入新营养部落，一起共创健康未来。" content:@"在新营养部落开店，享受自由健康生活，免费游览欧洲北美。" url:urlStr image:[UIImage appIcon]];
}

/** 分享商品 */
- (void)shareGoodsWithGoodsModel:(CommotityListModel *)model image:(UIImage *)image
{
    [MBProgressHUD showHUDAddedTo:kWindow animated:YES];
    
    [[HTTPCilent shareCilent] GET:[NSString stringWithFormat:@"goods/brand/get/%@", model.brand.uuid] parameters:nil success:^(NSURLSessionDataTask *task, id responseObject) {
        
        [MBProgressHUD hideHUDForView:kWindow];
        if (kIsResponseSuccess(responseObject)) {
            
            NSString *description = responseObject[@"goodsBrand"][@"description"];
            NSString *url = [NSString stringWithFormat:@"%@Wap/detail/shop_id/%@/id/%@/hastbar/0/.html", [UserInfo shareInstance].appConfig.webShopBaseUrl, [UserInfo shareInstance].reseller.shop.uuid, model.uuid];
            
            if (description && description.length > 0 && model.name.length > 0)
            {
                [self showWithTitle:model.name content:description url:url image:image];
            }else
            {
                [MBProgressHUD Javen_showError:@"该商品暂不支持分享" onView:kWindow delay:0.5 complete:nil];
            }
 
        }
    } failure:^(NSURLSessionDataTask *task, NSError *error) {
        
    }];
    
}

@end
