//
//  SecurityPasswordController.m
//  AIIM
//
//  Created by qitewei on 2025/5/13.
//

#import "SecurityPasswordController.h"
#import "APPLocker.h"

@interface SecurityPasswordController ()
@property (nonatomic, strong) UIStackView * titleStackView;
@property (nonatomic, strong) UILabel * passwordTitle;
@property (nonatomic, strong) UILabel * confirmTitle;
@property (nonatomic, strong) UITextField * passwordTextfield;
@property (nonatomic, strong) UITextField * confirmTextfield;
@property (nonatomic, strong) UIButton * saveBtn;
@end

@implementation SecurityPasswordController

- (void)viewDidLoad {
    [super viewDidLoad];
    // 创建一个点击手势
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(dismissKeyboard)];
    // 将手势添加到主视图上
    [self.view addGestureRecognizer:tap];
    [self configUI];
}
-(void)dismissKeyboard{
    NSLog(@"dismissKeyboard");
    [self.view endEditing:YES];
}

- (void)configUI{
    [self setNavigationTitle:NSLocalizedString(@"userCenter-anquanmm", @"")];
    // 设置导航栏背景色
    [self setNavigationBarBackgroundColor:UIColor.clearColor];
    
    // 设置标题颜色和字体
    [self setNavigationTitleColor:[UIColor whiteColor] font:[UIFont boldSystemFontOfSize:16]];
    
    // 设置返回按钮
    [self setBackButtonTitle:@""];
    [self setBackButtonColor:[UIColor whiteColor]];
    
    UIImageView * bgImageView = [[UIImageView alloc] initWithImage:kImageMake(@"loginBG")];
    [self.view addSubview:bgImageView];
    [bgImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.top.bottom.mas_equalTo(0);
    }];
    
    [self.view addSubview:self.titleStackView];
    [self.titleStackView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.height.mas_equalTo(120);
        make.left.mas_equalTo(20);
        make.top.mas_equalTo(SCREEN_TOP+48);
        make.width.mas_equalTo(70);
    }];
    
    [self.view addSubview:self.passwordTextfield];
    [self.passwordTextfield mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(104);
        make.height.mas_equalTo(50);
        make.right.mas_equalTo(-20);
        make.centerY.mas_equalTo(self.passwordTitle.mas_centerY);
    }];
    
    [self.view addSubview:self.confirmTextfield];
    [self.confirmTextfield mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(104);
        make.height.mas_equalTo(50);
        make.right.mas_equalTo(-20);
        make.centerY.mas_equalTo(self.confirmTitle.mas_centerY);
    }];
    
    [self.view addSubview:self.saveBtn];
    [self.saveBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.height.mas_equalTo(46);
        make.left.mas_equalTo(20);
        make.right.mas_equalTo(-20);
        make.top.mas_equalTo(self.confirmTextfield.mas_bottom).offset(30);
    }];
}

#pragma mark event
- (void)saveButtonClicked{
    NSLog(@"1");
    NSString *password = self.passwordTextfield.text;
    NSString *confirmPassword = self.confirmTextfield.text;
    if (password.length == 0) {
        [MBProgressHUD showWithText:NSLocalizedString(@"Security_confirmPassword_alter", @"")];
        return;
    }
    
    if (confirmPassword.length == 0) {
        [MBProgressHUD showWithText:NSLocalizedString(@"Security_newPassword_alter", @"")];
        return;
    }
    
    if (![password isEqualToString:confirmPassword]) {
        [MBProgressHUD showWithText:NSLocalizedString(@"Security_passwordDefferent", @"")];
        return;
    }
    
    BOOL success = NO;
    NSError *error = nil;
    NSLog(@"3");
    // 验证安全密码不能跟应用锁密码一致
    success = [[APPLocker sharedLocker] verifyPassword:password];
    if (success) {
        NSLog(@"验证安全密码不能跟应用锁密码一致");
        [MBProgressHUD showWithText:NSLocalizedString(@"Security_lockf-same", @"")];
        return;
    }
    
    success = [APPLocker.sharedLocker setSecurityPassword:password error:&error];
    
    if (success) {
        [MBProgressHUD showWithText:NSLocalizedString(@"Common_successfully_set", @"")];
        [self.navigationController popViewControllerAnimated:YES];
    }
    else{
        [MBProgressHUD showWithText:NSLocalizedString(@"AppLock_fail", @"")];
        
    }
}

