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

import Foundation
import UIKit
import SnapKit


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


class LNWalletViewController: LNViewController {
    private let coinLabel = UILabel()
    private let diamondLabel = UILabel()
    private let beanLabel = UILabel()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        setupViews()
        
        updateWalletInfo()
        LNEventDeliver.addObserver(self)
        LNPurchaseManager.shared.reloadWalletInfo()
    }
}

extension LNWalletViewController: LNPurchaseManagerNotify {
    func onUserWalletInfoChanged(info: LNUserWalletInfo) {
        updateWalletInfo()
    }
}

extension LNWalletViewController {
    private func updateWalletInfo() {
        diamondLabel.text = myWalletInfo.diamond.toDisplay
        coinLabel.text = myWalletInfo.coin.toDisplay
        beanLabel.text = myWalletInfo.bean.toDisplay
    }
    
    private func setupViews() {
        view.backgroundColor = .primary_1
        
        let topCover = UIImageView()
        topCover.image = .icHomeTopBg
        view.addSubview(topCover)
        topCover.snp.makeConstraints { make in
            make.leading.trailing.equalToSuperview()
            make.top.equalTo(fakeNaviBgView)
        }
        
        setupNavBar()
        
        let stackView = UIStackView()
        stackView.axis = .vertical
        stackView.spacing = 16
        view.addSubview(stackView)
        stackView.snp.makeConstraints { make in
            make.horizontalEdges.equalToSuperview().inset(16)
            make.top.equalToSuperview().offset(16)
        }
        
        stackView.addArrangedSubview(buildWallet())
        stackView.addArrangedSubview(buildIncome())
    }
    
    private func setupNavBar() {
        navigationBarColor = .clear
        title = .init(key: "A00215")
        
        let history = UIButton()
        history.setImage(.icWalletHistory, for: .normal)
        history.addAction(UIAction(handler: { [weak self] _ in
            guard let self else { return }
            view.pushToWebView(.init(url: .walletHistoryUrl))
        }), for: .touchUpInside)
        setRightButton(history)
    }
    
    private func buildWallet() -> UIView {
        let container = UIView()
        container.layer.cornerRadius = 12
        container.backgroundColor = .fill
        
        let titleView = UIView()
        container.addSubview(titleView)
        titleView.snp.makeConstraints { make in
            make.horizontalEdges.equalToSuperview()
            make.top.equalToSuperview()
            make.height.equalTo(40)
        }
        
        let titleLabel = UILabel()
        titleLabel.text = .init(key: "A00268")
        titleLabel.font = .heading_h3
        titleLabel.textColor = .text_5
        titleView.addSubview(titleLabel)
        titleLabel.snp.makeConstraints { make in
            make.leading.equalToSuperview().offset(16)
            make.centerY.equalToSuperview()
        }
        
        let coinView = buildCoinView()
        container.addSubview(coinView)
        coinView.snp.makeConstraints { make in
            make.horizontalEdges.equalToSuperview().inset(16)
            make.top.equalTo(titleView.snp.bottom)
            make.height.equalTo(64)
        }
        
        let diamondView = buildDiamondView()
        container.addSubview(diamondView)
        diamondView.snp.makeConstraints { make in
            make.horizontalEdges.equalToSuperview().inset(16)
            make.top.equalTo(coinView.snp.bottom).offset(10)
            make.height.equalTo(64)
            make.bottom.equalToSuperview().offset(-12)
        }
        
        return container
    }
    
