//
//  RNUMShare.m
//  FCSDemo
//
//  Created by Run on 2017/9/8.
//  Copyright © 2017年 Facebook. All rights reserved.
//

#import "RNUMShare.h"

@interface RNUMShare ()

@property (nonatomic, strong) UMSocialMessageObject *messageObject;

@end

@implementation RNUMShare

RCT_EXPORT_MODULE(UMShare)

RCT_EXPORT_METHOD(sharePlatform:(UMSocialPlatformType)type
                  appKey:(NSString *)key
                  appSecret:(NSString *)secret
                  redirectURL:(NSString *)url ) {
    [[UMSocialManager defaultManager] setPlaform:type appKey:key  appSecret:secret redirectURL:url];
}

//RCT_EXPORT_METHOD(login) {
//  [[UMSocialManager defaultManager] authWithPlatform:UMSocialPlatformType_Sina currentViewController:nil completion:^(id result, NSError *error) {
//    
//  }];
//  
////  [[UMSocialManager defaultManager] getUserInfoWithPlatform:UMSocialPlatformType_Sina currentViewController:nil completion:^(id result, NSError *error) {
////    if (error) {
////      
////    } else {
////      UMSocialUserInfoResponse *resp = result;
////      
////      // 授权信息
////      NSLog(@"Sina uid: %@", resp.uid);
////      NSLog(@"Sina accessToken: %@", resp.accessToken);
////      NSLog(@"Sina refreshToken: %@", resp.refreshToken);
////      NSLog(@"Sina expiration: %@", resp.expiration);
////      
////      // 用户信息
////      NSLog(@"Sina name: %@", resp.name);
////      NSLog(@"Sina iconurl: %@", resp.iconurl);
////      NSLog(@"Sina gender: %@", resp.unionGender);
////      
////      // 第三方平台SDK源数据
////      NSLog(@"Sina originalResponse: %@", resp.originalResponse);
////    }
////  }];
//}



RCT_EXPORT_METHOD(shareWeb:(NSString *)title
                  descr:(NSString *)descr
                  webcontent:(id)content
                  webUrl:(NSString *)url
                  platform:(UMSocialPlatformType)type
                  resolver:(RCTPromiseResolveBlock)resolve
                  rejecter:(RCTPromiseRejectBlock)reject) {
  UMSocialMessageObject *obj = [[UMSocialMessageObject alloc] init];

  //contentURl UIImage或者NSData类型，或者image_url
  UMShareWebpageObject *webObj = [UMShareWebpageObject shareObjectWithTitle:title descr:descr thumImage:content];
  webObj.webpageUrl = url;
  obj.shareObject = webObj;

  [self share:type object:obj resolver:resolve rejecter:reject];
}

RCT_EXPORT_METHOD(shareText:(NSString *)text
                  platform:(UMSocialPlatformType)type
                  resolver:(RCTPromiseResolveBlock)resolve
                  rejecter:(RCTPromiseRejectBlock)reject) {
  UMSocialMessageObject *obj = [[UMSocialMessageObject alloc] init];
  obj.text = text;
  
  [self share:type object:obj resolver:resolve rejecter:reject];
}

RCT_EXPORT_METHOD(shareImage){
  
}

- (void)share:(UMSocialPlatformType)type object:(UMSocialMessageObject *)obj  resolver:(RCTPromiseResolveBlock)resolve rejecter:(RCTPromiseRejectBlock)reject {
  dispatch_async(dispatch_get_main_queue(), ^{
    [[UMSocialManager defaultManager] shareToPlatform:type messageObject:obj currentViewController:nil completion:^(id result, NSError *error) {
      if (error) {
        NSLog(@"------%@",error);
        NSString *code = [NSString stringWithFormat:@"%ld",error.code];
        reject(code,error.localizedDescription,error);
      }
      else {
        resolve(@{@"code":@(200),@"message":@"分享成功"});
        NSLog(@"======%@",result);
        if ([result isKindOfClass:[UMSocialShareResponse class]]) {
          UMSocialShareResponse *resp = result;
          //分享结果消息
          UMSocialLogInfo(@"response message is %@",resp.message);
          //第三方原始返回的数据
          UMSocialLogInfo(@"response originalResponse data is %@",resp.originalResponse);
          
        }else{
          UMSocialLogInfo(@"response data is %@",result);
        }
      }
    }];
  });
}

- (NSDictionary *)constantsToExport {
  return @{@"sina":@(UMSocialPlatformType_Sina),
           @"WechatTimeLine":@(UMSocialPlatformType_WechatTimeLine),
           @"QQ":@(UMSocialPlatformType_QQ),
           @"Qzone":@(UMSocialPlatformType_Qzone)};
}


+ (void)initKey:(NSString *)key {
  if (!key) {
    NSString *reason = [NSString stringWithFormat:@"apiKey为空，请检查key是否正确设置。"];
    
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" message:reason delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
    
    [alert show];
  }
#if DEBUG
    [[UMSocialManager defaultManager] openLog:YES];
#endif
  [[UMSocialManager defaultManager] setUmSocialAppkey:key];
}

- (UMSocialMessageObject *)messageObject {
  if (!_messageObject) {
    _messageObject = [[UMSocialMessageObject alloc] init];
  }
    
  return _messageObject;
}

@end

@implementation RCTConvert (RNUMEnums)

//枚举
RCT_ENUM_CONVERTER(UMSocialPlatformType, (@{@"sina":@(UMSocialPlatformType_Sina),
             @"WechatTimeLine":@(UMSocialPlatformType_WechatTimeLine),
             @"QQ":@(UMSocialPlatformType_QQ),
             @"Qzone":@(UMSocialPlatformType_Qzone)
                                            }), UMSocialPlatformType_Sina, integerValue)

@end
