//
//  CoinsRechargeHeader.swift

//
//  Created by Leon on 2025/9/15.
//

import Combine

class CoinsRechargeHeader: UIView {
    var block: UIView!
    var bottomBg: UIImageView!
    var titleLabel: UILabel!
    var coinAndLabel: CLIconLabel!
    var billListEntryLabel: CLLabel!
    var billListEntryButton: UIButton!
    
    
    private var cancellables = Set<AnyCancellable>()

    override init(frame: CGRect) {
        super.init(frame: frame)
        setupViews()
        setupEvent()
    }

    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    private func setupViews() {
        block = {
            let v = UIView()
            v.backgroundColor = .clear
            v.cornerRadius = 16
            v.clipsToBounds = true
            v.layer.borderWidth = 1
            v.layer.borderColor = UIColor.c.con.cgColor
            addSubview(v)
            v.snp.makeConstraints { make in
                make.top.equalToSuperview().offset(16)
                make.leading.equalToSuperview().offset(24)
                make.trailing.equalToSuperview().offset(-24)
                make.height.equalTo(168)
            }
            return v

        }()

        bottomBg = {
            let v = UIImageView()
            v.image = UIImage(named: "walletBg")
            let size = v.image?.size ?? .zero
            v.contentMode = .scaleAspectFill
            v.clipsToBounds = true
            block.addSubview(v)
            v.snp.makeConstraints { make in
                make.bottom.equalToSuperview()
                make.leading.trailing.equalToSuperview()
                make.height.equalTo(v.snp.width).multipliedBy(size.height / size.width)
            }
            return v
        }()

        titleLabel = {
            let v = UILabel()
            v.font = .t.tts
            v.textColor = .c.ctpn
            block.addSubview(v)
            v.snp.makeConstraints { make in
                make.top.equalToSuperview().offset(24)
                make.centerX.equalToSuperview()
            }

            return v
        }()

        coinAndLabel = {
            let v = CLIconLabel()
            v.spacing = 8
            v.iconSize = CGSize(width: 24, height: 24)
            v.iconImageView.image = UIImage.icon32Diamond
            v.contentLabel.font = .t.tndl
            v.contentLabel.textColor = .white
            v.contentLabel.text = "0"
            block.addSubview(v)
            v.snp.makeConstraints { make in
                make.centerX.equalToSuperview()
                make.top.equalTo(titleLabel.snp.bottom).offset(4)
            }
            return v
        }()

        billListEntryLabel = {
            let v = CLLabel()
            v.textColor = .c.cpvn
            v.font = .t.tls
            block.addSubview(v)
            v.snp.makeConstraints { make in
                make.centerX.equalToSuperview()
                make.bottom.equalToSuperview().offset(-24)
            }
            return v
        }()

        billListEntryButton = {
            let v = UIButton()
            
            block.addSubview(v)
            v.snp.makeConstraints { make in
                make.centerY.equalTo(billListEntryLabel)
                make.leading.trailing.equalTo(billListEntryLabel)
                make.height.equalTo(30)
            }
            return v
        }()

        let rechargeTitleLabel = {
            let v = CLLabel()
            v.font = .t.ttm
            addSubview(v)
            v.snp.makeConstraints { make in
                make.leading.equalToSuperview().offset(24)
                make.trailing.equalToSuperview().offset(-24)
                make.top.equalTo(block.snp.bottom).offset(24)
            }
            return v
        }()

        titleLabel.text = "Balance"
        billListEntryLabel.text = "Transaction Details>"
        rechargeTitleLabel.text = "Recharge"
    }
    
    private func setupEvent(){
        WalletCore.shared.$balance.sink {[weak self] wallet in
            self?.coinAndLabel.contentLabel.text = wallet.displayBalance()
        }.store(in: &cancellables)
    }

    
}
