//
//  BillListViews.swift

//
//  Created by Leon on 2025/9/16.
//
import Combine
class CoinsIncomeHeadView: UIView{
    var block: UIView!
    var bottomBg: UIImageView!
    
    var stackV:UIStackView!
    var titleLabel1: CLLabel!
    var coinAndLabel1 : CLIconLabel!
    
    var line: CLLine!
    var titleLabel2: CLLabel!
    var queryButton: EPIconTertiaryDarkButton!
    var coinAndLabel2 : CLIconLabel!
    
    var billListEntryLabel: CLLabel!
    var billListEntryButton: UIButton!

    var tableTitleLabel: CLLabel!
    
    static let heightOfHeader = 372.0
    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(292)
            }
            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
        }()
        
        stackV = {
            let v = UIStackView()
            v.axis = .vertical
            v.spacing = 24
            block.addSubview(v)
            v.snp.makeConstraints { make in
                make.leading.equalToSuperview().offset(24)
                make.trailing.equalToSuperview().offset(-24)
                make.top.equalToSuperview().offset(24)
            }
            return v
        }()
        
        do{
            let view = UIView()
            stackV.addArrangedSubview(view)
            
            titleLabel1 = {
                let v = CLLabel()
                v.font = .t.tts
                view.addSubview(v)
                v.snp.makeConstraints { make in
                    make.leading.equalToSuperview()
                    make.top.equalToSuperview()
                }
                return v
            }()
            titleLabel1.text = "Income"
            
            coinAndLabel1 = {
                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"
                view.addSubview(v)
                v.snp.makeConstraints { make in
                    make.leading.equalToSuperview()
                    make.trailing.lessThanOrEqualToSuperview()
                    make.top.equalTo(titleLabel1.snp.bottom).offset(4)
                    make.bottom.equalToSuperview()
                }
                return v
            }()
        }
        
        line = {
            let v = CLLine()
            stackV.addArrangedSubview(v)
            v.snp.makeConstraints { make in
                make.height.equalTo(1)
            }
            return v
        }()
        
        do{
            let view = UIView()
            stackV.addArrangedSubview(view)
            
            titleLabel2 = {
                let v = CLLabel()
                v.font = .t.tts
                view.addSubview(v)
                v.snp.makeConstraints { make in
                    make.leading.equalToSuperview()
                    make.top.equalToSuperview()
                }
                return v
            }()
            titleLabel2.text = "Pending"
            
            queryButton = {
                let v = EPIconTertiaryDarkButton(radius: .round, iconSize: .xs, iconCode: .question)
                v.addTarget(self, action: #selector(tapQueryButton), for: .touchUpInside)
                view.addSubview(v)
                v.snp.makeConstraints { make in
                    make.centerY.equalTo(titleLabel2)
                    make.leading.equalTo(titleLabel2.snp.trailing).offset(4)
                    make.size.equalTo(v.bgImageSize())
                }
                return v
            }()
            
            coinAndLabel2 = {
                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"
                view.addSubview(v)
                v.snp.makeConstraints { make in
                    make.leading.equalToSuperview()
                    make.trailing.lessThanOrEqualToSuperview()
                    make.top.equalTo(titleLabel2.snp.bottom).offset(4)
                    make.bottom.equalToSuperview()
                }
                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()
            // v.addTarget(self, action: #selector(billListEntryButtonAction), for: .touchUpInside)
            block.addSubview(v)
            v.snp.makeConstraints { make in
                make.centerY.equalTo(billListEntryLabel)
                make.leading.trailing.equalTo(billListEntryLabel)
                make.height.equalTo(30)
            }
            return v
        }()
        
        tableTitleLabel = {
            let v = CLLabel()
            v.font = .t.ttm
            addSubview(v)
            v.snp.makeConstraints { make in
                make.leading.equalToSuperview().offset(24)
                make.top.equalTo(block.snp.bottom).offset(24)
            }
            return v
        }()
        billListEntryLabel.text = "Transaction Details>"
        tableTitleLabel.text = "Creation Income"
        
        
    }
    
    private func setupEvent(){
        WalletCore.shared.$balance.sink {[weak self] _ in
            self?.refreshBalanceShow()
        }.store(in: &cancellables)
    }
    
    // MARK: - Helper
    
    private func refreshBalanceShow(){
        let balance = WalletCore.shared.balance
        let income = balance.income ?? 0
        let pending = balance.awaitingIncome ?? 0
        let incomeCoin = Coin(cents: income)
        let pendingCoin = Coin(cents: pending)
        
        coinAndLabel1.contentLabel.text = incomeCoin.thousandthsFormatted
        coinAndLabel2.contentLabel.text = pendingCoin.thousandthsFormatted
    }

    // MARK: - ACtion
    @objc func tapQueryButton(){
        let content = "获得的收益30日后可提现."
        let alert = Alert(title: "Tips", text: content)
        let action1 = AlertAction(title: "Got it", actionStyle: .confirm) {
        }
        alert.addAction(action1)
        alert.show()
    }
    
//    @objc func billListEntryButtonAction() {
//        print("billListEntryButtonAction")
//    }
    
//    func config(_ model: BillListResponse?){
//        let income = model?.incomeTotal ?? 0
//        let pending = model?.outcomeTotal ?? 0
//        let incomeCoin = Coin(cents: income)
//        let pendingCoin = Coin(cents: pending)
//        
//        coinAndLabel1.contentLabel.text = incomeCoin.formatted
//        coinAndLabel2.contentLabel.text = pendingCoin.formatted
//    }
}
