//
//  XVInputFieldView.m
//  fastservice
//
//  Created by xuning on 2/3/24.
//  Copyright © 2024 FastService. All rights reserved.
//

#import "XVInputFieldView.h"

@interface XVInputFieldView()<UITextFieldDelegate>

@property (nonatomic, strong) UIView *lineView;

@property (nonatomic,assign) InputType type;


@end

@implementation XVInputFieldView

- (instancetype)initWithType:(InputType)type  placeholder:(NSString *)placeholder {
    if (self = [super init]) {
        self.type = type;
        NSAttributedString *attr = [[NSAttributedString alloc]initWithString:placeholder attributes:@{NSForegroundColorAttributeName : kHexRGB(0x737272),NSFontAttributeName:[UIFont systemFontOfSize:14]}];
        self.textField.attributedPlaceholder = attr;
        
        [self setupUI];
     
    }
    return self;
}

- (void)setupUI {
    
    self.backgroundColor = [UIColor whiteColor];
    
    
    [self addSubview:self.textField];
    [self.textField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(self);
        make.left.mas_equalTo(self).mas_offset(0);
        make.right.mas_equalTo(self).mas_offset(0);
        make.width.mas_lessThanOrEqualTo(SCREEN_WIDTH-60);
        make.height.mas_equalTo(30);
    }];
  
    [self addSubview:self.lineView];
    [self.lineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.mas_equalTo(self);
        make.height.mas_equalTo(1);
        make.left.mas_equalTo(self).mas_offset(0);
        make.right.mas_equalTo(self).mas_offset(0);
    }];
    
    if(self.type == InputTypePassword) {
        
        self.textField.secureTextEntry = YES;
        [self addSubview:self.eyesButton];
        [self.eyesButton sizeToFit];
        [self.eyesButton mas_makeConstraints:^(MASConstraintMaker *make) {
            make.right.mas_equalTo(self).mas_offset(-7);
            make.centerY.mas_equalTo(self.textField);
        }];
        self.voiceLabel.hidden = NO;
        [self addSubview:self.voiceLabel];
        self.voiceLabel.text = @"密码长度6-20位";
        [self.voiceLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.right.mas_equalTo(self.textField);
            make.top.mas_equalTo(self.lineView.mas_bottom).mas_offset(5);
        }];
        
    }
    if(self.type == InputTypeSmsCode) {
        
        [self addSubview:self.smsCodeButton];
        [self.smsCodeButton sizeToFit];
        [self.smsCodeButton mas_makeConstraints:^(MASConstraintMaker *make) {
            make.right.mas_equalTo(self).mas_offset(-7);
            make.centerY.mas_equalTo(self.textField);
            make.height.mas_equalTo(28);
            make.width.mas_equalTo(self.smsCodeButton.width + 10);
        }];
        kViewBorderRadius(self.smsCodeButton, 4, 0, [UIColor clearColor]);
        
        [self addSubview:self.voiceLabel];
        [self.voiceLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.right.mas_equalTo(self.textField);
            make.top.mas_equalTo(self.lineView.mas_bottom).mas_offset(5);
        }];
        
    }
    
    if (self.type == InputTypePicCode) {
        
        
        [self addSubview:self.picCodeButton];
        
        [self.picCodeButton mas_makeConstraints:^(MASConstraintMaker *make) {
            make.right.mas_equalTo(self).mas_offset(-7);
            make.centerY.mas_equalTo(self.textField);
            make.height.mas_equalTo(30);
            make.width.mas_equalTo(70);
        }];

        
        
    }
    
    if(self.type == InputTypeNumber) {
        self.textField.keyboardType = UIKeyboardTypeNumberPad;
    }
    
        
}


#pragma mark - UITextFieldDelegate
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField {
    
    [textField addTarget:self action:@selector(handlerTextFieldEndEdit:) forControlEvents:UIControlEventEditingChanged];
    return YES; // 当前 textField 可以编辑
}

- (void)handlerTextFieldEndEdit:(UITextField *)textField {
    
    if (self.textEditingChanged) {
        self.textEditingChanged(textField.text);
    }
    
}

- (UITextField *)textField {
    if (_textField == nil) {
        _textField = [[UITextField alloc]init];
        [_textField setValue: kHexRGB(0x737272) forKeyPath:@"placeholderLabel.textColor"];
        [_textField setValue:[UIFont systemFontOfSize:14] forKeyPath:@"placeholderLabel.font"];
        _textField.font = [UIFont systemFontOfSize:14.0f];
        _textField.textColor = [UIColor blackColor];
        _textField.delegate = self;
    }
    return _textField;
}

- (UIView *)lineView {
    if (_lineView == nil) {
        _lineView = [[UIView alloc]init];
        _lineView.backgroundColor = BB_3_COLOR;
    }
    return _lineView;
}
- (UIButton *)eyesButton {
    if(_eyesButton == nil) {
        _eyesButton = [[UIButton alloc]initWithNormalImageName:@"closeeyes" selectedImgName:@"openeyes"];
        _eyesButton.hitEdgeInsets = UIEdgeInsetsMake(-10, -10, -10, -10);
        WS(weakSelf);
        [_eyesButton addBlockForControlEvents:UIControlEventTouchUpInside block:^(UIButton *sender) {
            sender.selected = !sender.selected;
            weakSelf.textField.secureTextEntry = !weakSelf.textField.secureTextEntry;
        }];
    }
    return _eyesButton;
}

- (UIButton *)smsCodeButton {
    if(_smsCodeButton == nil) {
        _smsCodeButton = [[UIButton alloc]initWithTitle:@"获取验证码" fontSize:14 titleColor:[UIColor whiteColor] bgColor:BB_3_COLOR];
    }
    return _smsCodeButton;
}

- (UIButton *)picCodeButton {
    if (_picCodeButton == nil) {
        _picCodeButton = [[UIButton alloc]init];
        [_picCodeButton setBackgroundColor:BB_3_COLOR];
    }
    return _picCodeButton;
}

- (UILabel *)voiceLabel {
    if (_voiceLabel == nil) {
        _voiceLabel = [[UILabel alloc]initWithText:@"收不到验证码？来个语音验证码吧" textColor:F_BCBCBC_COLOR textAlignment:(NSTextAlignmentLeft) fontSize:12];
        _voiceLabel.hidden = YES;
    }
    return _voiceLabel;
}
@end
