//
//  LNEditNickNamePanel.swift
//  Lanu
//
//  Created by OneeChan on 2025/12/19.
//

import Foundation
import UIKit
import SnapKit


class LNEditNickNamePanel: LNPopupView {
    let inputField = UITextField()
    private let confirmButton = UIButton()
    
    var handler: ((String) -> Void)?
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        setupViews()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}

extension LNEditNickNamePanel: UITextFieldDelegate {
    func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
        let currentText = textField.text ?? ""
                
        guard let range = Range(range, in: currentText) else { return false }
        let newText = currentText.replacingCharacters(in: range, with: string)
        if newText.count < currentText.count {
            return true
        }
        
        return newText.count <= LNProfileManager.nameMaxInput
    }
    
    func textFieldShouldReturn(_ textField: UITextField) -> Bool {
        textField.resignFirstResponder()
        
        return true
    }
}

extension LNEditNickNamePanel {
    private func setupViews() {
        let titleLabel = UILabel()
        titleLabel.font = .heading_h3
        titleLabel.textColor = .text_5
        titleLabel.text = .init(key: "A00188")
        container.addSubview(titleLabel)
        titleLabel.snp.makeConstraints { make in
            make.centerX.equalToSuperview()
            make.top.equalToSuperview().offset(16)
        }
        
        let inputHolder = UIView()
        container.addSubview(inputHolder)
        inputHolder.snp.makeConstraints { make in
            make.top.equalTo(titleLabel.snp.bottom).offset(16)
            make.horizontalEdges.equalToSuperview().inset(16)
        }
        
        let holder = UIView()
        holder.backgroundColor = .fill_1
        holder.layer.cornerRadius = 8
        inputHolder.addSubview(holder)
        holder.snp.makeConstraints { make in
            make.horizontalEdges.equalToSuperview()
            make.top.equalToSuperview()
            make.height.equalTo(46)
        }
        
        inputField.font = .body_m
        inputField.textColor = .text_5
        inputField.clearButtonMode = .whileEditing
        inputField.delegate = self
        inputField.returnKeyType = .done
        inputField.visibleView = inputHolder
        inputField.addAction(UIAction(handler: { [weak self] _ in
            guard let self else { return }
            let text = inputField.text
            confirmButton.isEnabled = text?.isEmpty == false
            if confirmButton.isEnabled, confirmButton.backgroundImage(for: .normal) == nil {
                confirmButton.setBackgroundImage(.primary_8, for: .normal)
            } else if !confirmButton.isEnabled, confirmButton.backgroundImage(for: .normal) != nil {
                confirmButton.setBackgroundImage(nil, for: .normal)
            }
        }), for: .editingChanged)
        holder.addSubview(inputField)
        inputField.snp.makeConstraints { make in
            make.horizontalEdges.equalToSuperview().inset(12)
            make.centerY.equalToSuperview()
        }
        
        let tipsLabel = UILabel()
        tipsLabel.font = .body_s
        tipsLabel.text = .init(key: "A00189", LNProfileManager.nameMaxInput)
        tipsLabel.textColor = .text_3
        inputHolder.addSubview(tipsLabel)
        tipsLabel.snp.makeConstraints { make in
            make.trailing.equalTo(holder)
            make.top.equalTo(holder.snp.bottom).offset(8)
            make.bottom.equalToSuperview()
        }
        
        confirmButton.setTitle(.init(key: "A00185"), for: .normal)
        confirmButton.setTitleColor(.text_1, for: .normal)
        confirmButton.titleLabel?.font = .heading_h3
        confirmButton.setBackgroundImage(.primary_8, for: .normal)
        confirmButton.layer.cornerRadius = 23.5
        confirmButton.clipsToBounds = true
        confirmButton.backgroundColor = .fill_4
        confirmButton.addAction(UIAction(handler: { [weak self] _ in
            guard let self else { return }
            guard let text = inputField.text else { return }
            dismiss()
            handler?(text)
        }), for: .touchUpInside)
        container.addSubview(confirmButton)
        confirmButton.snp.makeConstraints { make in
            make.horizontalEdges.equalToSuperview().inset(12)
            make.top.equalTo(inputHolder.snp.bottom).offset(16)
            make.bottom.equalToSuperview().offset(commonBottomInset)
            make.height.equalTo(47)
        }
    }
}
