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

#import "XVLoginViewController.h"
#import "XVRegisterViewController.h"
#import "XVPasswordViewController.h"
#import "XVInputFieldView.h"

@interface XVLoginViewController ()

@property (nonatomic, strong) UIView *containerView;

@property (nonatomic, strong) UILabel *titleLabel;

@property (nonatomic, strong) UIButton *changeLoginButton;

@property (nonatomic, strong) UIButton *forgetButton;

@property (nonatomic, strong) UIButton *registerButton;

@property (nonatomic, strong) UIButton *loginButton;

@property (nonatomic, strong) YYLabel *protocolLabel;

// 手机号登录
@property (nonatomic, strong) XVInputFieldView *phoneView;

@property (nonatomic, strong) XVInputFieldView *passwordView;

// 验证码登录
@property (nonatomic, strong) XVInputFieldView *phoneCodeView;

@property (nonatomic, strong) XVInputFieldView *smsCodeView;



@end

@implementation XVLoginViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    [self setNavigationBarsetBackgroundColor:COLOR_THEMEGRENN];
    self.view.backgroundColor =  COLOR_THEMEGRENN;
    
    [self setupUI];
}

- (void)setupUI {
    
    [self.view addSubview:self.containerView];
    [self.containerView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.view).mas_offset((120-KTOP_HEIGHT)*heightScale);
        make.left.right.bottom.mas_equalTo(self.view);
    }];
    [self.view layoutIfNeeded];
    [self.containerView addRoundedCorners:UIRectCornerTopLeft | UIRectCornerTopRight withRadii:CGSizeMake(50, 50)];
    
    [self.containerView addSubview:self.titleLabel];
    [self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.containerView).mas_offset(37);
        make.top.mas_equalTo(self.containerView).mas_offset(108*heightScale);
    }];
    
    [self.containerView addSubview:self.changeLoginButton];
    [self.changeLoginButton sizeToFit];
    [self.changeLoginButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(self.containerView).mas_offset(-31);
        make.centerY.mas_equalTo(self.titleLabel);
    }];

    [self.containerView addSubview:self.phoneView];
    [self.phoneView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.titleLabel.mas_bottom).mas_offset(58*heightScale);
        make.left.mas_equalTo(self.containerView).mas_offset(30);
        make.right.mas_equalTo(self.containerView).mas_offset(-30);
        make.height.mas_equalTo(40);
    }];
    
    [self.containerView addSubview:self.passwordView];
    [self.passwordView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.phoneView.mas_bottom).mas_offset(31*heightScale);
        make.left.mas_equalTo(self.containerView).mas_offset(30);
        make.right.mas_equalTo(self.containerView).mas_offset(-30);
        make.height.mas_equalTo(40);
    }];
    
    [self.containerView addSubview:self.phoneCodeView];
    self.phoneCodeView.hidden = YES;
    [self.phoneCodeView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.titleLabel.mas_bottom).mas_offset(58*heightScale);
        make.left.mas_equalTo(self.containerView).mas_offset(30);
        make.right.mas_equalTo(self.containerView).mas_offset(-30);
        make.height.mas_equalTo(40);
    }];
    
    [self.containerView addSubview:self.smsCodeView];
    self.smsCodeView.hidden = YES;
    [self.smsCodeView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.phoneCodeView.mas_bottom).mas_offset(31*heightScale);
        make.left.mas_equalTo(self.containerView).mas_offset(30);
        make.right.mas_equalTo(self.containerView).mas_offset(-30);
        make.height.mas_equalTo(40);
    }];
    
    [self.containerView addSubview:self.forgetButton];
    [self.forgetButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(self.containerView).mas_offset(-31);
        make.top.mas_equalTo(self.passwordView.mas_bottom).mas_offset(17);
    }];
    
    
    [self.containerView addSubview:self.protocolLabel];
    [self.protocolLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.mas_equalTo(self.containerView.mas_safeAreaLayoutGuideBottom).mas_offset(-55*widthScale);
        make.left.right.mas_equalTo(self.containerView);
    }];

    
    [self.containerView addSubview:self.loginButton];
    [self.loginButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.containerView).mas_offset(66);
        make.right.mas_equalTo(self.containerView).mas_offset(-66);
        make.height.mas_equalTo(44);
        make.bottom.mas_equalTo(self.protocolLabel.mas_top).mas_offset(-72*heightScale);
    }];
    kViewBorderRadius(self.loginButton, 10, 0, [UIColor clearColor]);
    
    
    
    [self.containerView addSubview:self.registerButton];
    [self.registerButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(self.containerView);
        make.height.mas_equalTo(22);
        make.bottom.mas_equalTo(self.loginButton.mas_top).mas_offset(-17);
    }];
    [self.registerButton sizeToFit];

    
    
}

