//
//  KLTLocalSettingManager.m
//  KaiLiTe
//
//  Created by yuwell on 2024/7/25.
//  Copyright © 2024 boosal. All rights reserved.
//

#import "KLTLocalSettingManager.h"
#import "KLTUserDefautKeyDefine.h"
@implementation KLTLocalSettingManager

+ (KLTLocalSettingManager *)shareInstance {
    static dispatch_once_t once;
    static KLTLocalSettingManager *instance = nil;
    dispatch_once(&once,^{
        instance = [[KLTLocalSettingManager alloc] init];
        [instance startupLocalSettingInit];
    });
    
    return instance;
}

- (void)startupLocalSettingInit {
    NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
    
    NSNumber *unitSetting = [userDefaults objectForKey:KLTLocalUnitSettingKey];
    if (unitSetting) {
        _unitMGSetting = unitSetting.boolValue;
    } else {
        // 默认使用mg/dL单位
        _unitMGSetting = YES;
    }
    
    _mmolGluLimits = [userDefaults objectForKey:KLTMmolUnitGluLimitsSettingKey];
    _mgGluLimits = [userDefaults objectForKey:KLTMgUnitGluLimitsSettingKey];
    if (!_mmolGluLimits) {
        // 默认血糖高低限
        _mmolGluLimits = @[@3.9, @11.1];
    }
    if (!_mgGluLimits) {
        // 默认血糖高低限
        _mgGluLimits = @[@70, @200];
    }
    
    // 提醒设置页的告警类型设置
    NSNumber *alertKindType = [userDefaults objectForKey:KLTRemindAlertKindTypeKey];
    if (alertKindType) {
        _alertKindValue = alertKindType.integerValue;
    } else {
        // 默认KLTRemindAlertKindMediaTypeNOSoundVibration无声音+无震动
        _alertKindValue = KLTRemindAlertKindMediaTypeNOSoundVibration;
    }
    
    // 传感器设备二维码信息
    //Sensor device QR code information
    _deviceInfoSensor = [userDefaults objectForKey:KLTLocalDeviceQRCodeInfoKey];
    [self handleDeviceInfoSensor];

    NSNumber *canConnectOtherDevice = [userDefaults objectForKey:KLTLocalCanConnectedOtherDeviceKey];
    if (canConnectOtherDevice) {
        _canConnectOtherDevice = canConnectOtherDevice.boolValue;
    } else {
        // 默认为未绑定能连其他发射器设备
        _canConnectOtherDevice = YES;
    }
}

#pragma mark - 单位设置
// 默认使用mg/dL单位
- (void)setUnitMGSetting:(BOOL)unitMGSetting {
    _unitMGSetting = unitMGSetting;
    
    NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
    [userDefaults setObject:@(unitMGSetting) forKey:KLTLocalUnitSettingKey];
    [userDefaults synchronize];
}

#pragma mark - 血糖上下限数组（分单位保存）设置
- (void)setMgGluLimits:(NSArray<NSString *> *)mgGluLimits {
    _mgGluLimits = mgGluLimits;
    
    NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
    [userDefaults setObject:mgGluLimits forKey:KLTMgUnitGluLimitsSettingKey];
    [userDefaults synchronize];
}

- (void)setMmolGluLimits:(NSArray<NSString *> *)mmolGluLimits {
    _mmolGluLimits = mmolGluLimits;
    
    NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
    [userDefaults setObject:mmolGluLimits forKey:KLTMmolUnitGluLimitsSettingKey];
    [userDefaults synchronize];
}

#pragma mark -提醒设置页的告警类型设置、免打扰设置
- (void)setAlertKindValue:(KLTRemindAlertKindMediaType)alertKindValue {
    _alertKindValue = alertKindValue;
    
    NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
    [userDefaults setObject:@(alertKindValue) forKey:KLTRemindAlertKindTypeKey];
    [userDefaults synchronize];
}

#pragma mark - 传感器设备二维码信息
// 传感器设备二维码信息
- (void)setDeviceInfoSensor:(NSString *)deviceInfoSensor {
    _deviceInfoSensor = deviceInfoSensor;
    [self handleDeviceInfoSensor];

    NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
    [userDefaults setObject:deviceInfoSensor forKey:KLTLocalDeviceQRCodeInfoKey];
    [userDefaults synchronize];
}

- (void)handleDeviceInfoSensor {
    if (self.deviceInfoSensor.length > 0) {
        NSString *regex = @"^[A-Za-z].*";
        //创建谓词对象并设定条件的表达式
        NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", regex];
        if ([predicate evaluateWithObject:self.deviceInfoSensor]) {
            self.hasNumberPrefix = NO;
        } else {
            self.hasNumberPrefix = YES;
        }
    }
}

#pragma mark - 是否能连其他发射器

- (void)setCanConnectOtherDevice:(BOOL)canConnectOtherDevice {
    _canConnectOtherDevice = canConnectOtherDevice;
    
    NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
    [userDefaults setObject:@(canConnectOtherDevice) forKey:KLTLocalCanConnectedOtherDeviceKey];
    [userDefaults synchronize];
}

@end