#pragma mark lazy
- (UIStackView *)titleStackView{
    if (!_titleStackView) {
        _titleStackView = [[UIStackView alloc] initWithArrangedSubviews:@[self.passwordTitle,self.confirmTitle]];
        _titleStackView.axis = UILayoutConstraintAxisVertical;
        _titleStackView.alignment = UIStackViewAlignmentFill;
        _titleStackView.distribution = UIStackViewDistributionEqualSpacing;
        _titleStackView.spacing = 64.f;
    }
    return _titleStackView;
}

- (UILabel *)passwordTitle{
    if (!_passwordTitle) {
        _passwordTitle = [[UILabel alloc] init];
        _passwordTitle.text = @"新密码";
        _passwordTitle.textColor = UIColor.whiteColor;
        _passwordTitle.font =  SYSFONT(16);
    }
    return _passwordTitle;
}

- (UILabel *)confirmTitle{
    if (!_confirmTitle) {
        _confirmTitle = [[UILabel alloc] init];
        _confirmTitle.text = @"确认密码";
        _confirmTitle.textColor = UIColor.whiteColor;
        _confirmTitle.font =  SYSFONT(16);
    }
    return _confirmTitle;
}

- (UITextField *)passwordTextfield{
    if (!_passwordTextfield) {
        _passwordTextfield = [[UITextField alloc] init];
        _passwordTextfield.textColor = UIColor.whiteColor;
        _passwordTextfield.borderStyle = UITextBorderStyleNone;
        _passwordTextfield.layer.borderWidth = 1.f;
        _passwordTextfield.layer.borderColor = globalColor(GCTypeDark2).CGColor;
        _passwordTextfield.layer.cornerRadius = 5.f;
        _passwordTextfield.secureTextEntry = YES;
//        _passwordTextfield.delegate = self;
        _passwordTextfield.leftViewMode = UITextFieldViewModeAlways;
        UIView * leftView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 15, 50)];
        _passwordTextfield.leftView = leftView;
    }
    return _passwordTextfield;
}

- (UITextField *)confirmTextfield{
    if (!_confirmTextfield) {
        _confirmTextfield = [[UITextField alloc] init];
        _confirmTextfield.textColor = UIColor.whiteColor;
        _confirmTextfield.borderStyle = UITextBorderStyleNone;
        _confirmTextfield.layer.borderWidth = 1.f;
        _confirmTextfield.layer.borderColor = globalColor(GCTypeDark2).CGColor;
        _confirmTextfield.layer.cornerRadius = 5.f;
        _confirmTextfield.secureTextEntry = YES;
//        _confirmTextfield.delegate = self;
        _confirmTextfield.leftViewMode = UITextFieldViewModeAlways;
        UIView * leftView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 15, 50)];
        _confirmTextfield.leftView = leftView;
    }
    return _confirmTextfield;
}

- (UIButton *)saveBtn{
    if (!_saveBtn) {
        _saveBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [_saveBtn setTitle:@"保存" forState:UIControlStateNormal];
        [_saveBtn setBackgroundColor:globalColor(GCTypeGreen)];
        [_saveBtn setTitleColor:UIColor.blackColor forState:UIControlStateNormal];
        _saveBtn.titleLabel.font = [UIFont systemFontOfSize:16 weight:UIFontWeightMedium];
        _saveBtn.layer.cornerRadius = 5.f;
        _saveBtn.layer.masksToBounds = YES;
        [_saveBtn addTarget:self action:@selector(saveButtonClicked) forControlEvents:UIControlEventTouchUpInside];
    }
    return _saveBtn;
}

@end
