//
//  UmShare.m
//  JZPlus_RN
//
//  Created by 丁乐 on 2020/6/4.
//

#import "UmShare.h"
#import <UMShare/UMShare.h>
#import <UShareUI/UShareUI.h>

@interface UmShare ()


@end

@implementation UmShare

RCT_EXPORT_MODULE();

-(NSArray<NSString *> *)supportedEvents{
  return @[@""];
}

- (dispatch_queue_t)methodQueue{
  return dispatch_get_main_queue();
}


/// 微信和QQ完整版会校验合法的universalLink，不设置会在初始化平台失败
RCT_EXPORT_METHOD(initUniversalLink:(NSString *)wx qq:(NSString *)qq){
  
  NSMutableDictionary * universalLinkDic = [[NSMutableDictionary alloc] init];

  if (wx != nil) {
    [universalLinkDic setObject:wx forKey:@(UMSocialPlatformType_WechatSession)];
  }
  
  if (qq != nil) {
    [universalLinkDic setObject:qq forKey:@(UMSocialPlatformType_QQ)];
  }

  [UMSocialGlobal shareInstance].universalLinkDic = universalLinkDic;
}


/// 判断 平台是否安装
/// @param platformType 平台类型
RCT_EXPORT_METHOD(isInstall:(NSInteger)platformType success:(RCTPromiseResolveBlock)success rejecter:(RCTPromiseRejectBlock)reject){
    BOOL install = [[UMSocialManager defaultManager] isInstall:platformType];
    success(@(install));
}


/// 初始化分享平台 微信/QQ/新浪
/// @param data 初始化数据 包含 type:类型  appkey：app的key  appSecret：秘钥  redirectURL：重定向url
RCT_EXPORT_METHOD(initPlaform:(NSDictionary *)data ){
  
  UMSocialPlatformType type = [[data objectForKey:@"type"] integerValue];
  NSString *appkey = [[data objectForKey:@"appkey"] stringValue];
  NSString *appSecret = [[data objectForKey:@"appSecret"] stringValue];
  NSString *redirectURL = [[data objectForKey:@"redirectURL"] stringValue];
  /* 设置微信的appKey和appSecret */
  [[UMSocialManager defaultManager] setPlaform:type appKey:appkey appSecret:appSecret redirectURL:redirectURL];
}

/// 第三方平台登录
/// @param platformType 平台类型
RCT_EXPORT_METHOD(getUserInfoForPlatform:(NSInteger)platformType success:(RCTPromiseResolveBlock)success
                  rejecter:(RCTPromiseRejectBlock)reject){
  
  [[UMSocialManager defaultManager] getUserInfoWithPlatform:platformType currentViewController:nil completion:^(UMSocialUserInfoResponse * result, NSError *error) {
    if (error) {
      reject(@"错误",@"错误",error);
    }else{
      NSDictionary *userInfo = [result dictionaryWithValuesForKeys:@[@"uid",@"openid",@"accessToken",@"refreshToken",@"expiration",@"name",@"iconurl",@"unionGender",@"originalResponse"]];
      success(userInfo);
    }
  }];
}


/// 第三方平台 取消授权
/// @param platformType 平台类型
RCT_EXPORT_METHOD(cancelAuthWithPlatform:(NSInteger)platformType  success:(RCTPromiseResolveBlock)success
                  rejecter:(RCTPromiseRejectBlock)reject){
  
  [[UMSocialManager defaultManager] cancelAuthWithPlatform:platformType completion:^(id result, NSError *error) {
    if (error) {
      reject(@"错误",@"错误",error);
    }else{
      success(@"成功");
    }
  }];
}


/// 分享文本
/// @param data 数据
RCT_EXPORT_METHOD(shareText:(NSDictionary *)data success:(RCTPromiseResolveBlock)success
                  rejecter:(RCTPromiseRejectBlock)reject){
  UMSocialPlatformType platformType = [[data objectForKey:@"platformType"] integerValue];
  NSString *text = [[data objectForKey:@"text"] stringValue];
  
  //创建分享消息对象
  UMSocialMessageObject *messageObject = [UMSocialMessageObject messageObject];
  //设置文本
  messageObject.text = text;
  //调用分享接口
  [[UMSocialManager defaultManager] shareToPlatform:platformType messageObject:messageObject currentViewController:nil completion:^(UMSocialShareResponse *data, NSError *error) {
      if (error) {
        reject(@"分享报错",@"分享报错",error);
      }else{
        success(@"成功");
      }
  }];
}


