//
//  ChangeLogoViewController.m
//  双子星云手机
//
//  Created by APPLE on 2023/8/18.
//

#import "ChangeLogoViewController.h"
#import "LogoSelectView.h"
#import "iPhone.h"
#import "UITextfield+NLLimit.h"

@interface ChangeLogoViewController ()<UITextFieldDelegate,LogoSelectViewDelegate>
{
    LogoSelectView *logoSelectView;
    UITextField *pwdTextField;
    UITextField *pwdAgainTextField;
    UIButton *okBtn;
}

@end

@implementation ChangeLogoViewController

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

- (void)drawAnyView{
    [self.view setBackgroundColor:HWF5F7FAColor];
    
    [self.toolBar setHidden:YES];
    [self.navigationBar setHidden:YES];
    [self.navBarBGView setHidden:NO];
    [self.titleLabel setText:NSLocalizedString(@"my_set_no_change_login",nil)];
    
    logoSelectView = [[LogoSelectView alloc] init];
    logoSelectView.delegate = self;
    //logoSelectView.backgroundColor = [UIColor redColor];
    [self.view addSubview:logoSelectView];
    [logoSelectView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.height.mas_equalTo(256);
        make.left.mas_equalTo(0);
        make.right.mas_equalTo(0);
        make.top.equalTo(self.navBarBGView.mas_bottom);
    }];
    
    /*修改入口密码*/
