//
//  PwdInPutView.m
//  VclustersGemini
//
//  Created by APPLE on 2023/2/20.
//  Copyright © 2023 APPLE. All rights reserved.
//

#import "PwdInPutView.h"
#import "ForbidMenuTextField.h"
#import "UITextfield+NLLimit.h"
#import "UITextField+SPExtension.h"

@interface PwdInPutView()<UITextFieldDelegate>

@property (strong, nonatomic) ForbidMenuTextField *oneTextField;
@property (strong, nonatomic) ForbidMenuTextField *twoTextField;
@property (strong, nonatomic) ForbidMenuTextField *threeTextField;
@property (strong, nonatomic) ForbidMenuTextField *fourTextField;
@property (copy, nonatomic) NSMutableArray       *textFieldArray;

@end

@implementation PwdInPutView
@synthesize delegate;
@synthesize pwd;

- (id)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    
    if (self){
        [self drawAnyView];
    }
    
    return self;
}

- (void)drawAnyView{
    _oneTextField = [[ForbidMenuTextField alloc] init];
    [self addSubview:_oneTextField];
    [_oneTextField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(0);
        make.centerY.equalTo(self.mas_centerY);
        make.width.mas_equalTo(48);
        make.height.mas_equalTo(48);
    }];
    
    _twoTextField = [[ForbidMenuTextField alloc] init];
    [self addSubview:_twoTextField];
    [_twoTextField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self.mas_centerX).offset(-7.5);
        make.centerY.equalTo(self.mas_centerY);
        make.width.mas_equalTo(48);
        make.height.mas_equalTo(48);
    }];
    
    _threeTextField = [[ForbidMenuTextField alloc] init];
    [self addSubview:_threeTextField];
    [_threeTextField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.mas_centerX).offset(7.5);
        make.centerY.equalTo(self.mas_centerY);
        make.width.mas_equalTo(48);
        make.height.mas_equalTo(48);
    }];
    
    _fourTextField = [[ForbidMenuTextField alloc] init];
    [self addSubview:_fourTextField];
    [_fourTextField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(0);
        make.centerY.equalTo(self.mas_centerY);
        make.width.mas_equalTo(48);
        make.height.mas_equalTo(48);
    }];
    
    [self configTextField];
}



- (void)configTextField {
    [self setEnableTextField:self.oneTextField];
    [self.oneTextField becomeFirstResponder];
    [self setDisableTextField:self.twoTextField];
    [self setDisableTextField:self.threeTextField];
    [self setDisableTextField:self.fourTextField];
    
    [self.oneTextField addTarget:self action:@selector(textFieldChange:) forControlEvents:UIControlEventEditingChanged];
    [self.twoTextField addTarget:self action:@selector(textFieldChange:) forControlEvents:UIControlEventEditingChanged];
    [self.threeTextField addTarget:self action:@selector(textFieldChange:) forControlEvents:UIControlEventEditingChanged];
    [self.fourTextField addTarget:self action:@selector(textFieldChange:) forControlEvents:UIControlEventEditingChanged];
    
    self.oneTextField.delegate = self;
    self.twoTextField.delegate = self;
    self.threeTextField.delegate = self;
    self.fourTextField.delegate = self;

    self.oneTextField.keyboardType = UIKeyboardTypeNumberPad;
    self.twoTextField.keyboardType = UIKeyboardTypeNumberPad;
    self.threeTextField.keyboardType = UIKeyboardTypeNumberPad;
    self.fourTextField.keyboardType = UIKeyboardTypeNumberPad;
    
    [self.oneTextField   setNl_maxLength:1];
    [self.twoTextField   setNl_maxLength:1];
    [self.threeTextField setNl_maxLength:1];
    [self.fourTextField  setNl_maxLength:1];
    
    self.oneTextField.backgroundColor = HWFFFFFFColor10;
    self.twoTextField.backgroundColor = HWFFFFFFColor10;
    self.threeTextField.backgroundColor = HWFFFFFFColor10;
    self.fourTextField.backgroundColor = HWFFFFFFColor10;
    
    self.oneTextField.textColor = HWFFFFFFColor;
    self.twoTextField.textColor = HWFFFFFFColor;
    self.threeTextField.textColor = HWFFFFFFColor;
    self.fourTextField.textColor = HWFFFFFFColor;
    
    [self.oneTextField setFont:[UIFont boldSystemFontOfSize:24.f]];
    [self.twoTextField setFont:[UIFont boldSystemFontOfSize:24.f]];
    [self.threeTextField setFont:[UIFont boldSystemFontOfSize:24.f]];
    [self.fourTextField setFont:[UIFont boldSystemFontOfSize:24.f]];
    
    [self.oneTextField setTextAlignment:(NSTextAlignmentCenter)];
    [self.twoTextField setTextAlignment:(NSTextAlignmentCenter)];
    [self.threeTextField setTextAlignment:(NSTextAlignmentCenter)];
    [self.fourTextField setTextAlignment:(NSTextAlignmentCenter)];
}

