//
//  RNAliTop.m
//  RNAliTop
//
//  Created by 王发果 on 2018/5/10.
//  Copyright © 2018年 王发果. All rights reserved.
//

#import "RNAliTop.h"
#if __has_include(<React/RCTBridge.h>)
#import <React/RCTBridge.h>
#else // Compatibility for RN version < 0.40
#import "RCTBridge.h"
#endif
#if __has_include(<React/RCTUtils.h>)
#import <React/RCTUtils.h>
#else // Compatibility for RN version < 0.40
#import "RCTUtils.h"
#endif
#if __has_include(<React/RCTBridgeModule.h>)
#import <React/RCTBridgeModule.h>
#else // Compatibility for RN version < 0.40
#import "RCTBridgeModule.h"
#endif
#import <AlibcTradeSDK/AlibcTradeSDK.h>
#import <AlibabaAuthSDK/ALBBSDK.h>

static NSString * AliTopTradeStatus = @"AliTopTradeStatus";
static NSString * AliTopLoginStatus = @"AliTopLoginStatus";

@implementation RNAliTop

RCT_EXPORT_MODULE(RNAliTop)

- (instancetype)init
{
    self = [super init];
    if (self) {
        [[ALBBSDK sharedInstance] setLoginResultHandler:^(ALBBSession *session) {
            RCTLog(@"ALBBSession = %@",[session isLogin] ? @"islogin " :@"is not login ");
            if([session isLogin]){
                RCTLog(@"ALBBUserDescription = %@",[[session getUser] ALBBUserDescription] );
            }
        }];
    }
    return self;
}
RCT_EXPORT_METHOD(isLogin:(RCTResponseSenderBlock)callback){
    if([[ALBBSession sharedInstance] isLogin]){
        NSMutableDictionary *body = [[NSMutableDictionary alloc] init];
        body[@"isLogin"] = @(1);
        callback(@[body]);
        return;
    }
    NSMutableDictionary *body = [[NSMutableDictionary alloc] init];
    body[@"isLogin"] = @(0);
    callback(@[body]);

}

RCT_EXPORT_METHOD(login :(RCTResponseSenderBlock)callback) {
    
    if(![[ALBBSession sharedInstance] isLogin]){
            id rootcontroller = [[UIApplication sharedApplication] delegate].window.rootViewController;
            [[ALBBSDK sharedInstance] auth:rootcontroller successCallback:^(ALBBSession *session){
                ALBBUser *user = [session getUser];
                NSMutableDictionary *body = [[NSMutableDictionary alloc] init];
                body[@"code"] = @(0);
                body[@"nick"] = user.nick;
                body[@"avatarUrl"] = user.avatarUrl;
                body[@"openId"] = user.openId;
                body[@"openSid"] = user.openSid;
                body[@"topAccessToken"] = user.topAccessToken;
                body[@"topAuthCode"] = user.topAuthCode;
                callback(@[body]);
            } failureCallback:^(ALBBSession *session, NSError *error){
                NSMutableDictionary *body = [[NSMutableDictionary alloc] init];
                body[@"code"] = @(error.code);
                body[@"msg"] = error.localizedDescription;
                callback(@[body]);
            }];
        
    }else{
        ALBBSession *session = [ALBBSession sharedInstance];
        ALBBUser *user = [session getUser];
        NSMutableDictionary *body = [[NSMutableDictionary alloc] init];
        body[@"code"] = @(0);
        body[@"nick"] = user.nick;
        body[@"avatarUrl"] = user.avatarUrl;
        body[@"openId"] = user.openId;
        body[@"openSid"] = user.openSid;
        body[@"topAccessToken"] = user.topAccessToken;
        body[@"topAuthCode"] = user.topAuthCode;
        callback(@[body]);
    }
}

/**
 * type
 * 0 :auto
 * 1 : native
 * 2 :webview
 */