- (void)agreeClick:(UIButton *)button {
    button.selected = !button.selected;
}

- (UIView *)containerView {
    if(_containerView == nil) {
        _containerView = [[UIView alloc]init];
        _containerView.backgroundColor = [UIColor whiteColor];

    }
    return _containerView;
}

- (UILabel *)titleLabel {
    if(_titleLabel == nil) {
        _titleLabel = [[UILabel alloc]initWithText:@"登录骑手账号" textColor:F_3D3B4F_COLOR textAlignment:NSTextAlignmentLeft fontSize:24];
    }
    return _titleLabel;
}
- (UIButton *)changeLoginButton {
    if(_changeLoginButton == nil) {
        _changeLoginButton = [[UIButton alloc]initWithTitle:@"验证码登录" fontSize:14 titleColor:BLUE_COLOR normalImageName:@"right" selectedImgName:@"right" backgroundColor:[UIColor whiteColor]];
        [_changeLoginButton setTitle:@"密码登录" forState:UIControlStateSelected];
        [_changeLoginButton setForceRightToLeft:0 forState:UIControlStateNormal];
        WS(weakSelf);
        [_changeLoginButton addBlockForControlEvents:UIControlEventTouchUpInside block:^(UIButton *sender) {
            weakSelf.changeLoginButton.selected = !weakSelf.changeLoginButton.selected;
            weakSelf.phoneView.hidden = !weakSelf.phoneView.hidden;
            weakSelf.passwordView.hidden = !weakSelf.passwordView.hidden;
            weakSelf.phoneCodeView.hidden = !weakSelf.phoneCodeView.hidden;
            weakSelf.smsCodeView.hidden = !weakSelf.smsCodeView.hidden;
        }];
    }
    return _changeLoginButton;
}

- (XVInputFieldView *)phoneView {
    if(_phoneView == nil) {
        _phoneView = [[XVInputFieldView alloc]initWithType:InputTypeText placeholder:@"请输入手机号"];
    }
    return _phoneView;
}
- (XVInputFieldView *)passwordView {
    if(_passwordView == nil) {
        _passwordView = [[XVInputFieldView alloc]initWithType:InputTypePassword placeholder:@"请输入登录密码"];
    }
    return _passwordView;
}

- (XVInputFieldView *)phoneCodeView {
    if(_phoneCodeView == nil) {
        _phoneCodeView = [[XVInputFieldView alloc]initWithType:InputTypeSmsCode placeholder:@"请输入手机号"];
    }
    return _phoneCodeView;
}
- (XVInputFieldView *)smsCodeView {
    if(_smsCodeView == nil) {
        _smsCodeView = [[XVInputFieldView alloc]initWithType:InputTypeNumber placeholder:@"请输入验证码"];
    }
    return _smsCodeView;
}



- (UIButton *)forgetButton {
    if(_forgetButton == nil) {
        _forgetButton = [[UIButton alloc]initWithTitle:@"忘记密码" fontSize:14 titleColor:BLUE_COLOR normalImageName:@"right" selectedImgName:@"right" backgroundColor:[UIColor whiteColor]];
        [_forgetButton setForceRightToLeft:0 forState:UIControlStateNormal];
        WS(weakSelf);
        [_forgetButton addBlockForControlEvents:UIControlEventTouchUpInside block:^(id  _Nonnull sender) {
            XVPasswordViewController *vc = [[XVPasswordViewController alloc]init];
            [weakSelf.navigationController pushViewController:vc animated:YES];
        }];

    }
    return _forgetButton;
}

