//
//  NoticeCenterListCell.swift

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

import UIKit

protocol NoticeCenterListCellDelegate: AnyObject{
    func noticeCellTapCheck(data:MessageNotice)
}

class NoticeCenterListCell: UITableViewCell {
    weak var delegate: NoticeCenterListCellDelegate?
    
    var block: UIView!
    var contentStackV: UIStackView!
    var titleLabel: UILabel!
    var contentLabel: LineSpaceLabel!

    var bottomContainer: UIView!
    var timeLabel: UILabel!
    var checkButton: StyleButton!

    var data: MessageNotice?
    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code
    }

    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)

        // Configure the view for the selected state
    }

    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.layer.cornerRadius = 16
            v.layer.masksToBounds = true
            contentView.addSubview(v)
            v.snp.makeConstraints { make in
                make.edges.equalToSuperview().inset(UIEdgeInsets(top: 12, left: 24, bottom: 4, right: 24))
            }
            return v
        }()
        contentStackV = {
            let v = UIStackView()
            v.axis = .vertical
            v.spacing = 8
            v.alignment = .leading
            block.addSubview(v)
            v.snp.makeConstraints { make in
                make.edges.equalToSuperview().inset(UIEdgeInsets(top: 16, left: 16, bottom: 16, right: 16))
            }
            return v
        }()

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

        contentLabel = {
            let v = LineSpaceLabel()
            let typo = CLSystemToken.typography(token: .tbs)
            v.config(typo)
            v.textColor = .text
            contentStackV.addArrangedSubview(v)
            return v
        }()

        bottomContainer = {
            let v = UIView()
            contentStackV.addArrangedSubview(v)
            v.snp.makeConstraints { make in
                make.leading.trailing.equalToSuperview()
                make.height.equalTo(32)
            }
            return v
        }()

        timeLabel = {
            let v = UILabel()
            v.textColor = .c.ctsn
            v.font = .t.tbs
            bottomContainer.addSubview(v)
            v.snp.makeConstraints { make in
                make.centerY.equalToSuperview()
                make.leading.equalToSuperview()
            }
            return v
        }()

        checkButton = {
            let v = StyleButton()
            v.tertiary(size: .small)
            v.addTarget(self, action: #selector(tapCheckButton), for: .touchUpInside)
            bottomContainer.addSubview(v)
            v.snp.makeConstraints { make in
                make.trailing.equalToSuperview()
                make.centerY.equalToSuperview()
            }
            return v
        }()
        checkButton.setTitle("Check", for: .normal)

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

    private func testData() {
        titleLabel.text = "About CrushLevel AI"
        contentLabel.text = "Experience a love story with Crushlevel AI: From \"Hello\" to \"I do\", every conversation is filled with heart.\nAt Crushlevel AI, every\nconversation is writing your love epic—from the awkward \"Hello\" to the trembling \"Marry Me\".\nThose love words that dare not say in reality, unstoppable responses, and fear of injury have finally found a place to be placed."
        timeLabel.text = "00:15"
    }
    
    func config(_ data: MessageNotice?){
        self.data = data
        guard let msg = data else{
            return
        }
        
        titleLabel.text = msg.title
        contentLabel.text = msg.content
        
        if let timestamp = msg.createTime{
            let display = Date.timerStyle(style: .IMCHAT, millisecond: timestamp)
            timeLabel.text = display
        }else{
            timeLabel.text = "-"
        }
        
        if let type = data?.type{
            checkButton.isHidden = type.hasNoCheckEntrance()
        }else{
            checkButton.isHidden = true
        }
    }

    @objc private func tapCheckButton() {
        delegate?.noticeCellTapCheck(data: self.data!)
    }
}
