//
//  LNCoinViewController.swift
//  Lanu
//
//  Created by OneeChan on 2025/12/24.
//

import Foundation
import UIKit
import SnapKit


extension UIView {
    func pushToCoinView() {
        let vc = LNCoinViewController()
        navigationController?.pushViewController(vc, animated: true)
    }
}


class LNCoinViewController: LNViewController {
    private let valueLabel = UILabel()
    private let rechargeView = LNPurchaseProductView()
    
    private let rechargeButton = UIButton()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        setupViews()
        
        rechargeView.loadList(.coin)
        onUserWalletInfoChanged(info: myWalletInfo)
        
        LNEventDeliver.addObserver(self)
    }
}

extension LNCoinViewController: LNPurchaseManagerNotify {
    func onUserWalletInfoChanged(info: LNUserWalletInfo) {
        valueLabel.text = info.coin.toDisplay
    }
    
    func onUserPurchaseResult(err: LNPurchaseError?) {
        dismissLoading()
        
        if let err {
            showToast(err.errorDesc)
            return
        }
    }
}

extension LNCoinViewController: 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 LNCoinViewController {
    private func setupViews() {
        navigationBarColor = .primary_1
        view.backgroundColor = .primary_1
        title = .init(key: "A00215")
        
        let bg = buildValueView()
        view.addSubview(bg)
        bg.snp.makeConstraints { make in
            make.horizontalEdges.equalToSuperview().inset(15)
            make.top.equalToSuperview().offset(8)
        }
        
        let set = buildRechargeSet()
        view.addSubview(set)
        set.snp.makeConstraints { make in
            make.horizontalEdges.equalToSuperview()
            make.bottom.equalToSuperview()
            make.top.equalTo(bg.snp.bottom).offset(-31)
        }
    }
    
    private func buildValueView() -> UIView {
        let bg = UIImageView()
        bg.image = .icCoinBg
        bg.isUserInteractionEnabled = true
        
        let titleLabel = UILabel()
        titleLabel.text = .init(key: "A00268")
        titleLabel.font = .heading_h4
        titleLabel.textColor = .text_5
        bg.addSubview(titleLabel)
        titleLabel.snp.makeConstraints { make in
            make.leading.equalToSuperview().offset(30)
            make.top.equalToSuperview().offset(38)
        }
        
        let valueView = UIView()
        bg.addSubview(valueView)
        valueView.snp.makeConstraints { make in
            make.leading.equalToSuperview().offset(30)
            make.top.equalTo(titleLabel.snp.bottom).offset(6)
        }
        
        let coin = UIImageView.coinImageView()
        valueView.addSubview(coin)
        coin.snp.makeConstraints { make in
            make.leading.equalToSuperview()
            make.centerY.equalToSuperview()
            make.width.height.equalTo(24)
        }
        
        valueLabel.text = "0"
        valueLabel.font = .heading_h1
        valueLabel.textColor = .text_5
        valueView.addSubview(valueLabel)
        valueLabel.snp.makeConstraints { make in
            make.verticalEdges.equalToSuperview()
            make.trailing.equalToSuperview()
            make.leading.equalTo(coin.snp.trailing).offset(2)
        }
        
        let jumpButton = UIButton()
        jumpButton.addAction(UIAction(handler: { _ in
            let panel = LNExchangePanel(exchangeType: .coinToDiamond)
            panel.popup()
        }), for: .touchUpInside)
        bg.addSubview(jumpButton)
        jumpButton.snp.makeConstraints { make in
            make.top.equalToSuperview()
            make.trailing.equalToSuperview()
            make.width.equalTo(189)
            make.height.equalTo(30)
        }
        
        let contentView = UIView()
        contentView.isUserInteractionEnabled = false
        jumpButton.addSubview(contentView)
        contentView.snp.makeConstraints { make in
            make.center.equalToSuperview()
        }
        
        let jumpTitleLabel = UILabel()
        jumpTitleLabel.text = .init(key: "A00264")
        jumpTitleLabel.font = .heading_h4
        jumpTitleLabel.textColor = .text_1
        jumpTitleLabel.textAlignment = .center
        contentView.addSubview(jumpTitleLabel)
        jumpTitleLabel.snp.makeConstraints { make in
            make.leading.equalToSuperview()
            make.verticalEdges.equalToSuperview()
        }
        
        let arrow = UIImageView.arrowImageView(size: 14)
        arrow.tintColor = .white
        contentView.addSubview(arrow)
        arrow.snp.makeConstraints { make in
            make.centerY.equalToSuperview()
            make.trailing.equalToSuperview()
            make.leading.equalTo(jumpTitleLabel.snp.trailing).offset(4)
        }
        
        return bg
    }
    
