//
//  LNMineWalletInfoView.swift
//  Lanu
//
//  Created by OneeChan on 2025/12/19.
//

import Foundation
import UIKit
import SnapKit


class LNMineWalletInfoView: UIView {
    private let coinCountLabel = UILabel()
    private let diamondCountLabel = UILabel()
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        setupViews()
        
        LNEventDeliver.addObserver(self)
        
        onUserWalletInfoChanged(info: myWalletInfo)
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}

extension LNMineWalletInfoView: LNPurchaseManagerNotify {
    func onUserWalletInfoChanged(info: LNUserWalletInfo) {
        coinCountLabel.text = info.coin.toDisplay
        diamondCountLabel.text = info.diamond.toDisplay
    }
}

extension LNMineWalletInfoView {
    private func setupViews() {
        layer.cornerRadius = 12
        backgroundColor = .fill
        
        onTap { [weak self] in
            guard let self else { return }
            pushToWallet()
        }
        
        let titleLabel = UILabel()
        titleLabel.font = .heading_h3
        titleLabel.textColor = .text_5
        titleLabel.text = .init(key: "A00215")
        addSubview(titleLabel)
        titleLabel.snp.makeConstraints { make in
            make.leading.equalToSuperview().offset(16)
            make.top.equalToSuperview().offset(12)
        }
        
        let coinView = UIView()
        coinView.backgroundColor = .init(hex: "#FFC4000D")
        coinView.layer.cornerRadius = 12
        addSubview(coinView)
        coinView.snp.makeConstraints { make in
            make.leading.equalToSuperview().offset(16)
            make.top.equalTo(titleLabel.snp.bottom).offset(12)
            make.bottom.equalToSuperview().offset(-12)
        }
        
        let coinIc = UIImageView()
        coinIc.image = .icCoin
        coinIc.alpha = 0.2
        coinView.addSubview(coinIc)
        coinIc.snp.makeConstraints { make in
            make.bottom.trailing.equalToSuperview()
            make.top.equalToSuperview().offset(14)
            make.width.height.equalTo(50)
        }
        
        let coinTextView = UIView()
        coinView.addSubview(coinTextView)
        coinTextView.snp.makeConstraints { make in
            make.centerY.equalToSuperview()
            make.leading.equalToSuperview().offset(12)
        }
        
        coinCountLabel.text = "0"
        coinCountLabel.font = .heading_h2
        coinCountLabel.textColor = .text_5
        coinTextView.addSubview(coinCountLabel)
        coinCountLabel.snp.makeConstraints { make in
            make.leading.equalToSuperview()
            make.top.equalToSuperview()
            make.trailing.equalToSuperview()
        }
        
        let coinLabel = UILabel()
        coinLabel.text = .init(key: "A00216")
        coinLabel.font = .heading_h5
        coinLabel.textColor = .text_5
        coinTextView.addSubview(coinLabel)
        coinLabel.snp.makeConstraints { make in
            make.leading.equalToSuperview()
            make.top.equalTo(coinCountLabel.snp.bottom)
            make.bottom.equalToSuperview()
            make.trailing.lessThanOrEqualToSuperview()
        }
        
        let diamondView = UIView()
        diamondView.backgroundColor = .init(hex: "#008FFF0D")
        diamondView.layer.cornerRadius = 12
        addSubview(diamondView)
        diamondView.snp.makeConstraints { make in
            make.leading.equalTo(coinView.snp.trailing).offset(10)
            make.size.equalTo(coinView)
            make.centerY.equalTo(coinView)
            make.trailing.equalToSuperview().offset(-16)
        }
        
        let diamondIc = UIImageView()
        diamondIc.image = .icDiamond
        diamondIc.alpha = 0.2
        diamondView.addSubview(diamondIc)
        diamondIc.snp.makeConstraints { make in
            make.bottom.trailing.equalToSuperview()
            make.width.height.equalTo(50)
        }
        
        let diamondTextView = UIView()
        diamondView.addSubview(diamondTextView)
        diamondTextView.snp.makeConstraints { make in
            make.centerY.equalToSuperview()
            make.leading.equalToSuperview().offset(12)
        }
        
        diamondCountLabel.text = "0"
        diamondCountLabel.font = .heading_h2
        diamondCountLabel.textColor = .text_5
        diamondTextView.addSubview(diamondCountLabel)
        diamondCountLabel.snp.makeConstraints { make in
            make.leading.equalToSuperview()
            make.top.equalToSuperview()
            make.trailing.equalToSuperview()
        }
        
        let diamondLabel = UILabel()
        diamondLabel.text = .init(key: "A00217")
        diamondLabel.font = .heading_h5
        diamondLabel.textColor = .text_5
        diamondTextView.addSubview(diamondLabel)
        diamondLabel.snp.makeConstraints { make in
            make.leading.equalToSuperview()
            make.top.equalTo(diamondCountLabel.snp.bottom)
            make.bottom.equalToSuperview()
            make.trailing.lessThanOrEqualToSuperview()
        }
        
        subviews.forEach { $0.isUserInteractionEnabled = false }
    }
}
