//
//  LNCaptchaInputView.swift
//  Gami
//
//  Created by OneeChan on 2026/1/16.
//

import Foundation
import UIKit
import SnapKit


protocol LNCaptchaInputViewDelegate: AnyObject {
    func onCaptchaInputChange(view: LNCaptchaInputView)
}


class LNCaptchaInputView: UIView {
    private let captchaCount: Int = 4
    private let stackView = UIStackView()
    private(set) var inputViews: [UITextField] = []
    
    weak var delegate: LNCaptchaInputViewDelegate?
    
    var hasDone: Bool {
        inputViews.first { $0.text?.isEmpty != false } == nil
    }
    var curInput: String {
        inputViews.map { $0.text ?? "" }.joined()
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        setupViews()
        buildCaptchaInput()
    }
    
    func startInput() {
        inputViews.first?.becomeFirstResponder()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}

extension LNCaptchaInputView: UITextFieldDelegate, LNTextFieldDelegate {
    func textFieldDidBeginEditing(_ textField: UITextField) {
        textField.text = nil
    }
    
    func onDeleteBackward(_ textField: UITextField, oldText: String?) {
        if let oldText, !oldText.isEmpty { return }
        guard let index = inputViews.firstIndex(of: textField) else { return }
        if index == 0 { return }
        
        inputViews[index - 1].text = nil
        inputViews[index - 1].becomeFirstResponder()
    }
}

extension LNCaptchaInputView {
    private func setupViews() {
        stackView.axis = .horizontal
        stackView.distribution = .equalCentering
        addSubview(stackView)
        stackView.snp.makeConstraints { make in
            make.edges.equalToSuperview()
        }
    }
    
    private func buildCaptchaInput() {
        let allViews = stackView.arrangedSubviews
        allViews.forEach {
            stackView.removeArrangedSubview($0)
            $0.removeFromSuperview()
        }
        inputViews.removeAll()
        
        for _ in 0..<captchaCount {
            let container = UIView()
            container.layer.cornerRadius = 10
            container.backgroundColor = .fill
            container.snp.makeConstraints { make in
                make.width.equalTo(64)
            }
            
            let input = LNTextField()
            input.delegate = self
            input.exDelegate = self
            input.font = .systemFont(ofSize: 32, weight: .semibold)
            input.textColor = .text_5
            input.keyboardType = .numberPad
            input.textAlignment = .center
            input.cursorWidth = 2
            input.cursorHeight = 24
            input.addAction(UIAction(handler: { [weak self, weak input] _ in
                guard let self, let input else { return }
                
                guard let text = input.text, !text.isEmpty else { return }
                input.text = "\(Int(text.prefix(1)) ?? 0)"
                guard let index = inputViews.firstIndex(of: input) else { return }
                if index < inputViews.count - 1 {
                    inputViews[index + 1].text = nil
                    inputViews[index + 1].becomeFirstResponder()
                } else {
                    input.resignFirstResponder()
                }
                delegate?.onCaptchaInputChange(view: self)
            }), for: .editingChanged)
            container.addSubview(input)
            input.snp.makeConstraints { make in
                make.horizontalEdges.equalToSuperview()
                make.centerY.equalToSuperview()
            }
            
            stackView.addArrangedSubview(container)
            inputViews.append(input)
        }
    }
}