    private func buildRechargeSet() -> UIView {
        let container = UIView()
        container.backgroundColor = .fill
        container.layer.cornerRadius = 20
        container.layer.maskedCorners = [.layerMinXMinYCorner, .layerMaxXMinYCorner]
        
        let scrollView = UIScrollView()
        scrollView.showsVerticalScrollIndicator = false
        scrollView.showsHorizontalScrollIndicator = false
        container.addSubview(scrollView)
        scrollView.snp.makeConstraints { make in
            make.horizontalEdges.equalToSuperview().inset(16)
            make.top.equalToSuperview().offset(20)
            make.bottom.equalToSuperview().offset(-view.safeBottomInset - 10)
        }
        
        let fakeView = UIView()
        scrollView.addSubview(fakeView)
        fakeView.snp.makeConstraints { make in
            make.horizontalEdges.equalToSuperview()
            make.top.width.equalToSuperview()
            make.height.equalTo(0)
        }
        
        rechargeView.delegate = self
        scrollView.addSubview(rechargeView)
        rechargeView.snp.makeConstraints { make in
            make.horizontalEdges.equalToSuperview()
            make.top.equalToSuperview()
        }
        
        rechargeButton.backgroundColor = .text_2
        rechargeButton.layer.cornerRadius = 23.5
        rechargeButton.setBackgroundImage(.primary_8, for: .normal)
        rechargeButton.setTitle(.init(key: "A00269"), for: .normal)
        rechargeButton.setTitleColor(.text_1, for: .normal)
        rechargeButton.titleLabel?.font = .heading_h3
        rechargeButton.clipsToBounds = true
        rechargeButton.addAction(UIAction(handler: { [weak self] _ in
            guard let self else { return }
            guard let goods = rechargeView.curSelected else { return }
//            LNPurchaseManager.shared.purchaseProduct(goods: goods)
            showLoading()
            RechargeManager.shared.startRecharge(goods: goods)
        }), for: .touchUpInside)
        scrollView.addSubview(rechargeButton)
        rechargeButton.snp.makeConstraints { make in
            make.horizontalEdges.equalToSuperview()
            make.top.equalTo(rechargeView.snp.bottom).offset(20)
            make.height.equalTo(47)
        }
        
        let introduce = buildDescView()
        scrollView.addSubview(introduce)
        introduce.snp.makeConstraints { make in
            make.horizontalEdges.equalToSuperview()
            make.top.equalTo(rechargeButton.snp.bottom).offset(20)
            make.bottom.equalToSuperview()
        }
        
        return container
    }
    
    private func buildDescView() -> UIView {
        let container = UIView()
        
        let titleLabel = UILabel()
        titleLabel.text = .init(key: "A00270")
        titleLabel.font = .heading_h5
        titleLabel.textColor = .text_5
        container.addSubview(titleLabel)
        titleLabel.snp.makeConstraints { make in
            make.leading.top.equalToSuperview()
        }
        
        let textView = LNAutoSizeTextView()
        textView.backgroundColor = .clear
        textView.text = .init(key: "A00271")
        textView.font = .body_s
        textView.textColor = .text_4
        textView.linkTextAttributes = [.foregroundColor: UIColor.primary_5]
        textView.isEditable = false
        container.addSubview(textView)
        textView.snp.makeConstraints { make in
            make.horizontalEdges.equalToSuperview().inset(-4)
            make.top.equalTo(titleLabel.snp.bottom).offset(-7)
            make.bottom.equalToSuperview()
        }
        
        return container
    }
}

#if DEBUG

import SwiftUI

struct LNCoinViewControllerPreview: UIViewControllerRepresentable {
    func makeUIViewController(context: Context) -> some UIViewController {
        LNCoinViewController()
    }
    
    func updateUIViewController(_ uiViewController: UIViewControllerType, context: Context) { }
}

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