//
//  BillListCell.swift

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

class BillListCell : UITableViewCell{
    
    var block: UIView!
    var icon: UIImageView!
    var coinAndLabel: CLIconLabel!
    var titleLabel: CLLabel!
    var descLabel: CLLabel!
    
    var bill: BillListInfo?
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        
        selectionStyle = .none
        backgroundColor = .clear
        
        setupViews()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func setupViews(){
        block = {
            let v = UIView()
            v.backgroundColor = .c.csbn
            v.cornerRadius = 12
            contentView.addSubview(v)
            v.snp.makeConstraints { make in
                make.leading.equalToSuperview().offset(24)
                make.trailing.equalToSuperview().offset(-24)
                make.top.equalToSuperview()
                make.bottom.equalToSuperview().offset(-16)
            }
            return v
        }()
        
        icon = {
            let v = UIImageView()
            block.addSubview(v)
            v.snp.makeConstraints { make in
                make.size.equalTo(CGSize(width: 48, height: 48))
                make.leading.equalToSuperview().offset(16)
                make.top.equalToSuperview().offset(16)
                make.bottom.equalToSuperview().offset(-16)
            }
            return v
        }()
        
        coinAndLabel = {
            let v = CLIconLabel()
            v.iconSize = CGSize(width: 16, height: 16)
            v.iconImageView.image = UIImage.icon32Diamond
            v.contentLabel.font = .t.tnmm
            v.contentLabel.textColor = .white
            v.contentLabel.text = "0"
            block.addSubview(v)
            v.snp.makeConstraints { make in
                make.centerY.equalToSuperview()
                make.trailing.equalToSuperview().offset(-16)
            }
            return v
        }()
        
        titleLabel = {
            let v = CLLabel()
            v.font = .t.tts
            block.addSubview(v)
            v.snp.makeConstraints { make in
                make.top.equalTo(icon)
                make.leading.equalTo(icon.snp.trailing).offset(12)
                make.trailing.lessThanOrEqualTo(coinAndLabel.snp.leading).offset(-12)
            }
            return v
        }()
        
        descLabel = {
            let v = CLLabel()
            v.font = .t.tbs
            block.addSubview(v)
            v.snp.makeConstraints { make in
                make.leading.equalTo(titleLabel)
                make.bottom.equalTo(icon)
                make.trailing.lessThanOrEqualTo(coinAndLabel.snp.leading).offset(-12)
            }
            v.text = "-"
            return v
        }()
        
//        testData()
    }
    
    private func testData(){
        icon.backgroundColor = .random
        titleLabel.text = "Gift"
        descLabel.text = "Sep 9, 2025 11:11:11"
    }
    
    func config(_ bill: BillListInfo?){
        self.bill = bill
        guard let data = bill else {return}
        
        // -- Icon
//        if let img = data.headImg{
//            icon.loadImage(img)
//        }
        icon.loadImage(data.headImg, bgColor: .c.csnn)
        titleLabel.text = data.item
        
        // Desc
        if let timestamp = data.time{
//            let date = Date.dateFromMilliseconds(Int64(timestamp))
            let display = Date.timerStyle(style: .WDMYHMS, millisecond: timestamp)
            descLabel.text = display
        }else{
            descLabel.text = "-"
        }
        
        
        let amount = data.amount ?? 0
        let coin = Coin(cents: amount)
        let inOrOut = data.inOrOut ?? .in
        coinAndLabel.contentLabel.text = "\(inOrOut == .in ? "+":"-")\(coin.formatted)"
        
        // Other
        if let bizType = data.bizType{
            if bizType == "GIFT"{
                if let obj = data.getExtendObj(), let giftId = obj.giftId{
                    AppDictManager.shared.getGiftModelBy(giftId: giftId) {[weak self] giftModel in
                        self?.icon.loadImage(giftModel?.icon)
                    }
                }
            }
        }
        
        
    }
}