//    UILabel *pwdLabel = [[UILabel alloc] init];
//    [pwdLabel setFont:[UIFont boldSystemFontOfSize:16.f]];
//    [pwdLabel setTextColor:HW0A132BColor];
//    [pwdLabel setText:NSLocalizedString(@"logo_set_pwd_tips",nil)];
//
//    [self.view addSubview:pwdLabel];
//    [pwdLabel mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.top.equalTo(logoSelectView.mas_bottom).offset(24.f);
//        make.left.mas_equalTo(16.f);
//        make.right.mas_equalTo(-16.f);
//        make.height.mas_equalTo(20.f);
//    }];
//
//    NSString *placeholderStr  = NSLocalizedString(@"logo_input_pwd_tips",nil);
//    NSString *placeholderStr1  = NSLocalizedString(@"logo_input_pwd_again_tips",nil);
//    CGFloat fontSize          = 13.f;
//    CGFloat textfontSize      = 16.f;
//    CGFloat inputTextFieldH   = 48.f;
//    NSMutableParagraphStyle *style = [[NSMutableParagraphStyle alloc] init];
//    style.alignment = NSTextAlignmentLeft;
//
//    NSAttributedString *attri = nil;
//    NSAttributedString *attri1 = nil;
//    NSString *verphone = [iPhone phoneVersion];
//
//    if ([verphone isEqualToString:@"10.3.3"]){
//        attri = [[NSAttributedString alloc] initWithString:placeholderStr
//        attributes:@{NSForegroundColorAttributeName:COLOR_PACEHOLDER,
//                     NSFontAttributeName:[UIFont systemFontOfSize:fontSize],
//                     NSBaselineOffsetAttributeName:[NSNumber numberWithFloat:-1.5],
//                     NSParagraphStyleAttributeName:style}];
//        attri1 = [[NSAttributedString alloc] initWithString:placeholderStr1
//        attributes:@{NSForegroundColorAttributeName:COLOR_PACEHOLDER,
//                     NSFontAttributeName:[UIFont systemFontOfSize:fontSize],
//                     NSBaselineOffsetAttributeName:[NSNumber numberWithFloat:-1.5],
//                     NSParagraphStyleAttributeName:style}];
//    }else{
//        attri = [[NSAttributedString alloc] initWithString:placeholderStr
//        attributes:@{NSForegroundColorAttributeName:COLOR_PACEHOLDER,
//                     NSFontAttributeName:[UIFont systemFontOfSize:fontSize],
//                     NSParagraphStyleAttributeName:style}];
//        attri1 = [[NSAttributedString alloc] initWithString:placeholderStr1
//        attributes:@{NSForegroundColorAttributeName:COLOR_PACEHOLDER,
//                     NSFontAttributeName:[UIFont systemFontOfSize:fontSize],
//                     NSBaselineOffsetAttributeName:[NSNumber numberWithFloat:-1.5],
//                     NSParagraphStyleAttributeName:style}];
//    }
//
//    pwdTextField = [[UITextField alloc] init];
//    pwdTextField.delegate = self;
//    [pwdTextField setNl_maxLength:4];
//    [pwdTextField setKeyboardType:(UIKeyboardTypeNumberPad)];
//    [self.view addSubview:pwdTextField];
//    [pwdTextField mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.top.equalTo(pwdLabel.mas_bottom).offset(12.f);
//        make.left.mas_equalTo(16.f);
//        make.right.mas_equalTo(-16.f);
//        make.height.mas_equalTo(inputTextFieldH);
//    }];
//    pwdTextField.attributedPlaceholder = attri;
//    pwdTextField.layer.cornerRadius = 8.f;
//    [pwdTextField setFont:[UIFont systemFontOfSize:textfontSize]];
//    [pwdTextField setTextColor:HW0A132BColor];
//    [pwdTextField setBackgroundColor:[UIColor whiteColor]];
//    UIView *leftView = [[UIView alloc] initWithFrame:(CGRectMake(0, 0, 15, inputTextFieldH))];
//    [leftView setBackgroundColor:[UIColor clearColor]];
//    [pwdTextField setLeftView:leftView];
//    [pwdTextField setLeftViewMode:(UITextFieldViewModeAlways)];
//
//    pwdAgainTextField = [[UITextField alloc] init];
//    pwdAgainTextField.delegate = self;
//    [pwdAgainTextField setNl_maxLength:4];
//    [pwdAgainTextField setKeyboardType:(UIKeyboardTypeNumberPad)];
//    [self.view addSubview:pwdAgainTextField];
//    [pwdAgainTextField mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.top.equalTo(pwdTextField.mas_bottom).offset(15.f);
//        make.left.mas_equalTo(16.f);
//        make.right.mas_equalTo(-16.f);
//        make.height.mas_equalTo(inputTextFieldH);
//    }];
//    pwdAgainTextField.attributedPlaceholder = attri1;
//    pwdAgainTextField.layer.cornerRadius = 8.f;
//    [pwdAgainTextField setFont:[UIFont systemFontOfSize:textfontSize]];
//    [pwdAgainTextField setTextColor:HW0A132BColor];
//    [pwdAgainTextField setBackgroundColor:[UIColor whiteColor]];
//    UIView *leftView1 = [[UIView alloc] initWithFrame:(CGRectMake(0, 0, 15, inputTextFieldH))];
//    [leftView1 setBackgroundColor:[UIColor clearColor]];
//    [pwdAgainTextField setLeftView:leftView1];
//    [pwdAgainTextField setLeftViewMode:(UITextFieldViewModeAlways)];
//
    /*确认按钮*/
    okBtn = [[UIButton alloc] init];
    CGFloat okBtn_W = SCREEN_W - 2*16.f;
    
    // gradient
    CAGradientLayer *gl = [CAGradientLayer layer];
    gl.frame = CGRectMake(0,0,okBtn_W,48);
    gl.startPoint = CGPointMake(0, 0.5);
    gl.endPoint = CGPointMake(1, 0.5);
    gl.colors = @[(__bridge id)HW0CDEFDColor.CGColor, (__bridge id)HW058DFBColor.CGColor];
    gl.locations = @[@(0), @(1.0f)];
    okBtn.layer.cornerRadius = 8;
    okBtn.clipsToBounds = YES;
    [okBtn.layer addSublayer:gl];
    [okBtn setTitle:NSLocalizedString(@"other_confirm",nil) forState:(UIControlStateNormal)];
    [okBtn setTitleColor:[UIColor whiteColor] forState:(UIControlStateNormal)];
    [okBtn.titleLabel setFont:[UIFont systemFontOfSize:16.f]];
    [self.view addSubview:okBtn];
    [okBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        //make.top.equalTo(pwdAgainTextField.mas_bottom).offset(16.f);
        make.top.equalTo(logoSelectView.mas_bottom).offset(60.f);
        make.centerX.equalTo(self.view.mas_centerX);
        make.width.mas_equalTo(okBtn_W);
        make.height.mas_equalTo(48);
    }];
    
    [okBtn addTarget:self
               action:@selector(okBtnPressed)
     forControlEvents:(UIControlEventTouchUpInside)];
    [okBtn setEnabled:NO];
    [okBtn setAlpha:0.5];
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
    if (textField == pwdAgainTextField || textField == pwdTextField)
    {
        /*特定字符限制*/
        NSCharacterSet *cs;
        cs = [[NSCharacterSet characterSetWithCharactersInString:@"0123456789"]invertedSet];
        NSString *filtered = [[string componentsSeparatedByCharactersInSet:cs]componentsJoinedByString:@""];//按cs分离出数组,数组按@""分离出字符串
        
        BOOL canChange = [string isEqualToString:filtered];

        if (logoSelectView.selectIndex <= 0){
            if ([pwdAgainTextField.text isEqualToString:@""]
                || pwdAgainTextField.text == nil
                || [pwdTextField.text isEqualToString:@""]
                || pwdTextField.text == nil)
            {
                [okBtn setEnabled:NO];
                [okBtn setAlpha:0.5];
            }else{
                [okBtn setEnabled:YES];
                [okBtn setAlpha:1];
            }
        }

        return canChange;
    }
    return YES;
}

