//
//  LeaderboardListCell.swift

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

class LeaderboardListCell: UITableViewCell {
    var avatarView: CLImageView!
    var iconAndLabel: CLIconLabel!
    var rankLabel: UILabel!
    var contentStackView: UIStackView!
    var nameLabel: CLLabel!
//    var descLabel: CLLabel!
    var descIconAndLabel: CLIconLabel!
    
    var data: AIDiscoverTop?
    var type: DiscoverRankType = .chat

    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() {
        avatarView = {
            let v = CLImageView()
            v.contentMode = .scaleAspectFill
            v.layer.cornerRadius = 24
            v.layer.masksToBounds = true
            contentView.addSubview(v)
            v.snp.makeConstraints { make in
                make.leading.equalToSuperview().offset(60)
                make.top.equalToSuperview().offset(16)
                make.bottom.equalToSuperview().offset(-16)
                make.width.height.equalTo(48)
            }
            return v
        }()

        iconAndLabel = {
            let v = CLIconLabel()
            v.iconImageView.image = MWIconFont.image(fromIcon: .chat, size: CGSize(width: 16, height: 16), color: .text)
            v.iconSize = CGSize(width: 16, height: 16)
            contentView.addSubview(v)
            v.snp.makeConstraints { make in
                make.trailing.equalToSuperview().offset(-24)
                make.centerY.equalToSuperview()
            }
            return v
        }()

        rankLabel = {
            let v = UILabel()
            v.font = .t.tnml
            v.textColor = .c.ctpn
            contentView.addSubview(v)
            v.snp.makeConstraints { make in
                make.centerX.equalTo(contentView.snp.leading).offset(30)
                make.centerY.equalToSuperview()
            }
            return v
        }()

        contentStackView = {
            let v = UIStackView()
            v.axis = .vertical
            v.alignment = .leading
            v.spacing = 4
            contentView.addSubview(v)
            v.snp.makeConstraints { make in
                make.leading.equalTo(avatarView.snp.trailing).offset(12)
                make.trailing.lessThanOrEqualTo(iconAndLabel.snp.leading).offset(-12)
                make.centerY.equalToSuperview()
            }
            return v
        }()

        nameLabel = {
            let v = CLLabel()
            v.font = .t.tts
            v.textColor = .c.ctpn
            contentStackView.addArrangedSubview(v)
            return v
        }()

//        descLabel = {
//            let v = CLLabel()
//            v.font = .t.tls
//            v.textColor = .c.ctsn
//            contentStackView.addArrangedSubview(v)
//            return v
//        }()
        
        descIconAndLabel = {
            let v = CLIconLabel()
            v.iconImageView.image = MWIconFont.image(fromIcon: .likeFill, size: CGSize(width: 12, height: 12), color: .c.ctsn)
            v.contentLabel.font = .t.tls
            v.contentLabel.textColor = .c.ctsn
            contentStackView.addArrangedSubview(v)
            return v
        }()

//        #warning("test")
//        testData()
    }

    func config(_ data: AIDiscoverTop?, type: DiscoverRankType) {
        self.data = data
        self.type = type
        
        guard let rank = data else { return }
        
        // 配置排名
        rankLabel.text = "\(rank.rankNo ?? 0)"
        
        // 配置头像
        avatarView.loadImage(rank.headImg)
        
        // 配置名称和描述
        nameLabel.text = rank.nickname ?? "-"
        
        
        //let likesDisplayString = "\(String.displayNumber(NSNumber(value: rank.likedNum ?? 0), scale: 1)) Likes"
        //descLabel.text = likesDisplayString
        
        let likesDisplayString = String.displayNumber(NSNumber(value: rank.likedNum ?? 0), scale: 1)
        descIconAndLabel.contentLabel.text = likesDisplayString
        
        // 配置右侧数值和图标
        var content = "-"
        switch type {
        case .chat:
            content = rank.formateChatDisplay()
        case .heartbeat:
            content = rank.formateHeartbeatDisplay()
        case .gift:
            content = rank.formateGiftDisplay()
        }
        iconAndLabel.contentLabel.text = content
        refreshIconLabelIcon()
    }
    
    private func refreshIconLabelIcon(){
        switch type {
        case .chat:
            iconAndLabel.iconImageView.image = MWIconFont.image(fromIcon: .chat, size: CGSize(width: 16, height: 16), color: .white)
        case .heartbeat:
            iconAndLabel.iconImageView.image = UIImage(named: "heartbeat")
        case .gift:
            iconAndLabel.iconImageView.image = UIImage(named: "icon_16_diamond")
        }
    }

    private func testData() {
        iconAndLabel.contentLabel.text = "123"
        rankLabel.text = "1"
        nameLabel.text = "123"
//        descLabel.text = "123"
    }
}

class LeaderboardListTopThreeCell: UITableViewCell {
    var topThreeView: DiscoverTopThreeRank!
    
    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() {
        topThreeView = {
            let v = DiscoverTopThreeRank()
            contentView.addSubview(v)
            v.snp.makeConstraints { make in
                make.top.equalToSuperview().offset(12)
                make.bottom.equalToSuperview().offset(-12)
                make.leading.equalToSuperview().offset(24)
                make.trailing.equalToSuperview().offset(-24)
            }
            v.setup2nd3rdBottomPdding()
            return v
        }()
    }
    
    func config(_ data: [AIDiscoverTop]?, type: DiscoverRankType) {
        topThreeView.type = type
        topThreeView.config(data)
    }
}
