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

#import "APPLockerChangeController.h"
#import "AppLockPasswordController.h"

#import "APPLocker.h"

@interface APPLockerChangeController ()
@property (nonatomic, strong) UISwitch * lockSwitch;
@property (nonatomic, strong) UILabel * switchTitle;
@property (nonatomic, strong) UIButton * changePwdBtn;
@end

@implementation APPLockerChangeController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self configUI];
}

- (void)configUI{
    [self setNavigationTitle:NSLocalizedString(@"userCenter-yingysm", @"")];
    // 设置导航栏背景色
    [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.switchTitle];
    [self.view addSubview:self.lockSwitch];
    [self.view addSubview:self.changePwdBtn];
    
    [self.switchTitle mas_makeConstraints:^(MASConstraintMaker *make) {
        make.height.mas_equalTo(16);
        make.left.mas_equalTo(30);
        make.top.mas_equalTo(SCREEN_TOP+48);
    }];
    
    [self.lockSwitch mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(-30);
        make.centerY.mas_equalTo(self.switchTitle.mas_centerY);
    }];
    
    [self.changePwdBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.height.mas_equalTo(50);
        make.left.mas_equalTo(30);
        make.right.mas_equalTo(-30);
        make.top.mas_equalTo(self.switchTitle.mas_bottom).offset(40);
    }];
    
    self.lockSwitch.on = [APPLocker.sharedLocker isLockEnabled];
}

#pragma mark event
- (void)changePwdButtonClicked{
    AppLockPasswordController * lockVc = [[AppLockPasswordController alloc] init];
    lockVc.mode = LockViewModeChange;
    UINavigationController * navi = [[UINavigationController alloc] initWithRootViewController:lockVc];
    navi.modalPresentationStyle = UIModalPresentationFullScreen;
    [self presentViewController:navi animated:YES completion:nil];
}

- (void)lockSwitchValueChange{
    NSError *error = nil;
    APPLocker *locker = [APPLocker sharedLocker];
    if (self.lockSwitch.isOn) {
        // 启用密码锁
        NSLog(@"111111");
        if (![locker enableLockWithError:&error]) {
            NSLog(@"启用失败: %@", error.localizedDescription);
        }
    }else{
        NSLog(@"2222");
        // 禁用密码锁
        if (![locker disableLockWithError:&error]) {
            NSLog(@"禁用失败: %@", error.localizedDescription);
        }
    }
}

#pragma mark lazy
- (UISwitch *)lockSwitch{
    if (!_lockSwitch) {
        _lockSwitch = [[UISwitch alloc] init];
        _lockSwitch.onTintColor = globalColor(GCTypeGreen);
        _lockSwitch.thumbTintColor = UIColor.whiteColor;
        [_lockSwitch addTarget:self action:@selector(lockSwitchValueChange) forControlEvents:UIControlEventValueChanged];
    }
    return _lockSwitch;
}

- (UILabel *)switchTitle{
    if (!_switchTitle) {
        _switchTitle = [[UILabel alloc] init];
        _switchTitle.textColor = UIColor.whiteColor;
        _switchTitle.text = NSLocalizedString(@"AppLock_switch", @"");
        _switchTitle.font = SYSFONT(16);
    }
    return _switchTitle;
}

- (UIButton *)changePwdBtn{
    if (!_changePwdBtn) {
        _changePwdBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [_changePwdBtn setBackgroundColor:globalColor(GCTypeGreen)];
//        [_changePwdBtn setBackgroundColor:[UIColor jk_colorWithHexString:@"#6E75FB"]];
        [_changePwdBtn setTitle:NSLocalizedString(@"userCenter-xiugaimm", @"") forState: UIControlStateNormal];
        [_changePwdBtn setTitleColor:UIColor.blackColor forState:UIControlStateNormal];
        _changePwdBtn.titleLabel.font = SYSFONT(16);
        _changePwdBtn.layer.cornerRadius = 5.f;
        _changePwdBtn.layer.masksToBounds = YES;
        [_changePwdBtn addTarget:self action:@selector(changePwdButtonClicked) forControlEvents:UIControlEventTouchUpInside];
    }
    return _changePwdBtn;
}

#pragma mark statusBar
- (UIStatusBarStyle)preferredStatusBarStyle{
    return UIStatusBarStyleLightContent;
}

@end
