//
//  LNRoomWelcomeMessageCell.swift
//  Gami
//
//  Created by OneeChan on 2026/3/22.
//

import Foundation
import UIKit
import SnapKit


class LNRoomWelcomeMessageCell: UITableViewCell {
    private let contentLabel = UILabel()
    private var curItem: LNRoomWelcomeMessageItem?
    
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        
        setupViews()
    }
    
    func update(_ message: LNRoomWelcomeMessageItem) {
        let text = String(key: "A00371", message.nickname)
        let attr = NSMutableAttributedString(string: text)
        let range = (text as NSString).range(of: message.nickname)
        attr.addAttributes([
            .foregroundColor: UIColor.text_7,
            .font: UIFont.heading_h5
        ], range: range)
        contentLabel.attributedText = attr
        
        curItem = message
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}

extension LNRoomWelcomeMessageCell {
    private func setupViews() {
        backgroundColor = .clear
        
        let container = UIView()
        container.backgroundColor = .fill.withAlphaComponent(0.12)
        container.layer.cornerRadius = 12
        container.onTap { [weak self] in
            guard let self else { return }
            guard let curItem else { return }
            let panel = LNRoomProfileCardPanel()
            panel.load(curItem.userNo)
            panel.popup(self)
        }
        contentView.addSubview(container)
        container.snp.makeConstraints { make in
            make.verticalEdges.equalToSuperview().inset(8)
            make.leading.equalToSuperview()
            make.trailing.lessThanOrEqualToSuperview()
        }
        
        contentLabel.text = " "
        contentLabel.font = .heading_h5
        contentLabel.textColor = .text_1
        contentLabel.numberOfLines = 0
        container.addSubview(contentLabel)
        contentLabel.snp.makeConstraints { make in
            make.horizontalEdges.equalToSuperview().inset(10)
            make.verticalEdges.equalToSuperview().inset(8)
        }
    }
}