- (UIButton *)registerButton {
    if(_registerButton == nil) {
        _registerButton = [[UIButton alloc]initWithTitle:@"注册账号" fontSize:14 titleColor:kHexRGB(0x3D3B4F) bgColor:[UIColor whiteColor]];
        WS(weakSelf);
        [_registerButton addBlockForControlEvents:UIControlEventTouchUpInside block:^(id  _Nonnull sender) {
                
            XVRegisterViewController *registerVc = [[XVRegisterViewController alloc]init];
            [weakSelf presentViewController:registerVc animated:YES completion:nil];
        }];
    }
    return _registerButton;
}

- (UIButton *)loginButton {
    if(_loginButton == nil) {
        _loginButton = [[UIButton alloc]initWithTitle:@"登录" fontSize:16 titleColor:[UIColor whiteColor] bgColor:COLOR_THEMEGRENN];
    }
    return _loginButton;
}

- (YYLabel *)protocolLabel {
    if(_protocolLabel == nil) {
        
        _protocolLabel = [[YYLabel alloc]init];
        
        NSMutableAttributedString *text = [NSMutableAttributedString new];
        UIFont *font = [UIFont systemFontOfSize:12];
        UIColor *color = [UIColor colorWithHexString:@"#737272"];

        // UIView attachment
        UIButton *selectButton = [[UIButton alloc]init];
        
        [selectButton setImage:IMG(@"order_check box") forState:UIControlStateNormal];
        [selectButton setImage:IMG(@"order_checkSelect") forState:UIControlStateSelected];
        [selectButton addTarget:self action:@selector(agreeClick:) forControlEvents:UIControlEventTouchUpInside];
        
        [selectButton sizeToFit];
        selectButton.hitEdgeInsets = UIEdgeInsetsMake(-40, -40, -40, -40);
        NSMutableAttributedString *attachment = [NSMutableAttributedString attachmentStringWithContent:selectButton contentMode:UIViewContentModeBottom attachmentSize:selectButton.size alignToFont:font alignment:YYTextVerticalAlignmentCenter];
    
        [text appendAttributedString: attachment];
        
    
        NSMutableAttributedString *str1 = [[NSMutableAttributedString alloc] initWithString:@" 我已阅读并同意"];
        str1.font = font;
        str1.color = color;
        
        [text appendAttributedString:str1];
        
        NSMutableAttributedString *str2 = [[NSMutableAttributedString alloc] initWithString:@"《快服务服务者合作协议》"];
        str2.font = font;
        str2.color = BLUE_COLOR;

        YYTextHighlight *highlight = [YYTextHighlight new];
        
        highlight.tapAction = ^(UIView *containerView, NSAttributedString *text, NSRange range, CGRect rect) {
            
        };
        [str2 setTextHighlight:highlight range:str2.rangeOfAll];
        
        
        [text appendAttributedString:str2];
        
        
        NSMutableAttributedString *str3 = [[NSMutableAttributedString alloc] initWithString:@"及"];
        str3.font = font;
        str3.color = color;
        
        [text appendAttributedString:str3];
        
        NSMutableAttributedString *str4 = [[NSMutableAttributedString alloc] initWithString:@"《隐私协议》"];
        str4.font = font;
        str4.color = BLUE_COLOR;

        YYTextHighlight *highlight1 = [YYTextHighlight new];
        
        highlight1.tapAction = ^(UIView *containerView, NSAttributedString *text, NSRange range, CGRect rect) {
            
        };
        [str4 setTextHighlight:highlight1 range:str4.rangeOfAll];
        
        
        [text appendAttributedString:str4];
        
        _protocolLabel.attributedText = text;
        _protocolLabel.textAlignment = NSTextAlignmentCenter;
        
    }
    return _protocolLabel;
}

@end
