//
//  LNFeedCommentCell.swift
//  Gami
//
//  Created by OneeChan on 2026/3/3.
//

import Foundation
import UIKit
import SnapKit


class LNFeedCommentCell: UITableViewCell {
    private let avatar = UIImageView()
    private let nameLabel = UILabel()
    private let contentLabel = UILabel()
    private let timeLabel = UILabel()
    
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        
        setupViews()
    }
    
    func update(_ comment: LNFeedCommentVO) {
        avatar.sd_setImage(with: URL(string: comment.avatar))
        nameLabel.text = comment.nickname
        timeLabel.text = TimeInterval(comment.createdAt / 1_000).tencentIMTimeDesc
        contentLabel.attributedText = comment.textContent.getEmojiString(with: .body_m)
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}

extension LNFeedCommentCell {
    private func setupViews() {
        avatar.layer.cornerRadius = 21
        avatar.clipsToBounds = true
        contentView.addSubview(avatar)
        avatar.snp.makeConstraints { make in
            make.leading.equalToSuperview().offset(16)
            make.top.equalToSuperview()
            make.width.height.equalTo(42)
        }
        
        let stackView = UIStackView()
        stackView.axis = .vertical
        stackView.spacing = 4
        contentView.addSubview(stackView)
        stackView.snp.makeConstraints { make in
            make.top.equalToSuperview()
            make.trailing.equalToSuperview().offset(-16)
            make.leading.equalTo(avatar.snp.trailing).offset(10)
            make.bottom.equalToSuperview().offset(-22)
        }
        
        nameLabel.font = .heading_h4
        nameLabel.textColor = .text_5
        stackView.addArrangedSubview(nameLabel)
        
        contentLabel.font = .body_m
        contentLabel.textColor = .text_5
        contentLabel.numberOfLines = 0
        stackView.addArrangedSubview(contentLabel)
        
        timeLabel.font = .body_xs
        timeLabel.textColor = .text_3
        stackView.addArrangedSubview(timeLabel)
    }
}
