//
//  WMShareObject.m
//  Gengmei
//
//  Created by Sean Lee on 7/22/15.
//  Copyright (c) 2015 Wanmeichuangyi. All rights reserved.
//

#import "WMShareObject.h"
#import "NSMutableDictionary+GMShareSDK.h"
@import GMFoundation;
@import SDWebImage;

@implementation WMShareBasicObject

@end


@implementation GMWechatminiObject

// 获取到图片地址后，进行优先图片下载
- (void)setHdImageUrl:(NSString *)hdImageUrl {
    _hdImageUrl = hdImageUrl;
    self.thumbImage = [UIImage imageNamed:@"icon"];
    if (hdImageUrl.length) {
        __weak typeof(self) weakSelf = self;
        [[SDWebImageDownloader sharedDownloader] downloadImageWithURL:[NSURL URLWithString:hdImageUrl] completed:^(UIImage * _Nullable image, NSData * _Nullable data, NSError * _Nullable error, BOOL finished) {
            if (image) {
                weakSelf.hdImage = image;
            }
        }];
    }
}
- (void)setThumbImageUrl:(NSString *)thumbImageUrl {
    _thumbImageUrl = thumbImageUrl;
    self.thumbImage = [UIImage imageNamed:@"share_smallProgram"];
    if (thumbImageUrl.length) {
        __weak typeof(self) weakSelf = self;
        [[SDWebImageDownloader sharedDownloader] downloadImageWithURL:[NSURL URLWithString:thumbImageUrl] completed:^(UIImage * _Nullable image, NSData * _Nullable data, NSError * _Nullable error, BOOL finished) {
            if (image) {
                weakSelf.thumbImage = image;
            }
        }];
    }
}

@end

@implementation GMCoverObject
@end

@implementation GMOthersObject
- (void)setAfterCoverUrl:(NSString *)afterCoverUrl {
    _afterCoverUrl = afterCoverUrl;
    if (!afterCoverUrl.isNonEmpty) {
        return;
    }
    __weak typeof(self) weakSelf = self;
    [[SDWebImageDownloader sharedDownloader] downloadImageWithURL:[NSURL URLWithString:afterCoverUrl] completed:^(UIImage * _Nullable image, NSData * _Nullable data, NSError * _Nullable error, BOOL finished) {
        if (image) {
            weakSelf.afterCoverImage = image;
        }
    }];
}

- (void)setBeforeCoverUrl:(NSString *)beforeCoverUrl {
    _beforeCoverUrl = beforeCoverUrl;
    if (!beforeCoverUrl.isNonEmpty) {
        return;
    }
    __weak typeof(self) weakSelf = self;
    [[SDWebImageDownloader sharedDownloader] downloadImageWithURL:[NSURL URLWithString:beforeCoverUrl] completed:^(UIImage * _Nullable image, NSData * _Nullable data, NSError * _Nullable error, BOOL finished) {
        if (image) {
            weakSelf.beforeCoverImage = image;
        }
    }];
}

@end
@implementation WMShareObject

- (void)setImage:(NSString *)image {
    
    _image = image;
    if (image.length) {
        __weak typeof(self) weakSelf = self;
        [[SDWebImageDownloader sharedDownloader] downloadImageWithURL:[NSURL URLWithString:image] completed:^(UIImage * _Nullable image, NSData * _Nullable data, NSError * _Nullable error, BOOL finished) {
            if (image) {
                weakSelf.shareImage = image;
            }
        }];
    }
}

- (void)setPlatform:(GMSharePlatform)platform {
    _platform = platform;
    switch (platform) {
        case GMSharePlatformWechatSessionSnapShot:
        case GMSharePlatformWechatTimelineSnapShot:
        {
            self.shareImage = self.wechatScreenshot.content;
            self.contentType = GMShareContentTypeImage;
        }
            break;
        default:
        {
            self.contentType = GMShareContentTypeAuto;
        }
            break;
    }
}

/// 获取分享通用数据
/// @param shareType 分享平台
- (NSMutableDictionary *)getShareParams:(GMSharePlatform)shareType {
    
    self.platform = shareType;
    WMShareBasicObject *object = nil;
    UIImage *shareImage = self.shareImage?:[UIImage imageNamed:@"icon"];
    // 微信会话分享且微信小程序内容不为空的时候，认为是微信小程序分享
    if (shareType == GMSharePlatformWechatSession && self.wechatmini.path.length) {
        shareType = GMSharePlatformWechatMiniProgram;
    }
    
    switch (shareType) {
        case GMSharePlatformWechatSession:
            object = self.wechat;
            break;
        case GMSharePlatformWechatTimeline:
            object = self.wechatline;
            break;
        case GMSharePlatformWechatMiniProgram:
            object = self.wechatmini;
            break;
        case GMSharePlatformQQFriend:
            object = self.qq;
            break;
        case GMSharePlatformQQSpace:
            object = self.qqzone;
            break;
        case GMSharePlatformSinaWeibo:
            object = self.weibo;
            break;
        case GMSharePlatformDouyin:
            object = self.douyin;
            break;
        case GMSharePlatformWechatSessionSnapShot:
            object = self.wechatScreenshot;
            shareImage = self.wechatScreenshot.content;
            break;
        case GMSharePlatformWechatTimelineSnapShot:
            object = self.wechatlineScreenshot;
            shareImage = self.wechatlineScreenshot.content;
            break;
        default:
            break;
    }
    NSMutableDictionary *shareParams = [NSMutableDictionary dictionary];
    if (shareType == GMSharePlatformWechatMiniProgram) {
        [shareParams shareSetupMiniProgramShareParamsByUrl:self.url
                                                  userName:self.wechatmini.userName
                                                      path:self.wechatmini.path
                                                     title:self.wechatmini.title
                                               description:self.wechatmini.desc
                                                thumbImage:self.wechatmini.thumbImage
                                               hdImageData:self.wechatmini.hdImage];
    } else {
        if (self.videoUrl.length) {
            // 分享视频
            [shareParams shareSetupShareParamsByText:object.content images:shareImage url:[NSURL URLWithString:self.url] title:object.title videoUrl:[NSURL URLWithString:self.videoUrl] type:self.contentType];
        } else {
            [shareParams shareSetupShareParamsByText:object.content
                                              images:shareImage
                                                 url:[NSURL URLWithString:self.url]
                                               title:object.title
                                                type:self.contentType];
        }
    }
    return shareParams;
}

@end

@implementation GMDiaryShareObject

@end

@implementation GMShareConfigObject

@end
