//
//  ViewController.m
//  QuickPlatforms
//
//

#import "ViewController.h"
#import <CommonCrypto/CommonDigest.h> // CC_MD5
#import <SMPCQuickSDK/SMPCQuickSDK.h>
#import "AppDelegate.h"
#import "MacroHelper.h"

@interface ViewController ()

//平台信息
@property (weak, nonatomic) IBOutlet UILabel *channelLab; //渠道标志


//用户信息
@property (weak, nonatomic) IBOutlet UILabel *userNameLab;
@property (weak, nonatomic) IBOutlet UILabel *userIdLab;


//按钮
@property (weak, nonatomic) IBOutlet UIButton *loginButton;
@property (weak, nonatomic) IBOutlet UIButton *logoutButton;
@property (weak, nonatomic) IBOutlet UIButton *rechargeButton;
@property (weak, nonatomic) IBOutlet UIButton *toolbarButton;
@property (weak, nonatomic) IBOutlet UIButton *settingButton;
@property (weak, nonatomic) IBOutlet UIButton *realAuthBtn;
@property (weak, nonatomic) IBOutlet UILabel *rechargeResultLabel;


@end

@implementation ViewController


#pragma mark -
#pragma mark - life cycle
- (void)viewWillAppear: (BOOL) animated {
    [super viewWillAppear : animated];
//设置物理设备的方向
//    NSNumber *orientationUnknown = [NSNumber numberWithInt:UIInterfaceOrientationUnknown] ;
//    [[UIDevice currentDevice] setValue:orientationUnknown forKey:@"orientation"];
//    NSNumber *orientationTarget = [NSNumber numberWithInt:UIInterfaceOrientationPortrait];
//    [[UIDevice currentDevice] setValue:orientationTarget forKey:@"orientation"];
}
- (void)viewDidLoad {
    [super viewDidLoad];
#ifdef QUDAO_NAME
    //配合在打包工具上添加的宏以达到控制渠道特殊处理
    NSLog(@"------------------QUDAO_NAME");
#endif
    // Do any additional setup after loading the view from its nib.
    //添加对QuickSDK通知的监听
    //登录
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(smpcQpLoginResult:) name:kSmpcQuickSDKNotiLogin object:nil];
    //注销
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(smpcQpLogoutResult:) name:kSmpcQuickSDKNotiLogout object:nil];
    //充值结果
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(smpcQpRechargeResult:) name:kSmpcQuickSDKNotiRecharge object:nil];
    //暂停结束 这个回调可以不用添加
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(smpcQpPauseOver:) name:kSmpcQuickSDKNotiPauseOver object:nil];
    [self loadMainView];
    _realAuthBtn.hidden = YES;
    UITextField * text = [[UITextField alloc]initWithFrame:CGRectMake(100, 100, 100, 40)];
    text.backgroundColor = [UIColor orangeColor];
//    [self.view addSubview:text];
//    [text becomeFirstResponder];
//    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(3.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
//        [self onLogin];
//    });
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


#pragma mark -
#pragma mark - Public Method

-(void)loadMainView {
    [self.channelLab setText:[NSString stringWithFormat:@"%d",[[SMPCQuickSDK defaultInstance] channelType]]];
}


#pragma mark -
#pragma mark - Pravire Method



#pragma mark -
#pragma mark - Notifications

- (void)smpcQpLoginResult:(NSNotification *)notify {
    NSString *token = [[SMPCQuickSDK defaultInstance] userToken];
    NSLog(@"登录成功通知%@=%@",notify, token);
    int error = [[[notify userInfo] objectForKey:kSmpcQuickSDKKeyError] intValue];
    NSDictionary *userInfo = [notify userInfo];
    if (error == 0) {
        NSString *uid = [[SMPCQuickSDK defaultInstance] userId];
        NSString *user_name = [[SMPCQuickSDK defaultInstance] userNick];
        //获取user_token，用于从服务器去验证用户信息
        NSString *user_token = userInfo[kSmpcQuickSDKKeyUserToken];
        [self.loginButton setHidden:YES];
        [self.logoutButton setHidden:NO];
        [self.rechargeButton setHidden:NO];
        [self.toolbarButton setHidden:NO];
        [self.settingButton setHidden:NO];
        [self.realAuthBtn setHidden:NO];
        [self.userNameLab setText:user_name];
        [self.userIdLab setText:uid];
        
        // 更新角色信息
        SMPCQuickSDKGameRoleInfo *gameRoleInfo = [SMPCQuickSDKGameRoleInfo new];
        gameRoleInfo.serverName = @"三国1区";
        gameRoleInfo.gameRoleName = @"倔强的学文";
        gameRoleInfo.serverId = @"4001"; //部分渠道需要纯数字字符串
        gameRoleInfo.gameRoleID = SMPCQuickSDK.defaultInstance.userId;//
//        gameRoleInfo.gameUserBalance = @"0";
        gameRoleInfo.vipLevel = @"23";
        gameRoleInfo.gameUserLevel = @"8";
        gameRoleInfo.partyName = @"iOS";
        gameRoleInfo.creatTime = @"0";
        gameRoleInfo.fightPower = @"0";//战力值
        gameRoleInfo.creatTime = @"1670925585";
        if (SMPCQuickSDK.defaultInstance.channelType == 240) {
            /** 这里用profession替代角色充值数据上报至小7游戏，如果需要请赋值否则会传递默认值-1*/
            gameRoleInfo.profession = @"100";
            /** 这里用professionId替代角色关卡数据上报至小7游戏，如果需要请赋值否则会传递默认值-1*/
            gameRoleInfo.professionId = @"101";
        }
        [[SMPCQuickSDK defaultInstance] updateRoleInfoWith:gameRoleInfo isCreate:NO];//如果这个角色是刚刚创建的，这里isCreate可以传YES

    }
}

