//
//  LNRoomProfileSkillView.swift
//  Gami
//
//  Created by OneeChan on 2026/3/16.
//

import Foundation
import UIKit
import SnapKit


class LNRoomProfileSkillView: UIView {
    private let stackView = UIStackView()
    
    weak var panel: LNPopupView?
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        let scrollView = UIScrollView()
        scrollView.showsHorizontalScrollIndicator = false
        scrollView.alwaysBounceHorizontal = true
        scrollView.contentInset = .init(top: 0, left: 12, bottom: 0, right: 0)
        addSubview(scrollView)
        scrollView.snp.makeConstraints { make in
            make.horizontalEdges.equalToSuperview()
            make.verticalEdges.equalToSuperview().inset(10)
        }
        
        stackView.axis = .horizontal
        stackView.spacing = 6
        scrollView.addSubview(stackView)
        stackView.snp.makeConstraints { make in
            make.edges.equalToSuperview()
            make.height.equalToSuperview()
        }
    }
    
    func update(_ userInfo: LNUserProfileVO, _ skills: [LNGameMateSkillVO]) {
        stackView.arrangedSubviews.forEach {
            stackView.removeArrangedSubview($0)
            $0.removeFromSuperview()
        }

        for (index, skill) in skills.enumerated() {
            let itemView = LNRoomProfileSkillItemView()
            itemView.update(userInfo, skill)
            itemView.onTap { [weak self] in
                guard let self else { return }
                
                panel?.dismiss()
                
                let panel = LNCreateOrderPanel()
                panel.update(skill, user: userInfo)
                panel.editable = true
                panel.scene = .room
                panel.popup(self)
            }
            stackView.addArrangedSubview(itemView)
            if index == 0 {
                LNOrderRoomGuideView.orderCardView = itemView
            }
        }
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}


private class LNRoomProfileSkillItemView: UIView {
    private let iconView = UIImageView()
    private let titleLabel = UILabel()
    private let priceLabel = UILabel()
    private let unitLabel = UILabel()
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        setupViews()
    }
    
    func update(_ user: LNUserProfileVO, _ item: LNGameMateSkillVO) {
        titleLabel.text = item.name
        priceLabel.text = item.price.toDisplay
        unitLabel.text = "/\(item.unit)"
        iconView.sd_setImage(with: URL(string: item.icon))
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}

private extension LNRoomProfileSkillItemView {
    func setupViews() {
        backgroundColor = UIColor.fill.withAlphaComponent(0.1)
        layer.cornerRadius = 12
        clipsToBounds = true
        snp.makeConstraints { make in
            make.width.equalTo(169)
            make.height.equalTo(68)
        }
        
        iconView.backgroundColor = UIColor.fill.withAlphaComponent(0.2)
        iconView.layer.borderWidth = 0.5
        iconView.layer.borderColor = UIColor.fill.withAlphaComponent(0.3).cgColor
        iconView.layer.cornerRadius = 20
        iconView.clipsToBounds = true
        iconView.contentMode = .scaleAspectFill
        addSubview(iconView)
        iconView.snp.makeConstraints { make in
            make.centerY.equalToSuperview()
            make.leading.equalToSuperview().offset(10)
            make.height.width.equalTo(40)
        }
        
        let infoView = UIView()
        addSubview(infoView)
        infoView.snp.makeConstraints { make in
            make.centerY.equalToSuperview()
            make.leading.equalTo(iconView.snp.trailing).offset(5)
            make.trailing.equalToSuperview().offset(-10)
        }
        
        titleLabel.font = .heading_h4
        titleLabel.textColor = .text_1
        titleLabel.lineBreakMode = .byTruncatingTail
        infoView.addSubview(titleLabel)
        titleLabel.snp.makeConstraints { make in
            make.horizontalEdges.equalToSuperview()
            make.top.equalToSuperview()
        }
        
        let priceView = UIView()
        infoView.addSubview(priceView)
        priceView.snp.makeConstraints { make in
            make.horizontalEdges.equalToSuperview()
            make.bottom.equalToSuperview()
            make.top.equalTo(titleLabel.snp.bottom).offset(4)
        }
        
        let coinIconView = UIImageView.coinImageView()
        priceView.addSubview(coinIconView)
        coinIconView.snp.makeConstraints { make in
            make.centerY.equalToSuperview()
            make.leading.equalToSuperview()
        }
        
        priceLabel.font = .heading_h5
        priceLabel.textColor = .text_1
        priceView.addSubview(priceLabel)
        priceLabel.snp.makeConstraints { make in
            make.leading.equalTo(coinIconView.snp.trailing).offset(3)
            make.verticalEdges.equalToSuperview()
        }
        
        unitLabel.font = .body_s
        unitLabel.textColor = .text_2
        priceView.addSubview(unitLabel)
        unitLabel.snp.makeConstraints { make in
            make.leading.equalTo(priceLabel.snp.trailing)
            make.centerY.equalTo(priceLabel)
            make.trailing.lessThanOrEqualToSuperview().offset(-10)
        }
    }
}
