//
//  LNFeedCommentView.swift
//  Gami
//
//  Created by OneeChan on 2026/3/5.
//

import Foundation
import UIKit
import SnapKit


class LNFeedCommentView: UIView, LNFeedManagerNotify {
    let commentIc = UIImageView()
    let commentLabel = UILabel()
    
    var uiColor: UIColor = .text_4 {
        didSet {
            commentLabel.textColor = uiColor
            commentIc.image = .icProfileBio.withTintColor(uiColor, renderingMode: .alwaysOriginal)
        }
    }
    
    private var curId: String?
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        commentIc.isUserInteractionEnabled = false
        commentIc.image = .icProfileBio.withTintColor(.text_4, renderingMode: .alwaysOriginal)
        addSubview(commentIc)
        commentIc.snp.makeConstraints { make in
            make.leading.equalToSuperview()
            make.verticalEdges.equalToSuperview()
            make.width.height.equalTo(24)
        }
        
        commentLabel.isUserInteractionEnabled = false
        commentLabel.font = .heading_h5
        commentLabel.textColor = .text_4
        addSubview(commentLabel)
        commentLabel.snp.makeConstraints { make in
            make.centerY.equalToSuperview()
            make.trailing.equalToSuperview()
            make.leading.equalTo(commentIc.snp.trailing).offset(2)
        }
        
        LNEventDeliver.addObserver(self)
    }
    
    func update(id: String, count: Int) {
        curId = id
        
        _updateUI(count: count)
    }
    
    func onFeedCommentCountChanged(id: String, count: Int) {
        guard id == curId else { return }
        
        _updateUI(count: count)
    }
    
    private func _updateUI(count: Int) {
        commentLabel.text = count == 0 ? .init(key: "A00302") : count.formattedAsShortNumber()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}