RCT_EXPORT_METHOD(showItemDetailPage:(NSString*)itemId :(NSInteger)type :(RCTResponseSenderBlock)callback){

    if(![[ALBBSession sharedInstance] isLogin]){
        id rootcontroller = [[UIApplication sharedApplication] delegate].window.rootViewController;
        [[ALBBSDK sharedInstance] auth:rootcontroller successCallback:^(ALBBSession *session){
            ALBBUser *user = [session getUser];
            NSMutableDictionary *body = [[NSMutableDictionary alloc] init];
            body[@"code"] = @(0);
            body[@"nick"] = user.nick;
            body[@"avatarUrl"] = user.avatarUrl;
            body[@"openId"] = user.openId;
            body[@"openSid"] = user.openSid;
            body[@"topAccessToken"] = user.topAccessToken;
            body[@"topAuthCode"] = user.topAuthCode;
            callback(@[body]);
        } failureCallback:^(ALBBSession *session, NSError *error){
            NSMutableDictionary *body = [[NSMutableDictionary alloc] init];
            body[@"code"] = @(error.code);
            body[@"msg"] = error.localizedDescription;
            callback(@[body]);
        }];
        return;
    }
    id rootcontroller = [[UIApplication sharedApplication] delegate].window.rootViewController;
    id<AlibcTradeService> tradeService = [[AlibcTradeSDK sharedInstance] tradeService];
    id<AlibcTradePage> itemPage = [AlibcTradePageFactory itemDetailPage:itemId];
    AlibcTradeShowParams* showParam = [[AlibcTradeShowParams alloc] init];
    showParam.openType = AlibcOpenTypeAuto;
    if(type == 1){
        showParam.openType = AlibcOpenTypeNative;
    }else if (type == 2){
        showParam.openType = AlibcOpenTypeH5;
    }
    showParam.backUrl = @"tbopen23140447";
    showParam.isNeedPush = YES;
    showParam.linkKey = @"taobao";
    [tradeService show:rootcontroller page:itemPage showParams:showParam taoKeParams:nil trackParam:nil tradeProcessSuccessCallback:^(AlibcTradeResult * _Nullable result) {
        NSMutableDictionary *body = [[NSMutableDictionary alloc] init];
        body[@"code"] = @(0);
        NSLog(@"%ld",result.result);
        NSLog(@"%@",result.payResult);
        switch (result.result) {
            case AlibcTradeResultTypeAddCard:
            {
                body[@"type"] = @"addCard";
            }
                break;
            case AlibcTradeResultTypePaySuccess:
            {
                AlibcTradePayResult *payResult = result.payResult;
                body[@"type"] = @"paySuccess";
                body[@"paySuccessOrders"] = payResult.paySuccessOrders;
            }
                break;
            default:
                break;
        }
        
        callback(@[body]);
    } tradeProcessFailedCallback:^(NSError * _Nullable error) {
        NSMutableDictionary *body = [[NSMutableDictionary alloc] init];
        body[@"code"] = @(error.code);
        body[@"msg"] = error.localizedDescription;
        callback(@[body]);
    }];
    
}

RCT_EXPORT_METHOD(myOrdersPage:(NSInteger)status :(RCTResponseSenderBlock)callback){
        if(![[ALBBSession sharedInstance] isLogin]){
            id rootcontroller = [[UIApplication sharedApplication] delegate].window.rootViewController;
            [[ALBBSDK sharedInstance] auth:rootcontroller successCallback:^(ALBBSession *session){
                ALBBUser *user = [session getUser];
                NSMutableDictionary *body = [[NSMutableDictionary alloc] init];
                body[@"code"] = @(0);
                body[@"avatarUrl"] = user.avatarUrl;
                body[@"openId"] = user.openId;
                body[@"openSid"] = user.openSid;
                body[@"topAccessToken"] = user.topAccessToken;
                body[@"topAuthCode"] = user.topAuthCode;
                callback(@[body]);
            } failureCallback:^(ALBBSession *session, NSError *error){
                NSMutableDictionary *body = [[NSMutableDictionary alloc] init];
                body[@"code"] = @(error.code);
                body[@"msg"] = error.localizedDescription;
                callback(@[body]);
            }];
        }else{
            /**
             * 创建我的订单列表页面
             *
             * param status 所要展示订单的订单状态
             *  0: 为全部订单
             *  1: 为待付款订单
             *  2: 为待发货订单
             *  3: 为待收货订单
             *  4: 为待评价订单
             * param isAllOrder  是否显示全部订单
             *
             *  YES: 显示全部订单
             *  NO : 显示ISV自己创建的订单
             *
             * return 我的订单列表页面
             */
            id rootcontroller = [[UIApplication sharedApplication] delegate].window.rootViewController;
            id<AlibcTradeService> tradeService = [[AlibcTradeSDK sharedInstance] tradeService];
            id<AlibcTradePage> myOrdersPage = [AlibcTradePageFactory myOrdersPage:status isAllOrder:NO];
            AlibcTradeShowParams* showParam = [[AlibcTradeShowParams alloc] init];
            showParam.openType = AlibcOpenTypeH5;
            showParam.backUrl = @"tbopen23140447";
            showParam.isNeedPush = YES;
            [tradeService show:rootcontroller page:myOrdersPage showParams:showParam taoKeParams:nil trackParam:nil tradeProcessSuccessCallback:^(AlibcTradeResult * _Nullable result) {
                NSMutableDictionary *body = [[NSMutableDictionary alloc] init];
                body[@"code"] = @(0);
                switch (result.result) {
                    case AlibcTradeResultTypeAddCard:
                    {
                        body[@"type"] = @(AlibcTradeResultTypeAddCard);
                    }
                        break;
                    case AlibcTradeResultTypePaySuccess:
                    {
                        AlibcTradePayResult *payResult = result.payResult;
                        body[@"type"] = @(AlibcTradeResultTypePaySuccess);
                        body[@"paySuccessOrders"] = payResult.paySuccessOrders;
                    }
                        break;
                    default:
                        break;
                }
                
                callback(@[body]);
            } tradeProcessFailedCallback:^(NSError * _Nullable error) {
                NSMutableDictionary *body = [[NSMutableDictionary alloc] init];
                body[@"code"] = @(error.code);
                body[@"msg"] = error.localizedDescription;
                callback(@[body]);
            }];
        }
    
}
//获取邀请码
RCT_EXPORT_METHOD(inviteCode:(RCTResponseSenderBlock)callback){
   NSString *code = [[NSUserDefaults standardUserDefaults] objectForKey:@"keke_invite_code"];
    callback(@[code?:@""]);
}
//线程
- (dispatch_queue_t)methodQueue {
    return dispatch_get_main_queue();
}

@end
