//
//  SetPWDSecondViewController.m
//  隐私保护
//
//  Created by APPLE on 2023/9/19.
//

#import "SetPWDSecondViewController.h"
#import <Masonry.h>

#import "connectDeviceManager.h"
#import "privacyModeTipViewController.h"

@interface SetPWDSecondViewController (){
    NSString *pwd;
    UILabel *pointView0;
    UILabel *pointView1;
    UILabel *pointView2;
    UILabel *pointView3;
    
    UILabel *topLabel;
}

@end

@implementation SetPWDSecondViewController
@synthesize oldPWD;

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    pwd = @"";
    [self drawAnyView];
}


- (void)drawAnyView{
    [self.navigationBar setHidden:YES];
    [self.toolBar setHidden:YES];
    
    [self.view setBackgroundColor:HWF5F7FAColor];
    
    /*上一步按钮*/
    UIButton *upStepBtn = [[UIButton alloc] init];
    [upStepBtn setTitle:NSLocalizedString(@"guide_set_pwd_up_step",nil) forState:(UIControlStateNormal)];
    [upStepBtn setTitleColor:HW05BAFBColor forState:(UIControlStateNormal)];
    [upStepBtn.titleLabel setFont:[UIFont systemFontOfSize:16]];
    [upStepBtn addTarget:self.navigationController
                  action:@selector(popViewControllerAnimated:)
        forControlEvents:(UIControlEventTouchUpInside)];
    
    [self.view addSubview:upStepBtn];
    [upStepBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(10.f);
        make.width.mas_equalTo(58.f);
        make.top.mas_equalTo(5.f + H_STATE_BAR);
        make.height.mas_equalTo(32.f);
    }];
    
    /*设置密码标题*/
    topLabel = [[UILabel alloc] init];
    [topLabel setFont:[UIFont boldSystemFontOfSize:16.f]];
    [topLabel setTextColor:HW333333Color];
    [topLabel setTextAlignment:(NSTextAlignmentCenter)];
    [topLabel setText:NSLocalizedString(@"guide_set_pwd_makesure_title",nil)];
    [self.view addSubview:topLabel];
    [topLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(20);
        make.right.mas_equalTo(-20);
        make.top.mas_equalTo(99.f + H_STATE_BAR);
    }];
    
    CGFloat w_pwd = 24.f;
    CGFloat h_pwd = 24.f;
    
    CGFloat pointFont = 20.f;
    /*密码视图*/
    pointView0 = [[UILabel alloc] init];
    [pointView0 setTextColor:HW0A132BColor];
    [pointView0 setBackgroundColor:HWE3E8F1Color];
    [pointView0 setClipsToBounds:YES];
    [pointView0 setTextAlignment:(NSTextAlignmentCenter)];
    [pointView0 setFont:[UIFont systemFontOfSize:pointFont]];
    [pointView0.layer setCornerRadius:w_pwd/2.f];
    [self.view addSubview:pointView0];
    [pointView0 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.view.mas_centerX).offset((0-2)*(w_pwd+w_pwd) + w_pwd/2.f);
        make.height.mas_equalTo(h_pwd);
        make.width.mas_equalTo(w_pwd);
        make.top.mas_equalTo(151.f + H_STATE_BAR);
    }];
    
    pointView1 = [[UILabel alloc] init];
    [pointView1 setTextColor:HW0A132BColor];
    [pointView1 setBackgroundColor:HWE3E8F1Color];
    [pointView1 setClipsToBounds:YES];
    [pointView1 setTextAlignment:(NSTextAlignmentCenter)];
    [pointView1 setFont:[UIFont systemFontOfSize:pointFont]];
    [pointView1.layer setCornerRadius:w_pwd/2.f];
    [self.view addSubview:pointView1];
    [pointView1 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.view.mas_centerX).offset((1-2)*(w_pwd+w_pwd) + w_pwd/2.f);
        make.height.mas_equalTo(h_pwd);
        make.width.mas_equalTo(w_pwd);
        make.top.mas_equalTo(151.f + H_STATE_BAR);
    }];
    
    pointView2 = [[UILabel alloc] init];
    [pointView2 setTextColor:HW0A132BColor];
    [pointView2 setBackgroundColor:HWE3E8F1Color];
    [pointView2 setClipsToBounds:YES];
    [pointView2 setTextAlignment:(NSTextAlignmentCenter)];
    [pointView2 setFont:[UIFont systemFontOfSize:pointFont]];
    [pointView2.layer setCornerRadius:w_pwd/2.f];
    [self.view addSubview:pointView2];
    [pointView2 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.view.mas_centerX).offset((2-2)*(w_pwd+w_pwd) + w_pwd/2.f);
        make.height.mas_equalTo(h_pwd);
        make.width.mas_equalTo(w_pwd);
        make.top.mas_equalTo(151.f + H_STATE_BAR);
    }];
    
    pointView3 = [[UILabel alloc] init];
    [pointView3 setTextColor:HW0A132BColor];
    [pointView3 setBackgroundColor:HWE3E8F1Color];
    [pointView3 setClipsToBounds:YES];
    [pointView3 setTextAlignment:(NSTextAlignmentCenter)];
    [pointView3 setFont:[UIFont systemFontOfSize:pointFont]];
    [pointView3.layer setCornerRadius:w_pwd/2.f];
    [self.view addSubview:pointView3];
    [pointView3 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.view.mas_centerX).offset((3-2)*(w_pwd+w_pwd) + w_pwd/2.f);
        make.height.mas_equalTo(h_pwd);
        make.width.mas_equalTo(w_pwd);
        make.top.mas_equalTo(151.f + H_STATE_BAR);
    }];
    
    /*下方绘制键盘按钮*/
    UIView *bgViewForNumberBoard = [[UIView alloc] init];
    [bgViewForNumberBoard setBackgroundColor:[UIColor clearColor]];
    CGFloat w_bgViewForNumberBoard = SCREEN_W - 2*22.f*AUTOSCALE;
    CGFloat h_bgViewForNumberBoard = 330.f;
    [self.view addSubview:bgViewForNumberBoard];
    [bgViewForNumberBoard mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(22.f*AUTOSCALE);
        make.height.mas_equalTo(h_bgViewForNumberBoard);
        make.width.mas_equalTo(w_bgViewForNumberBoard);
        make.bottom.mas_equalTo(-70.f);
    }];
    
    CGFloat w_key_btn = w_bgViewForNumberBoard/3.f;
    CGFloat h_key_btn = h_bgViewForNumberBoard/4.f;
    
    /*绘制键盘 1~9*/
    for (NSInteger nFori = 0; nFori < 9; nFori++) {
        CGFloat x_key_btn = (nFori%3)*w_key_btn;
        CGFloat y_key_btn = (nFori/3)*h_key_btn;
        
        UIButton *keyBoardBtn = [[UIButton alloc] init];
        [keyBoardBtn setBackgroundColor:[UIColor clearColor]];
        [keyBoardBtn setTitleColor:HW0A132BColor forState:(UIControlStateNormal)];
        [keyBoardBtn.titleLabel setFont:[UIFont boldSystemFontOfSize:40.f]];
        [keyBoardBtn setTitle:[NSString stringWithFormat:@"%ld",nFori+1] forState:(UIControlStateNormal)];
        [keyBoardBtn addTarget:self action:@selector(keyBoardBtnPressed:) forControlEvents:(UIControlEventTouchUpInside)];
        [bgViewForNumberBoard addSubview:keyBoardBtn];
        [keyBoardBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(x_key_btn);
            make.top.mas_equalTo(y_key_btn);
            make.width.mas_equalTo(w_key_btn);
            make.height.mas_equalTo(h_key_btn);
        }];
    }
    
    /*删除按钮*/
    UIButton *deleteBoardBtn = [[UIButton alloc] init];
    [deleteBoardBtn setBackgroundColor:[UIColor clearColor]];
    [deleteBoardBtn addTarget:self action:@selector(deleteBoardBtnPressed) forControlEvents:(UIControlEventTouchUpInside)];
    [deleteBoardBtn setImage:[UIImage imageNamed:@"key_board_delete"] forState:(UIControlStateNormal)];
    [bgViewForNumberBoard addSubview:deleteBoardBtn];
    [deleteBoardBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(0);
        make.bottom.mas_equalTo(0);
        make.width.mas_equalTo(w_key_btn);
        make.height.mas_equalTo(h_key_btn);
    }];
    
    /*0*/
    UIButton *keyBoardBtn0 = [[UIButton alloc] init];
    [keyBoardBtn0 setBackgroundColor:[UIColor clearColor]];
    [keyBoardBtn0 setTitleColor:HW0A132BColor forState:(UIControlStateNormal)];
    [keyBoardBtn0.titleLabel setFont:[UIFont boldSystemFontOfSize:40.f]];
    [keyBoardBtn0 setTitle:@"0" forState:(UIControlStateNormal)];
    [keyBoardBtn0 addTarget:self action:@selector(keyBoardBtnPressed:) forControlEvents:(UIControlEventTouchUpInside)];
    [bgViewForNumberBoard addSubview:keyBoardBtn0];
    [keyBoardBtn0 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(w_key_btn);
        make.bottom.mas_equalTo(0);
        make.width.mas_equalTo(w_key_btn);
        make.height.mas_equalTo(h_key_btn);
    }];
    
    /*OK*/
    UIButton *keyBoardBtnOK = [[UIButton alloc] init];
    [keyBoardBtnOK setBackgroundColor:[UIColor clearColor]];
    [keyBoardBtnOK setTitleColor:HW13B2EBColor forState:(UIControlStateNormal)];
    [keyBoardBtnOK.titleLabel setFont:[UIFont boldSystemFontOfSize:30.f]];
    [keyBoardBtnOK setTitle:@"OK" forState:(UIControlStateNormal)];
    [keyBoardBtnOK addTarget:self action:@selector(keyBoardBtnOKPressed) forControlEvents:(UIControlEventTouchUpInside)];
    [bgViewForNumberBoard addSubview:keyBoardBtnOK];
    [keyBoardBtnOK mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(2*w_key_btn);
        make.bottom.mas_equalTo(0);
        make.width.mas_equalTo(w_key_btn);
        make.height.mas_equalTo(h_key_btn);
    }];
}