    private func buildCoinView() -> UIView {
        let container = UIView()
        container.backgroundColor = .init(hex: "#FFC4000D")
        container.layer.cornerRadius = 12
        container.onTap { [weak self] in
            guard let self else { return }
            view.pushToCoinView()
        }
        
        let coin = UIImageView.coinImageView(true)
        container.addSubview(coin)
        coin.snp.makeConstraints { make in
            make.leading.equalToSuperview().offset(14)
            make.centerY.equalToSuperview()
            make.width.height.equalTo(42)
        }
        
        let textView = UIView()
        container.addSubview(textView)
        textView.snp.makeConstraints { make in
            make.leading.equalTo(coin.snp.trailing).offset(8)
            make.centerY.equalToSuperview()
        }
        
        coinLabel.text = "0"
        coinLabel.font = .heading_h1_5
        coinLabel.textColor = .text_5
        textView.addSubview(coinLabel)
        coinLabel.snp.makeConstraints { make in
            make.leading.top.equalToSuperview()
            make.trailing.equalToSuperview()
        }
        
        let descLabel = UILabel()
        descLabel.text = .init(key: "A00216")
        descLabel.font = .heading_h5
        descLabel.textColor = .text_5
        textView.addSubview(descLabel)
        descLabel.snp.makeConstraints { make in
            make.leading.trailing.bottom.equalToSuperview()
            make.top.equalTo(coinLabel.snp.bottom).offset(-5)
        }
        
        let jumpButton = UIButton()
        jumpButton.setBackgroundImage(.primary_8, for: .normal)
        jumpButton.layer.cornerRadius = 12
        jumpButton.clipsToBounds = true
        jumpButton.addAction(UIAction(handler: { [weak self] _ in
            guard let self else { return }
            view.pushToCoinView()
        }), for: .touchUpInside)
        container.addSubview(jumpButton)
        jumpButton.snp.makeConstraints { make in
            make.centerY.equalToSuperview()
            make.trailing.equalToSuperview().offset(-14)
            make.height.equalTo(24)
        }
        
        let contentView = UIView()
        contentView.isUserInteractionEnabled = false
        jumpButton.addSubview(contentView)
        contentView.snp.makeConstraints { make in
            make.center.equalToSuperview()
            make.leading.greaterThanOrEqualToSuperview().offset(8)
        }
        
        let jumpTitleLabel = UILabel()
        jumpTitleLabel.text = .init(key: "A00279")
        jumpTitleLabel.font = .heading_h5
        jumpTitleLabel.textColor = .text_1
        contentView.addSubview(jumpTitleLabel)
        jumpTitleLabel.snp.makeConstraints { make in
            make.leading.verticalEdges.equalToSuperview()
        }
        
        let arrow = UIImageView.arrowImageView(size: 10)
        arrow.tintColor = .white
        contentView.addSubview(arrow)
        arrow.snp.makeConstraints { make in
            make.leading.equalTo(jumpTitleLabel.snp.trailing).offset(2)
            make.centerY.equalToSuperview()
            make.trailing.equalToSuperview()
        }
        
        return container
    }
    
    private func buildDiamondView() -> UIView {
        let container = UIView()
        container.backgroundColor = .init(hex: "#008FFF0D")
        container.layer.cornerRadius = 12
        container.onTap { [weak self] in
            guard let self else { return }
            view.pushToDiamondView()
        }
        
        let diamond = UIImageView.diamondImageView(true)
        container.addSubview(diamond)
        diamond.snp.makeConstraints { make in
            make.leading.equalToSuperview().offset(14)
            make.centerY.equalToSuperview()
            make.width.height.equalTo(42)
        }
        
        let textView = UIView()
        container.addSubview(textView)
        textView.snp.makeConstraints { make in
            make.leading.equalTo(diamond.snp.trailing).offset(8)
            make.centerY.equalToSuperview()
        }
        
        diamondLabel.text = "0"
        diamondLabel.font = .heading_h1_5
        diamondLabel.textColor = .text_5
        textView.addSubview(diamondLabel)
        diamondLabel.snp.makeConstraints { make in
            make.leading.top.equalToSuperview()
            make.trailing.equalToSuperview()
        }
        
        let descLabel = UILabel()
        descLabel.text = .init(key: "A00217")
        descLabel.font = .heading_h5
        descLabel.textColor = .text_5
        textView.addSubview(descLabel)
        descLabel.snp.makeConstraints { make in
            make.leading.trailing.bottom.equalToSuperview()
            make.top.equalTo(diamondLabel.snp.bottom).offset(-5)
        }
        
        let jumpButton = UIButton()
        jumpButton.setBackgroundImage(.primary_8, for: .normal)
        jumpButton.layer.cornerRadius = 12
        jumpButton.clipsToBounds = true
        jumpButton.addAction(UIAction(handler: { [weak self] _ in
            guard let self else { return }
            view.pushToDiamondView()
        }), for: .touchUpInside)
        container.addSubview(jumpButton)
        jumpButton.snp.makeConstraints { make in
            make.centerY.equalToSuperview()
            make.trailing.equalToSuperview().offset(-14)
            make.height.equalTo(24)
        }
        
        let contentView = UIView()
        contentView.isUserInteractionEnabled = false
        jumpButton.addSubview(contentView)
        contentView.snp.makeConstraints { make in
            make.center.equalToSuperview()
            make.leading.greaterThanOrEqualToSuperview().offset(8)
        }
        
        let jumpTitleLabel = UILabel()
        jumpTitleLabel.text = .init(key: "A00279")
        jumpTitleLabel.font = .heading_h5
        jumpTitleLabel.textColor = .text_1
        contentView.addSubview(jumpTitleLabel)
        jumpTitleLabel.snp.makeConstraints { make in
            make.leading.verticalEdges.equalToSuperview()
        }
        
        let arrow = UIImageView.arrowImageView(size: 10)
        arrow.tintColor = .white
        contentView.addSubview(arrow)
        arrow.snp.makeConstraints { make in
            make.leading.equalTo(jumpTitleLabel.snp.trailing).offset(2)
            make.centerY.equalToSuperview()
            make.trailing.equalToSuperview()
        }
        
        return container
    }
    