- (void)okBtnPressed{
//    if (pwdTextField.text.length > 0 || pwdAgainTextField.text.length > 0){
//        if (![pwdTextField.text isEqualToString:pwdAgainTextField.text] || pwdAgainTextField.text.length != 4 ){
//            [[iToast makeText:NSLocalizedString(@"logo_input_pwd_no_same_tips",nil)] show];
//            return;
//        }else{
//            /*设置密码*/
//            NSDictionary *deviceDict = [HWDataManager getObjectWithKey:Const_Have_Add_Device];
//            NSMutableDictionary *dict = [[NSMutableDictionary alloc] initWithDictionary:deviceDict];
//            [dict setObject:pwdAgainTextField.text forKey:Const_Have_Add_Device_PWD];
//            [HWDataManager setObjectWithKey:Const_Have_Add_Device value:dict];
//            
//            [[iToast makeText:NSLocalizedString(@"logo_set_pwd_success",nil)] show];
//        }
//    }
    
   
    
    // 获取Info.plist的路径
//    NSString *plistPath = [[NSBundle mainBundle] pathForResource:@"Info" ofType:@"plist"];
//
//    // 读取Info.plist文件
//    NSMutableDictionary *infoDict = [NSMutableDictionary dictionaryWithContentsOfFile:plistPath];

   
    if (logoSelectView.selectIndex == 1){
        /*设置图标*/
//        [[UIApplication sharedApplication] setAlternateIconName:@"AppIcon 1" completionHandler:^(NSError * _Nullable error) {
//            [HWDataManager setIntegerWithKey:Const_Mask_View_Model value:1];
//        }];
        
        [iTools lc_setAlternateIconName:@"AppIconJSJ"];
        [HWDataManager setIntegerWithKey:Const_Mask_View_Model value:1];
        
        // 修改应用名称
        //[infoDict setObject:NSLocalizedString(@"logo_calculator",nil) forKey:@"CFBundleDisplayName"]; // 或者使用CFBundleName

        
    }else if (logoSelectView.selectIndex == 2){
        /*设置图标*/
//        [[UIApplication sharedApplication] setAlternateIconName:@"AppIcon 2" completionHandler:^(NSError * _Nullable error) {
//            [HWDataManager setIntegerWithKey:Const_Mask_View_Model value:2];
//        }];
        
        [iTools lc_setAlternateIconName:@"AppIconLlq"];
        [HWDataManager setIntegerWithKey:Const_Mask_View_Model value:2];
        
        // 修改应用名称
        //[infoDict setObject:NSLocalizedString(@"logo_bowser",nil) forKey:@"CFBundleDisplayName"]; // 或者使用CFBundleName

    }
    
  
    
    // 保存修改后的Info.plist文件
//    [infoDict writeToFile:plistPath atomically:YES];
//
//    // 刷新应用的名称
//    [[NSBundle mainBundle] load];
//
//    infoDict = [NSMutableDictionary dictionaryWithContentsOfFile:plistPath];
//
//    NSString *DisplayName = [NSBundle mainBundle].infoDictionary[@"CFBundleDisplayName"];
//    HLog(@"CFBundleDisplayName:%@",DisplayName);
    
    
    //提示语
    [[iToast makeText:NSLocalizedString(@"logo_did_change_login",nil)] show];
    
}


- (void)logoSelectViewHaveSelect{
    [okBtn setEnabled:YES];
    [okBtn setAlpha:1];
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    [[UIApplication sharedApplication].keyWindow endEditing:YES];
}

- (NSString *)documentPathForAccount:(NSString *)account fileFolder:(NSString *)fileFolder {
    
    NSString *path = DocumentPath;
    if (account.length != 0) {
        path = [NSString stringWithFormat:@"%@/%@/",DocumentPath,account];
    }
        
    if ([fileFolder containsString:@"/"]) {
        NSString *path1 = [path stringByAppendingPathComponent:fileFolder];
        path1 = [path1 stringByDeletingLastPathComponent];
        NSError *error;
        if (![[NSFileManager defaultManager] fileExistsAtPath:path1]) {
            [[NSFileManager defaultManager] createDirectoryAtPath:path1 withIntermediateDirectories:YES attributes:nil error:&error];
        }
    }else{
        NSError *error;
        if (![[NSFileManager defaultManager] fileExistsAtPath:path]) {
            [[NSFileManager defaultManager] createDirectoryAtPath:path withIntermediateDirectories:YES attributes:nil error:&error];
        }
    }
    
    return [path stringByAppendingPathComponent:fileFolder];
}

@end
