//
//  LNSkillFieldsEditView.swift
//  Gami
//
//  Created by OneeChan on 2026/1/25.
//

import Foundation
import UIKit
import SnapKit


class LNSkillFieldsEditView: UIView {
    private let stackView = UIStackView()
    
    private let titleLabel = UILabel()
    private var fieldViews: [LNSkillFieldBaseEditView] = []
    
    weak var delegate: LNSkillFieldBaseEditViewDelegate?
    
    var hasAllInput: Bool {
        fieldViews.first { !$0.hasInput() } == nil
    }
    
    var checkAvailable: Bool {
        fieldViews.first { !$0.checkAvailable() } == nil
    }
    
    var needReview: Bool {
        fieldViews.first { $0.needReview } != nil
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        setupViews()
    }
    
    func update(_ name: String, fields: [LNSkillEditField]) {
        stackView.arrangedSubviews.forEach {
            stackView.removeArrangedSubview($0)
            $0.removeFromSuperview()
        }
        
        titleLabel.text = name
        
        for field in fields {
            let view: LNSkillFieldBaseEditView? =
            if field.fieldCode == LNSkillEditStaticFieldCode.fieldUnitPrice.rawValue {
                LNSkillFieldPriceEditView()
            } else {
                switch field.type {
                case .singleLineText, .number:
                    LNSkillFieldSingleLineEditView()
                case .multiLineText:
                    LNSkillFieldMultiLineEditView()
                case .singleSelection, .multiSelection:
                    LNSkillFieldSelectionEditView()
                case .voice:
                    LNSkillFieldVoiceEditView()
                case .photo:
                    LNSkillFieldPhotoEditView()
                default:
                    nil
                }
            }
            if let view {
                view.delegate = self
                view.update(field)
                stackView.addArrangedSubview(view)
                fieldViews.append(view)
            }
        }
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}

extension LNSkillFieldsEditView: LNSkillFieldBaseEditViewDelegate {
    func onSkillFieldBaseEditViewInputChanged(view: LNSkillFieldBaseEditView) {
        delegate?.onSkillFieldBaseEditViewInputChanged(view: view)
    }
}

extension LNSkillFieldsEditView {
    private func setupViews() {
        layer.cornerRadius = 12
        layer.backgroundColor = .fill
        
        let header = buildHeader()
        addSubview(header)
        header.snp.makeConstraints { make in
            make.horizontalEdges.equalToSuperview()
            make.top.equalToSuperview()
        }
        
        stackView.axis = .vertical
        stackView.spacing = 20
        addSubview(stackView)
        stackView.snp.makeConstraints { make in
            make.horizontalEdges.equalToSuperview().inset(16)
            make.top.equalTo(header.snp.bottom).offset(10)
            make.bottom.equalToSuperview().offset(-10)
        }
    }
    
    private func buildHeader() -> UIView {
        let container = UIView()
        container.snp.makeConstraints { make in
            make.height.equalTo(46)
        }
        
        titleLabel.font = .heading_h2
        titleLabel.textColor = .text_5
        container.addSubview(titleLabel)
        titleLabel.snp.makeConstraints { make in
            make.horizontalEdges.equalToSuperview().inset(16)
            make.centerY.equalToSuperview()
        }
        
        let line = UIView()
        line.backgroundColor = .fill_2
        container.addSubview(line)
        line.snp.makeConstraints { make in
            make.horizontalEdges.equalToSuperview().offset(16)
            make.bottom.equalToSuperview()
            make.height.equalTo(1)
        }
        
        return container
    }
}
