//
//  KBChangeNicknamePopView.m
//  keyBoard
//
//  Created by Codex on 2025/11/11.
//

@import UIKit;
#import "KBChangeNicknamePopView.h"
#import <Masonry/Masonry.h>

@interface KBChangeNicknamePopView ()

// 白底圆角卡片容器
@property (nonatomic, strong) UIView *cardView;
// 标题
@property (nonatomic, strong) UILabel *titleLabel;
// 右上角下拉箭头样式的关闭按钮
@property (nonatomic, strong) UIButton *closeButton;
// 输入框外层灰色圆角背景
@property (nonatomic, strong) UIView *fieldBg;
// 输入框
@property (nonatomic, strong) UITextField *textField;
// 保存按钮
@property (nonatomic, strong) UIButton *saveButton;

@end

@implementation KBChangeNicknamePopView

- (instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        self.backgroundColor = UIColor.clearColor; // 半透明背景由 LSTPopView 控制
        [self buildUI];
        [self makeConstraints];
    }
    return self;
}

#pragma mark - Public

- (void)setPrefillNickname:(NSString *)prefillNickname {
    _prefillNickname = [prefillNickname copy];
    self.textField.text = prefillNickname;
}

- (void)focusInput { [self.textField becomeFirstResponder]; }

#pragma mark - Actions

- (void)onTapClose {
    if (self.closeHandler) self.closeHandler();
}

- (void)onTapSave {
    NSString *name = self.textField.text ?: @"";
    // 简单去除首尾空格
    name = [name stringByTrimmingCharactersInSet:NSCharacterSet.whitespaceAndNewlineCharacterSet];
    if (self.saveHandler) self.saveHandler(name);
}

#pragma mark - UI

- (void)buildUI {
    [self addSubview:self.cardView];
    [self.cardView addSubview:self.titleLabel];
    [self.cardView addSubview:self.closeButton];
    [self.cardView addSubview:self.fieldBg];
    [self.fieldBg addSubview:self.textField];
    [self.cardView addSubview:self.saveButton];
}

- (void)makeConstraints {
    [self.cardView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self);
    }];

    [self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.cardView).offset(20);
        make.top.equalTo(self.cardView).offset(18);
    }];

    [self.closeButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self.titleLabel);
        make.right.equalTo(self.cardView).offset(-16);
        make.width.height.mas_equalTo(36);
    }];

    [self.fieldBg mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.cardView).offset(16);
        make.right.equalTo(self.cardView).offset(-16);
        make.top.equalTo(self.titleLabel.mas_bottom).offset(18);
        make.height.mas_equalTo(56);
    }];

    [self.textField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self.fieldBg).insets(UIEdgeInsetsMake(0, 12, 0, 12));
    }];

    [self.saveButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.cardView).offset(16);
        make.right.equalTo(self.cardView).offset(-16);
        make.bottom.equalTo(self.cardView).offset(-18);
        make.height.mas_equalTo(56);
    }];
}

#pragma mark - Lazy UI

- (UIView *)cardView {
    if (!_cardView) {
        _cardView = [UIView new];
        _cardView.backgroundColor = UIColor.whiteColor;
        _cardView.layer.cornerRadius = 18.0;
        _cardView.layer.masksToBounds = YES;
    }
    return _cardView;
}

- (UILabel *)titleLabel {
    if (!_titleLabel) {
        _titleLabel = [UILabel new];
        _titleLabel.text = @"Change The Nickname";
        _titleLabel.textColor = [UIColor blackColor];
        _titleLabel.font = [UIFont systemFontOfSize:22 weight:UIFontWeightBold];
    }
    return _titleLabel;
}

- (UIButton *)closeButton {
    if (!_closeButton) {
        _closeButton = [UIButton buttonWithType:UIButtonTypeCustom];
        _closeButton.backgroundColor = [UIColor colorWithWhite:1 alpha:0.95];
        _closeButton.layer.cornerRadius = 18.0; _closeButton.layer.masksToBounds = YES;
        _closeButton.layer.shadowColor = [UIColor colorWithWhite:0 alpha:0.05].CGColor;
        _closeButton.layer.shadowOpacity = 1.0; _closeButton.layer.shadowRadius = 4.0; _closeButton.layer.shadowOffset = CGSizeMake(0, 1);
        UIImage *img = nil;
        if (@available(iOS 13.0, *)) img = [UIImage systemImageNamed:@"chevron.down"]; // 下拉箭头
        [_closeButton setImage:img forState:UIControlStateNormal];
        if (!img) { [_closeButton setTitle:@"∨" forState:UIControlStateNormal]; }
        [_closeButton setTitleColor:[UIColor colorWithWhite:0.3 alpha:1] forState:UIControlStateNormal];
        _closeButton.tintColor = [UIColor colorWithWhite:0.3 alpha:1];
        [_closeButton addTarget:self action:@selector(onTapClose) forControlEvents:UIControlEventTouchUpInside];
        _closeButton.accessibilityLabel = @"关闭";
    }
    return _closeButton;
}

- (UIView *)fieldBg {
    if (!_fieldBg) {
        _fieldBg = [UIView new];
        _fieldBg.backgroundColor = [UIColor colorWithWhite:0.96 alpha:1.0];
        _fieldBg.layer.cornerRadius = 12.0; _fieldBg.layer.masksToBounds = YES;
    }
    return _fieldBg;
}

- (UITextField *)textField {
    if (!_textField) {
        _textField = [[UITextField alloc] init];
        _textField.clearButtonMode = UITextFieldViewModeWhileEditing;
        _textField.font = [UIFont systemFontOfSize:18 weight:UIFontWeightSemibold];
        _textField.textColor = [UIColor blackColor];
        _textField.placeholder = @"Please Enter The Modified Nickname";
    }
    return _textField;
}

- (UIButton *)saveButton {
    if (!_saveButton) {
        _saveButton = [UIButton buttonWithType:UIButtonTypeCustom];
        [_saveButton setTitle:@"Save" forState:UIControlStateNormal];
        [_saveButton setTitleColor:UIColor.whiteColor forState:UIControlStateNormal];
        _saveButton.titleLabel.font = [UIFont systemFontOfSize:20 weight:UIFontWeightSemibold];
        _saveButton.backgroundColor = [UIColor colorWithRed:0.02 green:0.75 blue:0.67 alpha:1.0];
        _saveButton.layer.cornerRadius = 28.0; _saveButton.layer.masksToBounds = YES; // 胶囊按钮
        [_saveButton addTarget:self action:@selector(onTapSave) forControlEvents:UIControlEventTouchUpInside];
    }
    return _saveButton;
}

@end

