//
//  LNIMChatUserRemarkPanel.swift
//  Gami
//
//  Created by OneeChan on 2026/2/8.
//

import Foundation
import UIKit
import SnapKit


class LNIMChatUserRemarkPanel: LNPopupView {
    private let countLabel = UILabel()
    private let inputField = UITextField()
    private let confirmButton = UIButton()
    
    var handler: ((String) -> Void)?
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        setupViews()
    }
    
    func update(_ remark: String) {
        inputField.text = remark
        updateCount()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}

extension LNIMChatUserRemarkPanel: 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 <= LNIMManager.maxRemarkLength
    }
    
    func textFieldShouldReturn(_ textField: UITextField) -> Bool {
        textField.resignFirstResponder()
        
        return true
    }
}

extension LNIMChatUserRemarkPanel {
    private func updateCount() {
        let count = inputField.text?.count ?? 0
        countLabel.text = "\(count)/\(LNIMManager.maxRemarkLength)"
    }
    
    private func setupViews() {
        let header = buildHeader()
        container.addSubview(header)
        header.snp.makeConstraints { make in
            make.horizontalEdges.equalToSuperview()
            make.top.equalToSuperview()
        }
        
        let tipsLabel = UILabel()
        tipsLabel.font = .heading_h4
        tipsLabel.text = .init(key: "A00290")
        tipsLabel.textColor = .text_5
        container.addSubview(tipsLabel)
        tipsLabel.snp.makeConstraints { make in
            make.leading.equalToSuperview().offset(16)
            make.top.equalTo(header.snp.bottom)
        }
        
        updateCount()
        countLabel.font = .body_m
        countLabel.textColor = .text_5
        container.addSubview(countLabel)
        countLabel.snp.makeConstraints { make in
            make.trailing.equalToSuperview().offset(-16)
            make.centerY.equalTo(tipsLabel)
        }
        
        let inputHolder = UIView()
        inputHolder.backgroundColor = .fill_1
        inputHolder.layer.cornerRadius = 8
        container.addSubview(inputHolder)
        inputHolder.snp.makeConstraints { make in
            make.top.equalTo(tipsLabel.snp.bottom).offset(8)
            make.horizontalEdges.equalToSuperview().inset(16)
            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.placeholder = .init(key: "A00006")
        inputField.addAction(UIAction(handler: { [weak self] _ in
            guard let self else { return }
            updateCount()
        }), for: .editingChanged)
        inputHolder.addSubview(inputField)
        inputField.snp.makeConstraints { make in
            make.horizontalEdges.equalToSuperview().inset(12)
            make.centerY.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(66)
            make.bottom.equalToSuperview().offset(commonBottomInset)
            make.height.equalTo(47)
        }
    }
    
    private func buildHeader() -> UIView {
        let container = UIView()
        container.snp.makeConstraints { make in
            make.height.equalTo(50)
        }
        
        let titleLabel = UILabel()
        titleLabel.font = .heading_h3
        titleLabel.textColor = .text_5
        titleLabel.text = .init(key: "A00086")
        container.addSubview(titleLabel)
        titleLabel.snp.makeConstraints { make in
            make.center.equalToSuperview()
        }
        
        return container
    }
}
