//
//  SessionNoticeCell.swift

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

import UIKit

class SessionNoticeCell: UITableViewCell {
    var iconBlock: UIView!
    var icon: UIImageView!
    var badge: BadgeView!

    var contentStackV: UIStackView!
    var titleLabel: UILabel!
    var contentLabel: UILabel!

    var timeLabel: UILabel!

    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() {
        iconBlock = {
            let v = UIView()
            v.backgroundColor = .c.cpn
            v.layer.cornerRadius = 24
            v.layer.masksToBounds = true
            contentView.addSubview(v)
            v.snp.makeConstraints { make in
                make.top.equalToSuperview().offset(16)
                make.leading.equalToSuperview().offset(24)
                make.bottom.equalToSuperview().offset(-16)
                make.size.equalTo(CGSize(width: 48, height: 48))
            }
            return v
        }()

        icon = {
            let v = UIImageView()
            iconBlock.addSubview(v)
            v.image = MWIconFont.image(fromIcon: .messages, size: CGSize(width: 20, height: 20), color: .white)
            v.snp.makeConstraints { make in
                make.center.equalToSuperview()
            }
            return v
        }()

        badge = {
            let v = BadgeView()
            v.showMax99 = true
            addSubview(v)
            v.snp.makeConstraints { make in
                make.top.equalTo(iconBlock)
                make.trailing.equalTo(iconBlock).offset(4)
            }
            return v
        }()

        timeLabel = {
            let v = UILabel()
            v.font = .t.tbs
            v.textColor = .c.ctsn
            contentView.addSubview(v)
            v.snp.makeConstraints { make in
                make.trailing.equalToSuperview().offset(-24)
                make.top.equalTo(iconBlock)
            }
            return v
        }()

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

        titleLabel = {
            let v = UILabel()
            v.font = .t.tts
            v.textColor = .text
            contentStackV.addArrangedSubview(v)
            return v
        }()

        contentLabel = {
            let v = UILabel()
            v.font = .t.tbs
            v.textColor = .white
            contentStackV.addArrangedSubview(v)
            return v
        }()

        titleLabel.text = "Notice"
        contentLabel.text = " "
        timeLabel.text = " "
        
        //testData()
    }

    private func testData() {
        timeLabel.text = "22:28"
        titleLabel.text = "Notice"
        badge.badgeValue = 100
        contentLabel.text = "Welcome to BGMDOG to learn about Crush Level"
    }
    
    func config(_ stat: MessageStat?){
        guard let data = stat else {return}
        
        badge.badgeValue = data.unRead ?? 0
        contentLabel.text = data.latestContent
        if let timestamp = data.latestTime{
            timeLabel.text = Date.timerStyle(style: .IMCHAT, millisecond: timestamp)
        }else{
            timeLabel.text = ""
        }
    }
}