/// 分享图片
/// @param data 数据
RCT_EXPORT_METHOD(shareImage:(NSDictionary *)data success:(RCTPromiseResolveBlock)success rejecter:(RCTPromiseRejectBlock)reject){
  
  UMSocialPlatformType platformType = [[data objectForKey:@"platformType"] integerValue];
  NSString *image = [[data objectForKey:@"image"] stringValue];
  
  //创建分享消息对象
  UMSocialMessageObject *messageObject = [UMSocialMessageObject messageObject];
  
  //创建图片内容对象
  UMShareImageObject *shareObject = [[UMShareImageObject alloc] init];
  shareObject.thumbImage = image;

  //设置图片
  [shareObject setShareImage:image];
  
  messageObject.shareObject = shareObject;

  //调用分享接口
  [[UMSocialManager defaultManager] shareToPlatform:platformType messageObject:messageObject currentViewController:nil completion:^(UMSocialShareResponse *data, NSError *error) {
      if (error) {
        reject(@"分享报错",@"分享报错",error);
      }else{
        success(@"成功");
      }
  }];
}

/// 分享网页
/// @param data 数据
RCT_EXPORT_METHOD(shareWebPage:(NSDictionary *)data success:(RCTPromiseResolveBlock)success rejecter:(RCTPromiseRejectBlock)reject){
  
  UMSocialPlatformType platformType = [[data objectForKey:@"platformType"] integerValue];
  NSString *title = [[data objectForKey:@"title"] stringValue];
  NSString *desc = [[data objectForKey:@"desc"] stringValue];
  NSString *icon = [[data objectForKey:@"icon"] stringValue];
  NSString *webpageUrl = [[data objectForKey:@"webpageUrl"] stringValue];
  
  //创建分享消息对象
  UMSocialMessageObject *messageObject = [UMSocialMessageObject messageObject];
  //创建网页内容对象
  UMShareWebpageObject *shareObject = [UMShareWebpageObject shareObjectWithTitle:title descr:desc thumImage:icon];
  //设置网页地址
  shareObject.webpageUrl = webpageUrl;
  //分享消息对象设置分享内容对象
  messageObject.shareObject = shareObject;
  
  //调用分享接口
  [[UMSocialManager defaultManager] shareToPlatform:platformType messageObject:messageObject currentViewController:nil completion:^(UMSocialShareResponse *data, NSError *error) {
    if (error) {
      reject(@"分享报错",@"分享报错",error);
    }else{
      success(@"成功");
    }
  }];
}



/// 分享音乐
/// @param data 数据
RCT_EXPORT_METHOD(shareMusic:(NSDictionary *)data success:(RCTPromiseResolveBlock)success rejecter:(RCTPromiseRejectBlock)reject){
  
  UMSocialPlatformType platformType = [[data objectForKey:@"platformType"] integerValue];
  NSString *title = [[data objectForKey:@"title"] stringValue];
  NSString *desc = [[data objectForKey:@"desc"] stringValue];
  NSString *icon = [[data objectForKey:@"icon"] stringValue];
  NSString *musicUrl = [[data objectForKey:@"musicUrl"] stringValue];
  NSString *musicDataUrl = [[data objectForKey:@"musicDataUrl"] stringValue];

  //创建分享消息对象
  UMSocialMessageObject *messageObject = [UMSocialMessageObject messageObject];
  //创建音乐内容对象
  UMShareMusicObject *shareObject = [UMShareMusicObject shareObjectWithTitle:title descr:desc thumImage:icon];
  //设置音乐网页播放地址
  shareObject.musicUrl = musicUrl;
// 这里设置音乐数据流地址（如果有的话，而且也要看所分享的平台支不支持）
  shareObject.musicDataUrl = musicDataUrl;
  //分享消息对象设置分享内容对象
  messageObject.shareObject = shareObject;
  
  //调用分享接口
  [[UMSocialManager defaultManager] shareToPlatform:platformType messageObject:messageObject currentViewController:nil completion:^(UMSocialShareResponse *data, NSError *error) {
    if (error) {
      reject(@"分享报错",@"分享报错",error);
    }else{
      success(@"成功");
    }
  }];
}

