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

import Foundation
import UIKit
import SnapKit


class LNPurchasePanel: LNPopupView {
    private let productsView = LNPurchaseProductView()
    private let currencyLabel = UILabel()
    private let currencyIc = UIImageView()
    private let rechargeButton = UIButton()
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        setupViews()
        
        LNEventDeliver.addObserver(self)
    }
    
    func update(_ type: LNCurrencyType) {
        switch type {
        case .coin:
            currencyIc.image = .icCoin
            currencyLabel.text = myWalletInfo.coin.toDisplay
        case .diamond:
            currencyIc.image = .icDiamond
            currencyLabel.text = myWalletInfo.diamond.toDisplay
        case .bean:
            currencyIc.image = .icBean
            currencyLabel.text = myWalletInfo.bean.toDisplay
        }
        
        productsView.loadList(type)
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}

extension LNPurchasePanel: LNPurchaseManagerNotify {
    func onUserPurchaseResult(err: LNPurchaseError?) {
        dismissLoading()
        
        if let err {
            showToast(err.errorDesc)
        } else {
            dismiss()
        }
    }
}

extension LNPurchasePanel: LNPurchaseProductViewDelegate {
    func onPurchaseProductView(view: LNPurchaseProductView, didSelect goods: LNPurchaseGoodsVO?) {
        if rechargeButton.isEnabled, goods == nil {
            rechargeButton.isEnabled = false
            rechargeButton.setBackgroundImage(nil, for: .normal)
        } else if !rechargeButton.isEnabled, goods != nil {
            rechargeButton.isEnabled = true
            rechargeButton.setBackgroundImage(.primary_8, for: .normal)
        }
    }
}

extension LNPurchasePanel {
    private func setupViews() {
        container.backgroundColor = .primary_1
        
        let titleLabel = UILabel()
        titleLabel.text = .init(key: "A00279")
        titleLabel.font = .heading_h3
        titleLabel.textColor = .text_5
        container.addSubview(titleLabel)
        titleLabel.snp.makeConstraints { make in
            make.centerX.equalToSuperview()
            make.top.equalToSuperview().offset(14)
        }
        
        let product = buildProducts()
        container.addSubview(product)
        product.snp.makeConstraints { make in
            make.horizontalEdges.equalToSuperview().inset(12)
            make.top.equalTo(titleLabel.snp.bottom).offset(18)
        }
        
        let currency = buildCurrency()
        container.addSubview(currency)
        currency.snp.makeConstraints { make in
            make.horizontalEdges.equalToSuperview().inset(12)
            make.top.equalTo(product.snp.bottom).offset(12)
        }
        
        rechargeButton.setTitle(.init(key: "A00269"), for: .normal)
        rechargeButton.setTitleColor(.text_1, for: .normal)
        rechargeButton.titleLabel?.font = .heading_h3
        rechargeButton.layer.cornerRadius = 23.5
        rechargeButton.clipsToBounds = true
        rechargeButton.backgroundColor = .text_2
        rechargeButton.addAction(UIAction(handler: { [weak self] _ in
            guard let self else { return }
            guard let goods = productsView.curSelected else { return }
            showLoading()
            RechargeManager.shared.startRecharge(goods: goods)
        }), for: .touchUpInside)
        container.addSubview(rechargeButton)
        rechargeButton.snp.makeConstraints { make in
            make.horizontalEdges.equalToSuperview().inset(12)
            make.top.equalTo(currency.snp.bottom).offset(37)
            make.bottom.equalToSuperview().offset(commonBottomInset)
            make.height.equalTo(47)
        }
    }
    
    private func buildProducts() -> UIView {
        let container = UIView()
        container.backgroundColor = .fill
        container.layer.cornerRadius = 12
        
        productsView.delegate = self
        container.addSubview(productsView)
        productsView.snp.makeConstraints { make in
            make.horizontalEdges.equalToSuperview().inset(12)
            make.verticalEdges.equalToSuperview().inset(10)
        }
        
        return container
    }
    
    private func buildCurrency() -> UIView {
        let container = UIView()
        container.backgroundColor = .fill
        container.layer.cornerRadius = 12
        container.snp.makeConstraints { make in
            make.height.equalTo(44)
        }
        
        let ic = UIImageView(image: .icWalletWithBg)
        container.addSubview(ic)
        ic.snp.makeConstraints { make in
            make.leading.equalToSuperview().offset(12)
            make.centerY.equalToSuperview()
            make.width.height.equalTo(24)
        }
        
        let descLabel = UILabel()
        descLabel.text = .init(key: "A00278")
        descLabel.font = .heading_h5
        descLabel.textColor = .text_5
        container.addSubview(descLabel)
        descLabel.snp.makeConstraints { make in
            make.leading.equalTo(ic.snp.trailing).offset(6)
            make.centerY.equalToSuperview()
        }
        
        currencyLabel.font = .body_m
        currencyLabel.textColor = .text_3
        container.addSubview(currencyLabel)
        currencyLabel.snp.makeConstraints { make in
            make.trailing.equalToSuperview().offset(-12)
            make.centerY.equalToSuperview()
        }
        
        container.addSubview(currencyIc)
        currencyIc.snp.makeConstraints { make in
            make.centerY.equalToSuperview()
            make.trailing.equalTo(currencyLabel.snp.leading).offset(-1)
            make.width.height.equalTo(16)
        }
        
        return container
    }
}