    private func buildIncome() -> UIView {
        let container = UIView()
        container.layer.cornerRadius = 12
        container.backgroundColor = .fill
        
        let titleView = UIView()
        container.addSubview(titleView)
        titleView.snp.makeConstraints { make in
            make.horizontalEdges.equalToSuperview()
            make.top.equalToSuperview()
            make.height.equalTo(40)
        }
        
        let titleLabel = UILabel()
        titleLabel.text = .init(key: "A00276")
        titleLabel.font = .heading_h3
        titleLabel.textColor = .text_5
        titleView.addSubview(titleLabel)
        titleLabel.snp.makeConstraints { make in
            make.leading.equalToSuperview().offset(16)
            make.centerY.equalToSuperview()
        }
        
        let beanView = buildBeanView()
        container.addSubview(beanView)
        beanView.snp.makeConstraints { make in
            make.horizontalEdges.equalToSuperview().inset(16)
            make.top.equalTo(titleView.snp.bottom)
            make.height.equalTo(64)
            make.bottom.equalToSuperview().offset(-10)
        }
        
        return container
    }
    
    private func buildBeanView() -> UIView {
        let container = UIView()
        container.backgroundColor = .init(hex: "#FF73000D")
        container.layer.cornerRadius = 12
        container.onTap { [weak self] in
            guard let self else { return }
            view.pushToBeanView()
        }
        
        let bean = UIImageView.beanImageView()
        container.addSubview(bean)
        bean.snp.makeConstraints { make in
            make.leading.equalToSuperview().offset(14)
            make.centerY.equalToSuperview()
            make.width.height.equalTo(42)
        }
        
        let textView = UIView()
        container.addSubview(textView)
        textView.snp.makeConstraints { make in
            make.leading.equalTo(bean.snp.trailing).offset(8)
            make.centerY.equalToSuperview()
        }
        
        beanLabel.text = myWalletInfo.bean.toDisplay
        beanLabel.font = .heading_h1_5
        beanLabel.textColor = .text_5
        textView.addSubview(beanLabel)
        beanLabel.snp.makeConstraints { make in
            make.leading.bottom.equalToSuperview()
            make.trailing.equalToSuperview()
        }
        
        let descLabel = UILabel()
        descLabel.text = .init(key: "A00277")
        descLabel.font = .heading_h5
        descLabel.textColor = .text_5
        textView.addSubview(descLabel)
        descLabel.snp.makeConstraints { make in
            make.leading.trailing.top.equalToSuperview()
            make.bottom.equalTo(beanLabel.snp.top)
        }
        
        let jumpButton = UIButton()
        jumpButton.setBackgroundImage(.primary_8, for: .normal)
        jumpButton.layer.cornerRadius = 12
        jumpButton.clipsToBounds = true
        jumpButton.addAction(UIAction(handler: { [weak self] _ in
            guard let self else { return }
            view.pushToBeanView()
        }), for: .touchUpInside)
        container.addSubview(jumpButton)
        jumpButton.snp.makeConstraints { make in
            make.centerY.equalToSuperview()
            make.trailing.equalToSuperview().offset(-14)
            make.height.equalTo(24)
        }
        
        let contentView = UIView()
        jumpButton.addSubview(contentView)
        contentView.snp.makeConstraints { make in
            make.center.equalToSuperview()
            make.leading.greaterThanOrEqualToSuperview().offset(8)
        }
        
        let jumpTitleLabel = UILabel()
        jumpTitleLabel.text = .init(key: "A00390")
        jumpTitleLabel.font = .heading_h5
        jumpTitleLabel.textColor = .text_1
        contentView.addSubview(jumpTitleLabel)
        jumpTitleLabel.snp.makeConstraints { make in
            make.leading.verticalEdges.equalToSuperview()
        }
        
        let arrow = UIImageView.arrowImageView(size: 10)
        arrow.tintColor = .white
        contentView.addSubview(arrow)
        arrow.snp.makeConstraints { make in
            make.leading.equalTo(jumpTitleLabel.snp.trailing).offset(2)
            make.centerY.equalToSuperview()
            make.trailing.equalToSuperview()
        }
        
        return container
    }
}

#if DEBUG

import SwiftUI

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

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