/// 分享视频
/// @param data 数据
RCT_EXPORT_METHOD(shareVideo:(NSDictionary *)data success:(RCTPromiseResolveBlock)success rejecter:(RCTPromiseRejectBlock)reject){
  
  UMSocialPlatformType platformType = [[data objectForKey:@"platformType"] integerValue];
  NSString *title = [[data objectForKey:@"title"] stringValue];
  NSString *desc = [[data objectForKey:@"desc"] stringValue];
  NSString *icon = [[data objectForKey:@"icon"] stringValue];
  NSString *videoUrl = [[data objectForKey:@"videoUrl"] stringValue];
  NSString *videoStreamUrl = [[data objectForKey:@"videoStreamUrl"] stringValue];

  //创建分享消息对象
  UMSocialMessageObject *messageObject = [UMSocialMessageObject messageObject];
  //创建视频内容对象
  UMShareVideoObject *shareObject = [UMShareVideoObject shareObjectWithTitle:title descr:desc thumImage:icon];
  //设置视频网页播放地址
  shareObject.videoUrl = videoUrl;
  //这里设置视频数据流地址（如果有的话，而且也要看所分享的平台支不支持）
  shareObject.videoStreamUrl = videoStreamUrl;
  //分享消息对象设置分享内容对象
  messageObject.shareObject = shareObject;

  //调用分享接口
  [[UMSocialManager defaultManager] shareToPlatform:platformType messageObject:messageObject currentViewController:nil completion:^(UMSocialShareResponse *data, NSError *error) {
    if (error) {
      reject(@"分享报错",@"分享报错",error);
    }else{
      success(@"成功");
    }
  }];
}


/// 分享小程序
/// @param data 数据
RCT_EXPORT_METHOD(shareMiniProgram:(NSDictionary *)data success:(RCTPromiseResolveBlock)success rejecter:(RCTPromiseRejectBlock)reject){
  
  UMSocialPlatformType platformType = [[data objectForKey:@"platformType"] integerValue];
  NSString *title = [[data objectForKey:@"title"] stringValue];
  NSString *desc = [[data objectForKey:@"desc"] stringValue];
  NSString *icon = [[data objectForKey:@"icon"] stringValue];
  NSString *userName = [[data objectForKey:@"userName"] stringValue];
  NSString *path = [[data objectForKey:@"path"] stringValue];
  NSString *hdImage = [[data objectForKey:@"hdImage"] stringValue];
  UShareWXMiniProgramType programType = [[data objectForKey:@"programType"] integerValue];
  
  //创建分享消息对象
  UMSocialMessageObject *messageObject = [UMSocialMessageObject messageObject];
  UMShareMiniProgramObject *shareObject = [UMShareMiniProgramObject shareObjectWithTitle:title descr:desc thumImage:icon];
  shareObject.userName = userName;
  shareObject.path = path;
  shareObject.hdImageData = [NSData dataWithContentsOfURL:[NSURL URLWithString:hdImage]];
  shareObject.miniProgramType = programType; // 可选体验版和开发板
  messageObject.shareObject = shareObject;

  //调用分享接口
  [[UMSocialManager defaultManager] shareToPlatform:platformType messageObject:messageObject currentViewController:nil completion:^(UMSocialShareResponse *data, NSError *error) {
    if (error) {
      reject(@"分享报错",@"分享报错",error);
    }else{
      success(@"成功");
    }
  }];
}




+(BOOL)handleOpenURL:(NSURL *)url options:(NSDictionary*)options
{
    return [[UMSocialManager defaultManager]  handleOpenURL:url options:options];
}


+(BOOL)handleUniversalLink:(id)userActivity options:(NSDictionary*)options
{
    return [[UMSocialManager defaultManager] handleUniversalLink:userActivity options:nil];
}





@end
