//
//  LNRoomNameInputPanel.swift
//  Gami
//
//  Created by OneeChan on 2026/3/9.
//

import Foundation
import UIKit
import SnapKit


class LNRoomNameInputPanel: LNPopupView {
    private let nameInputField = UITextField()
    
    var handler: ((String) -> Void)?
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        setupViews()
    }
    
    func update(_ name: String?) {
        nameInputField.text = name
    }
    
    override func popup(_ targetView: UIView? = nil, animated: Bool = true) {
        super.popup(targetView, animated: animated)
        
        nameInputField.becomeFirstResponder()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}

extension LNRoomNameInputPanel: 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 <= LNRoomManager.RoomNameMaxInput
    }
    
    func textFieldShouldReturn(_ textField: UITextField) -> Bool {
        textField.resignFirstResponder()
        
        if let currentText = textField.text,
           currentText.count >= LNRoomManager.RoomNameMinInput,
            currentText.count <= LNRoomManager.RoomNameMaxInput {
            dismiss()
            handler?(currentText)
        }
        
        return true
    }
}

extension LNRoomNameInputPanel {
    private func setupViews() {
        let header = buildHeader()
        container.addSubview(header)
        header.snp.makeConstraints { make in
            make.horizontalEdges.equalToSuperview()
            make.top.equalToSuperview()
        }
        
        let input = buildNameView()
        container.addSubview(input)
        input.snp.makeConstraints { make in
            make.horizontalEdges.equalToSuperview().inset(16)
            make.top.equalTo(header.snp.bottom)
        }
        
        let tipsLabel = UILabel()
        tipsLabel.font = .body_s
        tipsLabel.textColor = .text_3
        tipsLabel.text = .init(key: "A00320", LNRoomManager.RoomNameMinInput, LNRoomManager.RoomNameMaxInput)
        container.addSubview(tipsLabel)
        tipsLabel.snp.makeConstraints { make in
            make.trailing.equalToSuperview().offset(-16)
            make.top.equalTo(input.snp.bottom).offset(8)
            make.bottom.equalToSuperview().offset(commonBottomInset)
        }
        
        nameInputField.visibleView = tipsLabel
    }
    
    private func buildHeader() -> UIView {
        let header = UIView()
        header.snp.makeConstraints { make in
            make.height.equalTo(50)
        }
        
        let titleLabel = UILabel()
        titleLabel.font = .heading_h3
        titleLabel.textColor = .text_5
        titleLabel.text = .init(key: "A00315")
        header.addSubview(titleLabel)
        titleLabel.snp.makeConstraints { make in
            make.center.equalToSuperview()
        }
        
        return header
    }
    
    private func buildNameView() -> UIView {
        let container = UIView()
        container.backgroundColor = .fill_1
        container.layer.cornerRadius = 8
        container.snp.makeConstraints { make in
            make.height.equalTo(46)
        }
        
        nameInputField.placeholder = .init(key: "A00316")
        nameInputField.font = .heading_h4
        nameInputField.textColor = .text_5
        nameInputField.returnKeyType = .done
        nameInputField.delegate = self
        nameInputField.clearButtonMode = .whileEditing
        container.addSubview(nameInputField)
        nameInputField.snp.makeConstraints { make in
            make.horizontalEdges.equalToSuperview().inset(12)
            make.centerY.equalToSuperview()
        }
        
        return container
    }
}
