//
//  LNPostSkillSelectPanel.swift
//  Lanu
//
//  Created by OneeChan on 2025/12/16.
//

import Foundation
import UIKit
import SnapKit

class LNPostSkillSelectPanel: LNPopupView {
    private let stackView = UIStackView()
    private let confirmButton = UIButton()
    
    private let maxSelection = 3
    private var curSelecteds: [LNGameMateSkillVO] = []
    
    var handler: (([LNGameMateSkillVO]) -> Void)?
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        setupViews()
    }
    
    func setSkills(skills: [LNGameMateSkillVO], selecteds: [LNGameMateSkillVO]) {
        stackView.arrangedSubviews.forEach {
            stackView.removeArrangedSubview($0)
            $0.removeFromSuperview()
        }
        
        var itemViews: [UIView] = []
        skills.forEach { skill in
            let isSelected = selecteds.contains { $0.id == skill.id }
            let itemView = buildSkillItem(skill, selected: isSelected)
            itemViews.append(itemView)
        }
        for index in (1..<itemViews.count).reversed() {
            itemViews.insert(buildLine(), at: index)
        }
        itemViews.forEach {
            stackView.addArrangedSubview($0)
        }
        curSelecteds = selecteds
        updateConfirmButton()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}

extension LNPostSkillSelectPanel {
    private func updateConfirmButton() {
        if curSelecteds.isEmpty {
            confirmButton.isEnabled = false
            confirmButton.setBackgroundImage(nil, for: .normal)
        } else {
            confirmButton.isEnabled = true
            confirmButton.setBackgroundImage(.primary_8, for: .normal)
        }
    }
    
    private func setupViews() {
        stackView.axis = .vertical
        stackView.spacing = 0
        container.addSubview(stackView)
        stackView.snp.makeConstraints { make in
            make.leading.trailing.equalToSuperview()
            make.top.equalToSuperview().offset(6)
        }
        
        confirmButton.setTitle(.init(key: "A00223"), for: .normal)
        confirmButton.setTitleColor(.text_1, for: .normal)
        confirmButton.titleLabel?.font = .heading_h3
        confirmButton.setBackgroundImage(.primary_8, for: .normal)
        confirmButton.backgroundColor = .text_2
        confirmButton.layer.cornerRadius = 23.5
        confirmButton.clipsToBounds = true
        confirmButton.addAction(UIAction(handler: { [weak self] _ in
            guard let self else { return }
            dismiss()
            handler?(curSelecteds)
        }), for: .touchUpInside)
        container.addSubview(confirmButton)
        confirmButton.snp.makeConstraints { make in
            make.horizontalEdges.equalToSuperview().inset(16)
            make.top.equalTo(stackView.snp.bottom).offset(10)
            make.bottom.equalToSuperview().offset(commonBottomInset)
            make.height.equalTo(47)
        }
    }
    
    private func buildSkillItem(_ skill: LNGameMateSkillVO, selected: Bool) -> UIView {
        let container = UIView()
        container.snp.makeConstraints { make in
            make.height.equalTo(48)
        }
        
        let checkIc = UIImageView()
        checkIc.image = selected ? .icCheck : .icUncheck
        container.addSubview(checkIc)
        checkIc.snp.makeConstraints { make in
            make.centerY.equalToSuperview()
            make.trailing.equalToSuperview().offset(-16)
        }
        
        let skillIc = UIImageView()
        skillIc.layer.cornerRadius = 12
        skillIc.clipsToBounds = true
        skillIc.sd_setImage(with: URL(string: skill.icon))
        container.addSubview(skillIc)
        skillIc.snp.makeConstraints { make in
            make.leading.equalToSuperview().offset(16)
            make.centerY.equalToSuperview()
            make.width.height.equalTo(24)
        }
        
        let titleLabel = UILabel()
        titleLabel.text = skill.name
        titleLabel.font = .heading_h4
        titleLabel.textColor = .text_5
        container.addSubview(titleLabel)
        titleLabel.snp.makeConstraints { make in
            make.leading.equalTo(skillIc.snp.trailing).offset(8)
            make.centerY.equalToSuperview()
            make.trailing.lessThanOrEqualTo(checkIc.snp.leading).offset(-5)
        }
        
        container.onTap { [weak self, weak checkIc] in
            guard let self, let checkIc else { return }
            if curSelecteds.contains(where: { $0.id == skill.id }) {
                curSelecteds.removeAll { $0.id == skill.id }
                checkIc.image = .icUncheck
            } else {
                if maxSelection == curSelecteds.count {
                    showToast(.init(key: "A00224", maxSelection))
                } else {
                    curSelecteds.append(skill)
                    checkIc.image = .icCheck
                }
            }
            updateConfirmButton()
        }
        
        return container
    }
    
    private func buildLine() -> UIView {
        let container = UIView()
        
        let line = UIView()
        line.backgroundColor = .fill_2
        container.addSubview(line)
        line.snp.makeConstraints { make in
            make.verticalEdges.equalToSuperview()
            make.height.equalTo(1)
            make.horizontalEdges.equalToSuperview().inset(16)
        }
        
        return container
    }
}


#if DEBUG

import SwiftUI

struct LNPostSkillSelectPanelPreview: UIViewRepresentable {
    func makeUIView(context: Context) -> some UIView {
        let container = UIView()
        container.backgroundColor = .lightGray
        
        let view = LNPostSkillSelectPanel()
        view.popup(container)
        
        return container
    }
    
    func updateUIView(_ uiView: UIViewType, context: Context) { }
}

#Preview(body: {
    LNPostSkillSelectPanelPreview()
})
#endif // DEBUG