- (void)deleteBoardBtnPressed{
    if (pwd.length > 0){
        NSInteger numberLength = pwd.length;
        if (numberLength == 1){
            [pointView0 setText:@""];
            [pointView0 setBackgroundColor:HWE3E8F1Color];
        }else if (numberLength == 2){
            [pointView1 setText:@""];
            [pointView1 setBackgroundColor:HWE3E8F1Color];
        }else if (numberLength == 3){
            [pointView2 setText:@""];
            [pointView2 setBackgroundColor:HWE3E8F1Color];
        }else if (numberLength == 4){
            [pointView3 setText:@""];
            [pointView3 setBackgroundColor:HWE3E8F1Color];
        }
        
        pwd = [pwd substringToIndex:numberLength-1];
    }
}

- (void)keyBoardBtnOKPressed{
    if (pwd.length == 4){
        if ([pwd isEqualToString:oldPWD]) {
            /*设置密码成功*/
            NSDictionary *deviceDict = [HWDataManager getObjectWithKey:Const_Have_Add_Device];
            NSMutableDictionary *dict = [[NSMutableDictionary alloc] initWithDictionary:deviceDict];
            [dict setObject:pwd forKey:Const_Have_Add_Device_PWD];
            [HWDataManager setObjectWithKey:Const_Have_Add_Device value:dict];
           
            
            if (deviceDict && [[deviceDict allKeys] containsObject:Const_Have_Add_Device_SN]){
                //有设备了先去做链接准备  // 80bec9c5
                NSString *SNStr = deviceDict[@"Const_Have_Add_Device_SN"];
                if(SNStr){
                    KWeakSelf
                    [[connectDeviceManager shareInstance] updateCardInfoBySN:SNStr withPwdStr:pwd didNetEnd:^(bool didSuc) {
                        
                        if(didSuc){
                            [weakSelf getPwdFunBy:SNStr];
                        }
                        else{
                            [[iToast makeText:NSLocalizedString(@"log_login_system_busy",nil)] show];
                        }
                    }];
                }
            }
            
            
        }else{
            /*提示密码不一致*/
            [topLabel setText:NSLocalizedString(@"guide_set_pwd_no_same",nil)];
        }
    }
}