- (void)smpcQpLogoutResult:(NSNotification *)notify {
    NSLog(@"%s",__func__);
    NSDictionary *userInfo = notify.userInfo;
    int errorCode = [userInfo[kSmpcQuickSDKKeyError] intValue];
    switch (errorCode) {
        case SMPC_QUICK_SDK_ERROR_NONE:
        {
            NSLog(@"注销成功");
            //注销成功
            [self.loginButton setHidden:NO];
            [self.logoutButton setHidden:YES];
            [self.rechargeButton setHidden:YES];
            [self.toolbarButton setHidden:YES];
            [self.settingButton setHidden:YES];
            [self.realAuthBtn setHidden:YES];
            [self.userNameLab setText:@""];
            [self.userIdLab setText:@""];
        }
            break;
        case SMPC_QUICK_SDK_ERROR_LOGOUT_FAIL:
        default:
        {
            //注销失败
            NSLog(@"注销失败");
        }
            break;
    }
    if (errorCode == SMPC_QUICK_SDK_ERROR_NONE) {
        
    }
    
}
- (void)smpcQpRechargeResult:(NSNotification *)notify{
    NSLog(@"充值结果%@",notify);
    NSDictionary *userInfo = notify.userInfo;
    int error = [[userInfo objectForKey:kSmpcQuickSDKKeyError] intValue];
    switch (error) {
        case SMPC_QUICK_SDK_ERROR_NONE:
        {
            //充值成功
            //QuickSDK订单号,cp下单时传入的订单号，渠道sdk的订单号，cp下单时传入的扩展参数
            NSString *orderID = userInfo[kSmpcQuickSDKKeyOrderId];
            NSString *cpOrderID = userInfo[kSmpcQuickSDKKeyCpOrderId];
            NSLog(@"充值成功数据：%@,%@",orderID,cpOrderID);
            _rechargeResultLabel.text = [NSString stringWithFormat:@"充值成功数据：%@,%@",orderID,cpOrderID];
        }
            break;
        case SMPC_QUICK_SDK_ERROR_RECHARGE_CANCELLED:
        case SMPC_QUICK_SDK_ERROR_RECHARGE_FAILED:
        {
            //充值失败
            NSString *orderID = userInfo[kSmpcQuickSDKKeyOrderId];
            NSString *cpOrderID = userInfo[kSmpcQuickSDKKeyCpOrderId];
            NSLog(@"充值失败数据%@,%@",orderID,cpOrderID);
            _rechargeResultLabel.text = [NSString stringWithFormat:@"充值失败数据：%@,%@",orderID,cpOrderID];
        }
            break;
        default:
            break;
    }
}
- (void)smpcQpPauseOver:(NSNotification *)notify{
    NSLog(@"收到QuickSDK暂停结束通知");
}

