//
//  LNPurchaseProductView.swift
//  Lanu
//
//  Created by OneeChan on 2025/12/30.
//

import Foundation
import UIKit
import SnapKit


protocol LNPurchaseProductViewDelegate: AnyObject {
    func onPurchaseProductView(view: LNPurchaseProductView, didSelect goods: LNPurchaseGoodsVO?)
}


class LNPurchaseProductView: UIView {
    private let productsView = LNMultiLineStackView()
    private var itemViews: [LNPurchaseItemView] = []
    var curSelected: LNPurchaseGoodsVO? {
        itemViews.first { $0.isSelected }?.goods
    }
    
    weak var delegate: LNPurchaseProductViewDelegate?
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        setupViews()
    }
    
    func loadList(_ type: LNCurrencyType) {
        LNPurchaseManager.shared.loadGoodsList(currencyType: type) { [weak self] list in
            guard let self else { return }
            guard let list else { return }
            
            itemViews.removeAll()
            list.forEach {
                let itemView = LNPurchaseItemView()
                itemView.currencyIc.image = switch type {
                case .coin: .icCoin
                case .diamond: .icDiamond
                case .bean: .icBean
                }
                itemView.valueLabel.text = "\($0.coinRechargeAmount.currencyDisplay)"
                itemView.moneyLabel.text = "\($0.currency) \($0.amount.cashDisplay)"
                itemView.goods = $0
                itemView.onTap { [weak self, weak itemView] in
                    guard let self, let itemView else { return }
                    updateSelection(newSelection: itemView)
                }
                
                self.itemViews.append(itemView)
            }
            productsView.update(itemViews)
            
            updateSelection(newSelection: nil)
        }
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}

extension LNPurchaseProductView {
    private func updateSelection(newSelection: LNPurchaseItemView?) {
        itemViews.forEach {
            $0.isSelected = newSelection == $0
        }
        delegate?.onPurchaseProductView(view: self, didSelect: newSelection?.goods)
    }
    
    private func setupViews() {
        productsView.columns = 3
        productsView.itemSpacing = 10
        productsView.itemDistribution = .fillEqually
        productsView.spacing = 10
        addSubview(productsView)
        productsView.snp.makeConstraints { make in
            make.edges.equalToSuperview()
        }
    }
}


private class LNPurchaseItemView: UIView {
    let currencyIc = UIImageView()
    let valueLabel = UILabel()
    let moneyLabel = UILabel()
    var goods: LNPurchaseGoodsVO?
    
    var isSelected: Bool = false {
        didSet {
            bg.isHidden = !isSelected
        }
    }
    
    private let bg = UIImageView()
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        backgroundColor = .fill_1
        layer.cornerRadius = 12
        clipsToBounds = true
        snp.makeConstraints { make in
            make.height.equalTo(76)
        }
        
        bg.image = .primary_7
        addSubview(bg)
        bg.snp.makeConstraints { make in
            make.edges.equalToSuperview()
        }
        
        let cover = UIView()
        cover.backgroundColor = .fill_1
        cover.layer.cornerRadius = 11
        bg.addSubview(cover)
        cover.snp.makeConstraints { make in
            make.edges.equalToSuperview().inset(1)
        }
        
        let container = UIView()
        addSubview(container)
        container.snp.makeConstraints { make in
            make.center.equalToSuperview()
        }
        
        let coinView = UIView()
        container.addSubview(coinView)
        coinView.snp.makeConstraints { make in
            make.centerX.top.equalToSuperview()
        }
        
        coinView.addSubview(currencyIc)
        currencyIc.snp.makeConstraints { make in
            make.leading.centerY.equalToSuperview()
            make.width.height.equalTo(18)
        }
        
        valueLabel.font = .heading_h2
        valueLabel.textColor = .text_5
        coinView.addSubview(valueLabel)
        valueLabel.snp.makeConstraints { make in
            make.verticalEdges.equalToSuperview()
            make.trailing.equalToSuperview()
            make.leading.equalTo(currencyIc.snp.trailing).offset(2)
        }
        
        moneyLabel.font = .body_s
        moneyLabel.textColor = .text_4
        container.addSubview(moneyLabel)
        moneyLabel.snp.makeConstraints { make in
            make.horizontalEdges.equalToSuperview()
            make.top.equalTo(coinView.snp.bottom).offset(4)
            make.bottom.equalToSuperview()
        }
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}
