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

#import "APPLocker.h"


static NSString * _Nonnull lockStatekey = @"lockStatekey_";
static NSString * _Nonnull lockpassword = @"lockpassword_";
static NSString * _Nonnull savepassword = @"savepassword_";

@implementation APPLocker
+ (instancetype)sharedLocker {
    static APPLocker *sharedInstance = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        sharedInstance = [[self alloc] init];
    });
    return sharedInstance;
}

#pragma mark - 密码锁启用状态

// 检查是否设置了密码
- (BOOL)isPasswordSet{
    NSString *userId = [UDManager.shareInstance getSDManager:dkuserId];
    NSString *str = [NSString stringWithFormat:@"%@%@",lockpassword,userId];
    NSString *lockps=[UDManager.shareInstance getSDManager:str];
    if(lockps){
        if(lockps.length>0){
            return true;
        }
    }
    return false;
}

// 检查密码锁是否启用
- (BOOL)isLockEnabled{
    
    NSString *userId = [UDManager.shareInstance getSDManager:dkuserId];
    NSString *str = [NSString stringWithFormat:@"%@%@",lockStatekey,userId];
    NSString *state=[UDManager.shareInstance getSDManager:str];
    if([state isEqualToString:@"true"]){
        return true;
    }
    return false;
}

// 设置新密码
- (BOOL)setPassword:(NSString *)password error:(NSError **)error{
    NSString *userId = [UDManager.shareInstance getSDManager:dkuserId];
    NSString *str = [NSString stringWithFormat:@"%@%@",lockpassword,userId];
    [UDManager.shareInstance setSDManager:password key:str];
    [self enableLockWithError:nil];//新增密码锁默认启用
    return true;
}

// 验证应用锁密码
- (BOOL)verifyPassword:(NSString *)password{
    
    NSString *userId = [UDManager.shareInstance getSDManager:dkuserId];
    NSString *str = [NSString stringWithFormat:@"%@%@",lockpassword,userId];
    NSString *lockps=[UDManager.shareInstance getSDManager:str];
    if(lockps){
        if([lockps isEqualToString:password]){
            return true;
        }
    }
    return false;
}
// 校验安卓全密码
- (BOOL)verifySecurtyPassword:(NSString *)password{
    
    NSString *userId = [UDManager.shareInstance getSDManager:dkuserId];
    NSString *str = [NSString stringWithFormat:@"%@%@",savepassword,userId];
    NSString *saveps=[UDManager.shareInstance getSDManager:str];
    if(saveps){
        if([saveps isEqualToString:password]){
            return true;
        }
    }
    return false;
}

// 修改应用锁密码
- (BOOL)changePasswordFrom:(NSString *)oldPassword toNewPassword:(NSString *)newPassword error:(NSError **)error{
    if(![self verifyPassword:oldPassword]){//原密码输入错误
        return  false;
    }
    if([self verifySecurtyPassword:newPassword]){//新密码与安全密码相同
        return  false;
    }
    NSString *userId = [UDManager.shareInstance getSDManager:dkuserId];
    NSString *str = [NSString stringWithFormat:@"%@%@",lockpassword,userId];
    [UDManager.shareInstance setSDManager:newPassword key:str];
    
    return true;
}

// 删除密码
- (BOOL)removePasswordWithError:(NSError **)error{
    return false;
}

// 启用应用锁密码锁
- (BOOL)enableLockWithError:(NSError **)error{
    NSString *userId = [UDManager.shareInstance getSDManager:dkuserId];
    NSString *str = [NSString stringWithFormat:@"%@%@",lockStatekey,userId];
    [UDManager.shareInstance setSDManager:@"true" key:str];
    return true;
}

// 禁用应用锁密码锁
- (BOOL)disableLockWithError:(NSError **)error{
    NSString *userId = [UDManager.shareInstance getSDManager:dkuserId];
    NSString *str = [NSString stringWithFormat:@"%@%@",lockStatekey,userId];
    [UDManager.shareInstance setSDManager:@"false" key:str];
    return true;
}

// 设置安全密码
- (BOOL)setSecurityPassword:(NSString *)password error:(NSError **)error{
    NSString *userId = [UDManager.shareInstance getSDManager:dkuserId];
    NSString *str = [NSString stringWithFormat:@"%@%@",savepassword,userId];
    [UDManager.shareInstance setSDManager:password key:str];
    return true;
}
@end
