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

import Foundation
import UIKit
import SnapKit


class LNSkillFieldSelectionEditView: LNSkillFieldBaseEditView {
    private let curValueLabel = UILabel()
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        setupViews()
    }
    
    override func update(_ field: LNSkillEditField) {
        super.update(field)
        
        if case .singleSelection = field.type,
           let text = field.value as? String {
            curValueLabel.text = field.constants.first(where: { $0.key == text })?.value
        } else if case .multiSelection = field.type,
                  let texts = field.value as? [String] {
            let values: [String] = field.constants.compactMap {
                if texts.contains($0.key) {
                    $0.value
                } else {
                    nil
                }
            }
            curValueLabel.text = values.joined(separator: ",")
        }
        if curValueLabel.text?.isEmpty != false {
            curValueLabel.attributedText = .init(string: .init(key: "B00094"), attributes: [
                .font: UIFont.body_m,
                .foregroundColor: UIColor.text_3
            ])
        }
    }
    
    required init(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}

extension LNSkillFieldSelectionEditView {
    private func setupViews() {
        container.backgroundColor = .fill_2
        container.layer.cornerRadius = 19
        container.onTap { [weak self] in
            guard let self else { return }
            guard let field else { return }
            guard !field.constants.isEmpty else {
                showToast(.init(key: "B00098"))
                return
            }
            
            switch field.type {
//            case .date:
//                let panel = LNDatePickerPanel()
//                if let numLimit = field.validate.numLimit {
//                    panel.datePicker.minimumDate = Date(timeIntervalSince1970: Double(numLimit.min / 1_000))
//                    panel.datePicker.maximumDate = Date(timeIntervalSince1970: Double(numLimit.max / 1_000))
//                }
//                panel.handler = { [weak self] date in
//                    guard let self else { return }
//                    curValueLabel.text = date.formattedFullDate("-")
//                    field.value.value = date.formattedFullDate("-", normal: true)
//                }
//                panel.popup()
            case .singleSelection:
                let panel = LNSingleSelectionPanel()
                panel.setTitles(field.fieldName, desc: field.fieldDesc)
                
                let curSelect: String? = if let cur = field.value as? String {
                    field.constants.first(where: { $0.key == cur })?.value
                } else {
                    nil
                }
                panel.update(field.constants.map({ $0.value }), curSelect: curSelect)
                
                panel.handler = { [weak self] index in
                    guard let self else { return }
                    curValueLabel.attributedText = nil
                    curValueLabel.text = field.constants[index].value
                    field.value = field.constants[index].key
                    needReview = field.fieldCode != LNSkillEditStaticFieldCode.fieldUnit.rawValue
                    delegate?.onSkillFieldBaseEditViewInputChanged(view: self)
                }
                panel.popup()
            case .multiSelection:
                let panel = LNMultiSelectionPanel()
                panel.setTitles(field.fieldName, desc: field.fieldDesc)
                
                let curSelect: [String] = if let cur = field.value as? [String] {
                    field.constants.filter({
                        cur.contains($0.key)
                    }).map({ $0.value })
                } else {
                    []
                }
                panel.update(field.constants.map({ $0.value }), curSelected: curSelect)
                
                if let size = field.validate.size {
                    panel.setLimie(min: size.min, max: size.max)
                }
                
                panel.handler = { [weak self] indexs in
                    guard let self else { return }
                    var texts: [String] = []
                    var keys: [String] = []
                    for index in indexs {
                        texts.append(field.constants[index].value)
                        keys.append(field.constants[index].key)
                    }
                    curValueLabel.attributedText = nil
                    curValueLabel.text = texts.joined(separator: ",")
                    field.value = keys
                    needReview = true
                    delegate?.onSkillFieldBaseEditViewInputChanged(view: self)
                }
                panel.popup()
            default: break
            }
        }
        container.snp.makeConstraints { make in
            make.height.equalTo(38)
        }
        
        let arrow = UIImageView.arrowImageView(size: 15)
        arrow.tintColor = .text_3
        container.addSubview(arrow)
        arrow.snp.makeConstraints { make in
            make.centerY.equalToSuperview()
            make.trailing.equalToSuperview().offset(-16)
        }
        
        curValueLabel.font = .heading_h4
        curValueLabel.textColor = .text_5
        container.addSubview(curValueLabel)
        curValueLabel.snp.makeConstraints { make in
            make.leading.equalToSuperview().offset(16)
            make.centerY.equalToSuperview()
        }
    }
}
