//
//  PasswordInputField.swift
//  suixinkan
//

import SnapKit
import UIKit

/// 登录页密码输入框，对应 Android `PasswordInputField`。
final class PasswordInputField: UIView {

    private let textField = UITextField()
    private let toggleButton = UIButton(type: .custom)

    private var isPasswordVisible = false

    var text: String {
        get { textField.text ?? "" }
        set { textField.text = newValue }
    }

    var onTextChange: ((String) -> Void)?
    var onReturnKey: (() -> Void)?

    init(placeholder: String = "请输入密码") {
        super.init(frame: .zero)
        setupUI(placeholder: placeholder)
        setupConstraints()
    }

    @available(*, unavailable)
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    @discardableResult
    override func becomeFirstResponder() -> Bool {
        textField.becomeFirstResponder()
    }

    private func setupUI(placeholder: String) {
        backgroundColor = AppColor.inputBackground
        layer.cornerRadius = 12
        clipsToBounds = true

        textField.font = .systemFont(ofSize: 14)
        textField.textColor = AppColor.text333
        textField.isSecureTextEntry = true
        textField.autocapitalizationType = .none
        textField.autocorrectionType = .no
        textField.returnKeyType = .done
        textField.delegate = self

        let attributes: [NSAttributedString.Key: Any] = [
            .foregroundColor: AppColor.placeholder,
            .font: UIFont.systemFont(ofSize: 14),
        ]
        textField.attributedPlaceholder = NSAttributedString(string: placeholder, attributes: attributes)
        textField.addTarget(self, action: #selector(textDidChange), for: .editingChanged)

        toggleButton.setImage(UIImage(named: "icon_pwd_invisible"), for: .normal)
        toggleButton.addTarget(self, action: #selector(togglePasswordVisibility), for: .touchUpInside)

        addSubview(textField)
        addSubview(toggleButton)
    }

    private func setupConstraints() {
        snp.makeConstraints { make in
            make.height.equalTo(46)
        }

        toggleButton.snp.makeConstraints { make in
            make.trailing.equalToSuperview().inset(8)
            make.centerY.equalToSuperview()
            make.size.equalTo(30)
        }

        textField.snp.makeConstraints { make in
            make.leading.equalToSuperview().inset(16)
            make.trailing.equalTo(toggleButton.snp.leading).offset(-4)
            make.centerY.equalToSuperview()
        }
    }

    @objc private func textDidChange() {
        onTextChange?(text)
    }

    @objc private func togglePasswordVisibility() {
        isPasswordVisible.toggle()
        textField.isSecureTextEntry = !isPasswordVisible
        let imageName = isPasswordVisible ? "icon_pwd_visible" : "icon_pwd_invisible"
        toggleButton.setImage(UIImage(named: imageName), for: .normal)
    }
}

extension PasswordInputField: UITextFieldDelegate {

    func textFieldShouldReturn(_ textField: UITextField) -> Bool {
        onReturnKey?()
        return true
    }
}
