//
//  LNSkillFieldSingleLineEditView.swift
//  Gami
//
//  Created by OneeChan on 2026/1/20.
//

import Foundation
import UIKit
import SnapKit


class LNSkillFieldSingleLineEditView: LNSkillFieldBaseEditView {
    private let countLabel = UILabel()
    private let inputField = UITextField()
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        setupViews()
    }
    
    override func update(_ field: LNSkillEditField) {
        super.update(field)
        
        inputField.text = field.value as? String
        countLabel.isHidden = field.validate.size != nil
        
        inputField.clearButtonMode = field.validate.size != nil ? .always : .never
        inputField.keyboardType = switch field.valueClassType {
        case .string: .default
        case .int: .numberPad
        case .double: .decimalPad
        case .unknown: .default
        }
        needReview = false
    }
    
    required init(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}

extension LNSkillFieldSingleLineEditView: UITextFieldDelegate {
    func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
        guard let size = field?.validate.size else { return true }
        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 <= size.max
    }
    
    func textFieldShouldReturn(_ textField: UITextField) -> Bool {
        textField.resignFirstResponder()
        return true
    }
}

extension LNSkillFieldSingleLineEditView {
    private func setupViews() {
        container.backgroundColor = .fill_2
        container.layer.cornerRadius = 19
        container.snp.makeConstraints { make in
            make.height.equalTo(38)
        }
        
        let stackView = UIStackView()
        stackView.spacing = 16
        stackView.distribution = .fillProportionally
        stackView.alignment = .center
        container.addSubview(stackView)
        stackView.snp.makeConstraints { make in
            make.horizontalEdges.equalToSuperview().inset(16)
            make.verticalEdges.equalToSuperview()
        }
        
        inputField.font = .body_m
        inputField.textColor = .text_5
        inputField.placeholder = .init(key: "A00006")
        inputField.delegate = self
        inputField.returnKeyType = .done
        inputField.addAction(UIAction(handler: { [weak self] _ in
            guard let self else { return }
            guard let field else { return }
            if field.valueClassType == .string,
                let size = field.validate.size {
                countLabel.text = "\(inputField.text?.count ?? 0)/\(size.max)"
            }
            if let text = inputField.text {
                switch field.valueClassType {
                case .string: field.value = text
                case .int: field.value = Int(text) ?? 0
                case .double: field.value = Double(text) ?? 0
                case .unknown: break
                }
            }
            needReview = true
            delegate?.onSkillFieldBaseEditViewInputChanged(view: self)
        }), for: .editingChanged)
        stackView.addArrangedSubview(inputField)
        
        countLabel.font = .body_m
        countLabel.textColor = .text_2
        stackView.addArrangedSubview(countLabel)
    }
}
