//
//  LNOrderGenerateQRCodePanel.swift
//  Lanu
//
//  Created by OneeChan on 2025/11/26.
//

import Foundation
import UIKit
import SnapKit


class LNOrderGenerateQRCodePanel: LNPopupView {
    private let curSkillIc = UIImageView()
    private let curSkillNameLabel = UILabel()
    private let skillArrow = UIImageView.arrowImageView(size: 14)
    
    private let tabView = LNOrderQRTabView()
    
    private let showView = LNOrderQRCodeShowView()
    private let customView = LNOrderCustomView()
    
    private var curSkill: LNGameMateSkillVO? {
        didSet {
            if oldValue?.id != curSkill?.id {
                onSkillChanged()
            }
        }
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        setupViews()
        
        DispatchQueue.main.async { [weak self] in
            guard let self else { return }
            tabView.curType = .normal
            curSkill = myUserInfo.skills.first
        }
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}

extension LNOrderGenerateQRCodePanel: LNOrderQRTabViewDelegate {
    func onOrderQRTabView(view: LNOrderQRTabView, didChangedType newType: LNOrderSource) {
        switch newType {
        case .normal:
            showView.isHidden = false
            customView.isHidden = true
        case .custom:
            showView.isHidden = true
            customView.isHidden = false
        }
    }
}

extension LNOrderGenerateQRCodePanel {
    private func onSkillChanged() {
        curSkillIc.sd_setImage(with: URL(string: curSkill?.icon ?? ""))
        curSkillNameLabel.text = curSkill?.name
        
        if let curSkill {
            showView.update(curSkill)
            customView.update(curSkill)
        }
    }
}

extension LNOrderGenerateQRCodePanel {
    private func setupViews() {
        container.backgroundColor = .primary_1
        
        let skill = buildSkillView()
        container.addSubview(skill)
        skill.snp.makeConstraints { make in
            make.horizontalEdges.equalToSuperview()
            make.top.equalToSuperview()
        }
        
        let tab = buildTabView()
        container.addSubview(tab)
        tab.snp.makeConstraints { make in
            make.horizontalEdges.equalToSuperview().inset(16)
            make.top.equalTo(skill.snp.bottom)
        }
        
        container.addSubview(showView)
        showView.snp.makeConstraints { make in
            make.horizontalEdges.equalToSuperview()
            make.top.equalTo(tab.snp.bottom)
            make.bottom.equalToSuperview().offset(-36)
        }
        
        container.addSubview(customView)
        customView.snp.makeConstraints { make in
            make.horizontalEdges.equalToSuperview()
            make.top.equalTo(tab.snp.bottom)
            make.bottom.equalToSuperview().offset(-36)
        }
    }
    
    private func buildSkillView() -> UIView {
        let container = UIView()
        container.snp.makeConstraints { make in
            make.height.equalTo(45)
        }
        
        curSkillIc.layer.cornerRadius = 10
        curSkillIc.clipsToBounds = true
        container.addSubview(curSkillIc)
        curSkillIc.snp.makeConstraints { make in
            make.centerY.equalToSuperview()
            make.leading.equalToSuperview().offset(16)
            make.width.height.equalTo(26)
        }
        
        curSkillNameLabel.font = .heading_h5
        curSkillNameLabel.textColor = .text_5
        container.addSubview(curSkillNameLabel)
        curSkillNameLabel.snp.makeConstraints { make in
            make.centerY.equalToSuperview()
            make.leading.equalTo(curSkillIc.snp.trailing).offset(4)
        }
        
        skillArrow.tintColor = .text_4
        container.addSubview(skillArrow)
        skillArrow.snp.makeConstraints { make in
            make.centerY.equalToSuperview()
            make.leading.equalTo(curSkillNameLabel.snp.trailing).offset(4)
        }
        
        container.onTap { [weak self] in
            guard let self else { return }
            let panel = LNOrderSkillListPanel(curSkillId: curSkill?.id) { [weak self] skill in
                guard let self else { return }
                self.curSkill = skill
            }
            panel.popup()
        }
        
        return container
    }
    
    private func buildTabView() -> UIView {
        tabView.delegate = self
        
        return tabView
    }
}

#if DEBUG

import SwiftUI

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

#Preview(body: {
    LNOrderQRPanelPreview()
})
#endif

