//
//  quickLoginViewController.m
//  Open
//
//  Created by 看看智能 on 2019/5/24.
//  Copyright © 2019 黄江涛 . All rights reserved.
//

#import "quickLoginViewController.h"
#import "ViewController.h"
@interface quickLoginViewController ()

@end

@implementation quickLoginViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor clearColor];
    
    //耗时开始计时
    CFAbsoluteTime startButtonClick   = CFAbsoluteTimeGetCurrent();
    
   // [sender setEnabled:NO];
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
      //  [sender setEnabled:YES];
    });
    
    CGFloat screenScale = [UIScreen mainScreen].bounds.size.width/375.0;
    
    CLUIConfigure * baseUIConfigure = [CLUIConfigure new];
    baseUIConfigure.viewController = self;
    //NAV
    baseUIConfigure.cl_navigation_backgroundClear = @(YES);
    baseUIConfigure.cl_navigation_backBtnImage = [UIImage imageNamed:@"928返回"];
    
    //LOGO
    baseUIConfigure.clLogoImage = [UIImage imageNamed:@"logo"];
    baseUIConfigure.clLogoWidth = @(100);
    baseUIConfigure.clLogoHeight = @(100);
    baseUIConfigure.clLogoOffsetY = @(100 * screenScale);
    
    //手机号显示控件
    baseUIConfigure.clPhoneNumberColor = UIColor.blackColor;
    baseUIConfigure.clPhoneNumberOffsetY = @(250*screenScale);
    baseUIConfigure.clPhoneNumberWidth = @(200);
    baseUIConfigure.clPhoneNumberHeight = @(20);
    baseUIConfigure.clPhoneNumberFont = [UIFont fontWithName:@"PingFang-SC-Medium" size:20.0];
    
    baseUIConfigure.clLoginBtnText = @"本机号一键登录";
    baseUIConfigure.clLoginBtnBgColor = [UIColor blueColor];
    baseUIConfigure.clLoginBtnTextColor = [UIColor whiteColor];
    baseUIConfigure.clLoginBtnOffsetY = @(290*screenScale);
    baseUIConfigure.clLoginBtnWidth = @(self.view.bounds.size.width * 0.8);
    baseUIConfigure.clLoginBtnHeight =  @(40);
    baseUIConfigure.clLoginBtnCornerRadius = @(20);
    
    // baseUIConfigure.clAppPrivacyName = @"《我测条款名》";
    // baseUIConfigure.clAppPrivacyUrlString = @"https://www.baidu.com";
    // baseUIConfigure.clAppPrivacyColor = UIColor.greenColor;
    
    //CustomAreaView
    __weak typeof(self) weakSelf = self;
    baseUIConfigure.customAreaView = ^(UIView * _Nonnull customAreaView) {
        //customAreaView为导航条以下的全屏 375*667比例系数适配 默认375*667下一键登录button底部y约为 270
        
        UIButton * button = [[UIButton alloc]init];
        [button setTitle:@"其他方式登录" forState:(UIControlStateNormal)];
        button.titleLabel.font = [UIFont systemFontOfSize:14];
        [button setTitleColor:[UIColor grayColor] forState:(UIControlStateNormal)];
        [button addTarget:weakSelf action:@selector(otherLoginWayBtnCliced:) forControlEvents:(UIControlEventTouchUpInside)];
        [customAreaView addSubview:button];
        
        [button mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.right.mas_equalTo(0);
            make.top.mas_equalTo(340*screenScale);
            make.height.mas_equalTo(40);
        }];
    };
    
    [SVProgressHUD setContainerView:self.view];
    [SVProgressHUD show];
    [CLShanYanSDKManager quickAuthLoginWithConfigure: baseUIConfigure timeOut:4 complete:^(CLCompleteResult * _Nonnull completeResult) {
        
        [SVProgressHUD dismiss];
        
        if (completeResult.error) {
            
            //提示：错误无需提示给用户，可以在用户无感知的状态下直接切换登录方式
            if (completeResult.code == 1011){
                //用户取消登录（点返回）
                //处理建议：如无特殊需求可不做处理，仅作为交互状态回调，此时已经回到当前用户自己的页面
                [SVProgressHUD showInfoWithStatus: @"用户取消免密登录"];
            }else{
                //处理建议：其他错误代码表示闪验通道无法继续，可以统一走开发者自己的其他登录方式，也可以对不同的错误单独处理
                //1003    一键登录获取token失败
                //1008    未开启移动网络
                //1009    未检测到sim卡
                //其他     其他错误//
                
                dispatch_async(dispatch_get_main_queue(), ^{
                    // LoginResultController *vc = [LoginResultController new];
                    // vc.error = completeResult.error;
                    // [weakSelf.navigationController pushViewController:vc animated:YES];
                    //  NSLog(@"%@",completeResult.error);
                });
            }
        }else{
            dispatch_async(dispatch_get_main_queue(), ^{
                
                //                    //手动管理页面销毁
                //                    if (manualDismiss == YES) {
                //                        [self dismissViewControllerAnimated:YES completion:nil];
                //                        //释放内存和引用
                //                        [CLShanYanSDKManager clRelease];
                //                    }
                
                NSLog(@"quickAuthLogin Success:%@",completeResult.data);
                //LoginResultController *vc = [LoginResultController new];
                //vc.completeResultData = completeResult.data;
                //vc.timesStart = startButtonClick;
                //[self getPhonenumber];
                
                
                [logNetModel QuickLogin:[completeResult.data objectForKey:@"appId"] andAccessToken:[completeResult.data objectForKey:@"accessToken"] andTelecom:[completeResult.data objectForKey:@"telecom"] andTimeStamp:[completeResult.data objectForKey:@"timestamp"] andRandoms:[completeResult.data objectForKey:@"randoms"] andVersion:[completeResult.data objectForKey:@"version"] andDevice:[completeResult.data objectForKey:@"device"] andSign:[completeResult.data objectForKey:@"sign"] completionHandle:^(logModel_2 *model, NSError *error) {
                    {
                        
                        [SVProgressHUD dismiss];
                        NSLog(@"12345678");
                        NSLog(@"logResultModel%@",model.result);
                        
                        if ([model.code isEqualToString:@"0"]) {
                            
                            ViewController *vc = [ViewController new];
                            [self.navigationController pushViewController:vc animated:YES];
                            
                            
                            NSLog(@"123%@",model.code);
                            self.peopleMessage =[[NSString alloc]init];
                            NSLog(@"234%@",self.peopleMessage);
                            //判断账号是否登入时候使用
                            [[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"firstlog"];
                            [getPeopleID getPeopletitle:model.result.cus_name];
                            [getPeopleID getPeoplePhoto:model.result.photo];
                            [getPeopleID getPeopleID:model.result.ID];
                            [getPeopleID getPeopleIphoneNmber:model.result.telphone];
                            AppDelegate *delegate=(AppDelegate*)[[UIApplication sharedApplication]delegate];
                            
                            if ( delegate.isLogin == NO) {
                                [getPeopleID getProvince:model.result.provinceName];
                                
                            }
                            if (model.result.account_type == 0) {
                                self.editText =NO;
                                //_passView.hidden =YES;
                                //_registerView.hidden =YES;
                                if ([self.thirdStr isEqualToString:@"登入"]) {
                                    
                                }else{
                                    [getPeopleID getpeopleLimitedAuthority:YES];
                                    [self.navigationController popViewControllerAnimated:YES];
                                }
                                
                            }else if(model.result.account_type == 12){
                                //打开注册的页面
                                
//
//                                //s注册
//                                FandRViewController *logVC =    [FandRViewController sharedInstance];
//                                logVC.strTitle =@"注册";
//                                logVC.changeBut =YES;
//                                logVC.emailSender =NO;
//                                logVC.registLabelTwo.text =@"注册";
//                                logVC.hidesBottomBarWhenPushed =YES;
//                                [self.navigationController pushViewController:logVC animated:YES];
//
                                
                                
                            }else
                            {
                                [getPeopleID getpeopleLimitedAuthority:NO];
                                //[self.navigationController popViewControllerAnimated:YES];
                            }
                            
                        }else{
                            codeSelected *code = [codeSelected MustTakeReturn:[model.code intValue]];
                            [code returnCodeMethod];
                            
                        }
                    }
                    
                    
                    
                    
                    
                    
                    
                }];
                
                
              
                //vc.completeResultData = completeResult.data;
                //vc.timesStart = startButtonClick;
//                if(weakSelf.navigationController){
//                    [weakSelf.navigationController pushViewController:vc animated:YES];
//                } else {
//                    [weakSelf presentViewController:vc animated:YES completion:^{
//
//                    }];
//                }
                
                
                
                
//                if(weakSelf.navigationController){
//                    // [weakSelf.navigationController pushViewController:vc animated:YES];
//                } else {
//                    // [weakSelf presentViewController:vc animated:YES completion:^{
//                    
//                    // }];
//                }
            });
        }
    }];
    
    
    
    
}
+ (LoginViewController *)sharedInstance{
    static quickLoginViewController *p = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        p = [quickLoginViewController new];
    });
    return p;
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