// 选中
- (void)setEnableTextField:(UITextField *)textField {
    textField.layer.cornerRadius = 8;
    textField.layer.masksToBounds = YES;
    textField.layer.borderWidth = 1;
    textField.layer.borderColor = HW05BAFBColor.CGColor;
    
    if (textField.text != nil && ![textField.text isEqualToString:@""]) {
        textField.backgroundColor = HW05BAFBColor;
    }
    else{
        textField.backgroundColor = HWFFFFFFColor10;
    }
}

// 取消选中
- (void)setDisableTextField:(UITextField *)textField {
    textField.layer.cornerRadius = 8;
    textField.layer.masksToBounds = YES;
    textField.layer.borderWidth = 1;
    textField.layer.borderColor = HWFFFFFFColor10.CGColor;
//    [textField resignFirstResponder];
    
    if (textField.text != nil && ![textField.text isEqualToString:@""]) {
        textField.backgroundColor = HW05BAFBColor;
    }
    else{
        textField.backgroundColor = HWFFFFFFColor10;
    }
}

// 切换
- (void)setEnableTextField:(UITextField *)newTextField disableTextField:(UITextField *)oldTextField {
    
    [self setDisableTextField:oldTextField];
    
    [oldTextField resignFirstResponder];
    [newTextField becomeFirstResponder];
    [self setEnableTextField:newTextField];
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

#pragma mark - <UITextFieldDelegate> 监听删除按钮点击 限制其他textField响应
- (void)textFieldChange:(UITextField *)textField {
    if (textField.text.length != 0) { // 删除操作
        NSInteger index = [self.textFieldArray indexOfObject:textField];
        
        if (index < self.textFieldArray.count - 1){
            [self setEnableTextField:self.textFieldArray[index+1] disableTextField:textField];
        }
        
        if (textField == self.textFieldArray.lastObject) {
            HLog(@"输入完成");
            /*确认按钮可以点击*/
            [self setEnableTextField:self.oneTextField];
            [self setEnableTextField:self.twoTextField];
            [self setEnableTextField:self.threeTextField];
            [self setEnableTextField:self.fourTextField];
            
            [textField resignFirstResponder];
        }
    }
    
    BOOL isOK = YES;
    for (int i = 0; i < self.textFieldArray.count; i++) {
        UITextField *smsTextField = self.textFieldArray[i];
        
        if (smsTextField.text == nil || [smsTextField.text isEqualToString:@""])
        {
            isOK = NO;
            break;
        }
    }
    
    if ([delegate respondsToSelector:@selector(pwdInPutViewPWDIsOk:)]){
        [delegate pwdInPutViewPWDIsOk:isOK];
    }
}

- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{
//    UITextField *edittextField = self.textFieldArray[self.indexEditor];
//    if ([edittextField isEqual:textField]) {
        return YES;
//    }else {
//        return NO;
//    }
}

- (void)textFieldDidDeleteBackward:(UITextField *)textField {
    HLog(@"删除操作");
    NSInteger index = [self.textFieldArray indexOfObject:textField];
    textField.text = @"";
    if (index > 0){
        [self setEnableTextField:self.textFieldArray[index - 1] disableTextField:self.textFieldArray[index]];
    }else{
        
    }
    
    BOOL isOK = YES;
    for (int i = 0; i < self.textFieldArray.count; i++) {
        UITextField *smsTextField = self.textFieldArray[i];
        
        if (smsTextField.text == nil || [smsTextField.text isEqualToString:@""])
        {
            isOK = NO;
            break;
        }
    }
    
    if ([delegate respondsToSelector:@selector(pwdInPutViewPWDIsOk:)]){
        [delegate pwdInPutViewPWDIsOk:isOK];
    }
}

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
    
    if (string.length == 6) {
        
        NSArray *strARy = [string componentsSeparatedByString:@""];
        for (int i = 0; i < self.textFieldArray.count; i++) {
            
            UITextField *smsTextField = self.textFieldArray[i];
            smsTextField.text = strARy[i];
        }
        return NO;
    }
    
    return YES;
}

- (NSString *)pwd{
    NSString *resStr = self.oneTextField.text;
    resStr = [resStr stringByAppendingString:self.twoTextField.text];
    resStr = [resStr stringByAppendingString:self.threeTextField.text];
    resStr = [resStr stringByAppendingString:self.fourTextField.text];
    return resStr;
}

#pragma mark 懒加载
- (NSMutableArray *)textFieldArray {
    if (!_textFieldArray) {
        _textFieldArray = [NSMutableArray arrayWithObjects:self.oneTextField, self.twoTextField, self.threeTextField, self.fourTextField, nil];
    }
    return _textFieldArray;
}

@end
