//
//  LNRoomChatMessageCell.swift
//  Gami
//
//  Created by OneeChan on 2026/3/11.
//

import Foundation
import UIKit
import SnapKit


class LNRoomChatMessageCell: UITableViewCell {
    private let avatarView = UIImageView()
    private let nameLabel = UILabel()
    private let contentLabel = UILabel()
    
    private var curItem: LNRoomChatMessageItem?
    
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        
        setupViews()
    }
    
    func update(_ message: LNRoomChatMessageItem) {
        contentLabel.attributedText = message.content.getEmojiString(with: .heading_h5)
        avatarView.sd_setImage(with: URL(string: message.avatar))
        nameLabel.text = message.nickname
        
        curItem = message
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}

extension LNRoomChatMessageCell {
    private func showProfileCard() {
        guard let curItem else { return }
        let panel = LNRoomProfileCardPanel()
        panel.load(curItem.sender)
        panel.popup(self)
    }
}

extension LNRoomChatMessageCell {
    private func setupViews() {
        backgroundColor = .clear
        
        avatarView.layer.cornerRadius = 13
        avatarView.layer.borderColor = .fill
        avatarView.layer.borderWidth = 0.5
        avatarView.clipsToBounds = true
        avatarView.onTap { [weak self] in
            guard let self else { return }
            showProfileCard()
        }
        contentView.addSubview(avatarView)
        avatarView.snp.makeConstraints { make in
            make.leading.equalToSuperview()
            make.top.equalToSuperview().offset(8)
            make.width.height.equalTo(26)
            make.bottom.lessThanOrEqualToSuperview().offset(-8)
        }
        
        let bodyView = UIView()
        contentView.addSubview(bodyView)
        bodyView.snp.makeConstraints { make in
            make.leading.equalTo(avatarView.snp.trailing).offset(10)
            make.top.equalTo(avatarView)
            make.bottom.equalToSuperview().offset(-8)
            make.trailing.lessThanOrEqualToSuperview()
        }
        
        nameLabel.font = .body_s
        nameLabel.textColor = .text_2
        nameLabel.setContentHuggingPriority(.required, for: .vertical)
        nameLabel.onTap { [weak self] in
            guard let self else { return }
            showProfileCard()
        }
        bodyView.addSubview(nameLabel)
        nameLabel.snp.makeConstraints { make in
            make.horizontalEdges.equalToSuperview()
            make.top.equalToSuperview()
        }
        
        let bubble = UIView()
        bubble.backgroundColor = .fill.withAlphaComponent(0.12)
        bubble.layer.cornerRadius = 12
        bodyView.addSubview(bubble)
        bubble.snp.makeConstraints { make in
            make.top.equalTo(nameLabel.snp.bottom).offset(4)
            make.leading.equalToSuperview()
            make.trailing.lessThanOrEqualToSuperview()
            make.bottom.equalToSuperview()
        }
        
        contentLabel.font = .heading_h5
        contentLabel.textColor = .text_1
        contentLabel.numberOfLines = 0
        bubble.addSubview(contentLabel)
        contentLabel.snp.makeConstraints { make in
            make.horizontalEdges.equalToSuperview().inset(9)
            make.verticalEdges.equalToSuperview().inset(8)
        }
    }
}
