//
//  LNOrderSkillListPanel.swift
//  Lanu
//
//  Created by OneeChan on 2025/11/27.
//

import Foundation
import UIKit
import SnapKit

class LNOrderSkillListPanel: LNPopupView {
    private let curSkillId: String?
    private var handler: ((LNGameMateSkillVO) -> Void)?
    
    init(curSkillId: String?, handler: @escaping (LNGameMateSkillVO) -> Void) {
        self.curSkillId = curSkillId
        self.handler = handler
        
        super.init(frame: .zero)
        
        setupViews()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}

extension LNOrderSkillListPanel {
    private func setupViews() {
        let stackView = UIStackView()
        stackView.axis = .vertical
        stackView.spacing = 0
        container.addSubview(stackView)
        stackView.snp.makeConstraints { make in
            make.leading.trailing.equalToSuperview()
            make.top.equalToSuperview().offset(6)
            make.bottom.equalToSuperview().offset(-safeBottomInset - 5)
        }
        
        var itemViews: [UIView] = []
        myUserInfo.skills.forEach {
            itemViews.append(buildSkillItem($0))
        }
        itemViews.forEach {
            stackView.addArrangedSubview($0)
        }
    }
    
    private func buildSkillItem(_ skill: LNGameMateSkillVO) -> UIView {
        let container = UIView()
        
        let checkIc = UIImageView()
        checkIc.image = skill.id == curSkillId ? .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.top.equalToSuperview().offset(12)
            make.bottom.equalToSuperview().offset(-12)
            make.trailing.lessThanOrEqualTo(checkIc.snp.leading).offset(-5)
        }
        
        container.onTap { [weak self] in
            guard let self else { return }
            self.handler?(skill)
            self.dismiss()
        }
        
        return container
    }
}


#if DEBUG

import SwiftUI

struct LNOrderSkillListPanelPreview: UIViewRepresentable {
    func makeUIView(context: Context) -> some UIView {
        let container = UIView()
        container.backgroundColor = .lightGray
        
        let view = LNOrderSkillListPanel(curSkillId: "") { skill in
            
        }
        view.popup(container)
        
        return container
    }
    
    func updateUIView(_ uiView: UIViewType, context: Context) { }
}

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