- (void)keyBoardBtnPressed:(id)sender{
    UIButton *btn = (UIButton *)sender;
    NSString *selectNumberStr = btn.titleLabel.text;
    if (pwd.length < 4){
        NSInteger numberLength = pwd.length;
        if (numberLength == 0){
            [pointView0 setText:selectNumberStr];
            dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                [self->pointView0 setBackgroundColor:HW0A132BColor];
            });
        }else if (numberLength == 1){
            [pointView1 setText:selectNumberStr];
            dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                [self->pointView1 setBackgroundColor:HW0A132BColor];
            });
        }else if (numberLength == 2){
            [pointView2 setText:selectNumberStr];
            dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                [self->pointView2 setBackgroundColor:HW0A132BColor];
            });
        }else if (numberLength == 3){
            [pointView3 setText:selectNumberStr];
            dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                [self->pointView3 setBackgroundColor:HW0A132BColor];
            });
        }
        
        pwd = [pwd stringByAppendingString:selectNumberStr];
    }
}

#pragma mark 重新获取密码
-(void)getPwdFunBy:(NSString*)SNStr
{
    KWeakSelf
    [[connectDeviceManager shareInstance] getThridMsgBySN:SNStr needReconnect:NO  didNetEnd:^(bool didSuc) {
        
        if(didSuc){
            if(self->_isForgetPwdType){
                [[NSNotificationCenter defaultCenter] postNotificationName:forgetPwdDidSetNotification object:nil];
            }
            else{
//                [[NSNotificationCenter defaultCenter] postNotificationName:GuideOkNotification object:nil];
                [weakSelf gotoPrivacyModeTipVCFun];
            }
            
        }
        else{
            [[iToast makeText:NSLocalizedString(@"log_login_system_busy",nil)] show];
        }
        
    }];
}

- (void)gotoPrivacyModeTipVCFun
{
    privacyModeTipViewController *nextVC = [privacyModeTipViewController new];
    //[self.navigationController pushViewController:nextVC animated:YES];
    
    UIWindow* window = [iTools getKeyWindow];
    window.rootViewController = nextVC;
}

@end