#pragma mark -
#pragma mark - Button Action
- (IBAction)onLogin{
    NSLog(@"bflogin keywindow=%@", UIApplication.sharedApplication.keyWindow);
    NSLog(@"bflogin delegatewin=%@", [UIApplication.sharedApplication.delegate window]);
    NSLog(@"bflogin windows=%@", UIApplication.sharedApplication.windows);
    int error = [[SMPCQuickSDK defaultInstance] login];
    if (error != 0) {
        NSLog(@"%d",error);
    }
    NSLog(@"afterlogin keywindow=%@", UIApplication.sharedApplication.keyWindow);
    NSLog(@"afterlogin delegatewin=%@", [UIApplication.sharedApplication.delegate window]);
    NSLog(@"afterlogin windows=%@", UIApplication.sharedApplication.windows);
}
- (IBAction)onRecharge:(id)sender{
    if ([SMPCQuickSDK defaultInstance].userId.length < 1) {
        UIAlertController * vc = [UIAlertController alertControllerWithTitle:@"Tips" message:@"please login" preferredStyle:UIAlertControllerStyleAlert];
        UIAlertAction * action = [UIAlertAction actionWithTitle:@"Sure" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
        }];
        [vc addAction:action];
        [self presentViewController:vc animated:YES completion:nil];
        return;
    }
    UIAlertController * vc = [UIAlertController alertControllerWithTitle:@"Tips" message:@"PleaseInputInformation" preferredStyle:UIAlertControllerStyleAlert];
    [vc addTextFieldWithConfigurationHandler:^(UITextField * _Nonnull textField) {
        textField.placeholder = @"PleaseInputProductName";
        textField.text = @"0.01";
        textField.keyboardType = UIKeyboardTypeNumberPad;
    }];
    
    UIAlertAction * action = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
        [self recharge:[vc.textFields objectAtIndex:0].text];
    }];
    [vc addAction:action];
    [self presentViewController:vc animated:YES completion:nil];
}
- (void)recharge:(NSString *)amount {
    //充值
    CFUUIDRef theUUID = CFUUIDCreate(NULL);
    CFStringRef guid = CFUUIDCreateString(NULL, theUUID);
    CFRelease(theUUID);
    NSString *uuidString = [((__bridge NSString *)guid) stringByReplacingOccurrencesOfString:@"-" withString:@""];
    CFRelease(guid);
    
    SMPCQuickSDKGameRoleInfo *role = [[SMPCQuickSDKGameRoleInfo alloc] init];
    SMPCQuickSDKPayOrderInfo *order = [[SMPCQuickSDKPayOrderInfo alloc] init];
    role.serverName = @"三国1区"; //必填
    role.gameRoleName = @"倔强的学文";//@""
    role.serverId = @"4001"; //部分渠道需要纯数字字符串
    role.gameRoleID = SMPCQuickSDK.defaultInstance.userId;
    role.gameUserBalance = @"0";//
    role.vipLevel = @"23";//
    role.gameUserLevel = @"8";
    role.partyName = @"";//
    
    order.goodsID = [NSString stringWithFormat:@"%d",1]; //必填 iap时注意和苹果开发者后台一致，或者渠道映射的
    order.productName = @"500元宝";//必填
    order.cpOrderID = [uuidString lowercaseString]; //必填 游戏订单号
    order.count = 1;  //必填 数量
    order.amount = amount.floatValue > 0 ? amount.floatValue:0.01; //必填 总价
    order.callbackUrl = @"";
    order.extrasParams = @"This is extrasParams!!!";
    //个别渠道要求单价*数量==总价
    if([SMPCQuickSDK defaultInstance].channelType == 9999){
        //通过判断渠道号处理特定渠道的参数
//        order.goodsID = @"productlist.name";
    }
    int error = [[SMPCQuickSDK defaultInstance] payOrderInfo:order
                                                    roleInfo:role];
    if (error!=0)
        NSLog(@"%d", error);
}
- (IBAction)onLogout:(id)sender{
    //代码注销
    [[SMPCQuickSDK defaultInstance] logout];
}
- (IBAction)realNameAuth:(UIButton *)sender {
    NSDictionary * dict = [[SMPCQuickSDK defaultInstance] realNameInfo];
    if ([dict[kSmpcQuickSDKKeyRealName] boolValue]) {
        NSLog(@"已经实名认证了");
        return;
    }
    int error = [[SMPCQuickSDK defaultInstance] realAuth];
     if (error != 0) {
         NSLog(@"%d",error);
     }
}

- (IBAction)buttonAction:(id)sender {
    switch (((UIButton*)sender).tag) {
        case 5000: {
            //个人中心
            int error = [[SMPCQuickSDK defaultInstance] enterUserCenter];
            if (error != 0) {
                NSLog(@"%d",error);
            }
            
        }   break;
        case 9998: {
            //显示工具条
            [self.toolbarButton setUserInteractionEnabled:NO];
            [self.toolbarButton setTitle:@"隐藏工具条" forState:UIControlStateNormal];
            [self.toolbarButton setTag:9999];
            [[SMPCQuickSDK defaultInstance] showToolBar:SMPC_QUICK_SDK_TOOLBAR_TOP_LEFT];
            [self.toolbarButton setUserInteractionEnabled:YES];
        }   break;
        case 9999: {
            //隐藏工具条
            [self.toolbarButton setUserInteractionEnabled:NO];
            [self.toolbarButton setTitle:@"显示工具条" forState:UIControlStateNormal];
            [self.toolbarButton setTag:9998];
            [[SMPCQuickSDK defaultInstance] hideToolBar];
            [self.toolbarButton setUserInteractionEnabled:YES];
        }   break;
        default:
            break;
    }
}


#pragma mark -
#pragma mark - StatusBarHidden

- (BOOL)prefersStatusBarHidden {
    return YES;
}


#pragma mark -
#pragma mark - Autorotate

// Override to allow orientations other than the default portrait orientation.
//- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
//    // Return YES for supported orientations
//    if (!IS_DEVICE_IPAD) {
//        return (UIInterfaceOrientationIsLandscape(interfaceOrientation) || UIInterfaceOrientationPortrait == interfaceOrientation);
//    }
//    return UIDeviceOrientationIsValidInterfaceOrientation(interfaceOrientation);
//}
//
//
//- (BOOL)shouldAutorotate {
//    return NO;
//}
//- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation {
//    return UIInterfaceOrientationPortrait;
//}
//- (UIInterfaceOrientationMask)supportedInterfaceOrientations {
//    return UIInterfaceOrientationPortrait;
//}
//
//- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation {
//    return UIInterfaceOrientationPortrait;
//}


@end
