//
//  LNUserSearchRoomCardView.swift
//  Gami
//
//  Created by OneeChan on 2026/3/19.
//

import Foundation
import UIKit
import SnapKit


class LNUserSearchRoomCardView: UIView {
    private let cover = UIImageView()
    private let bottomCover = UIImageView()
    private let idLabel = UILabel()
    private let hotIconView = UIImageView()
    private let hotCountLabel = UILabel()
    private let titleLabel = UILabel()
    private let subtitleLabel = UILabel()
    
    private var curItem: LNRoomItemVO?
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        setupViews()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func update(_ item: LNRoomItemVO) {
        idLabel.text = "ID \(item.roomId)"
//        hotCountLabel.text = "\(item.viewers)"
        cover.sd_setImage(with: URL(string: item.roomCover))
        titleLabel.text = item.roomTitle
        subtitleLabel.text = item.user.nickname
        
        curItem = item
    }
}

extension LNUserSearchRoomCardView {
    private func setupViews() {
        backgroundColor = .clear
        
        onTap { [weak self] in
            guard let self, let curItem else { return }
            pushToRoom(curItem.roomId)
        }
        
        cover.layer.cornerRadius = 12
        cover.clipsToBounds = true
        cover.contentMode = .scaleAspectFill
        addSubview(cover)
        cover.snp.makeConstraints { make in
            make.top.equalToSuperview()
            make.horizontalEdges.equalToSuperview()
            make.height.equalTo(cover.snp.width)
        }
        
        bottomCover.image = UIImage.generateLinearGradientImage(
            colors: [.clear, .black.withAlphaComponent(0.65)],
            size: .init(width: 10, height: 10),
            endPoint: .init(x: 0, y: 1)
        )
        cover.addSubview(bottomCover)
        bottomCover.snp.makeConstraints { make in
            make.horizontalEdges.equalToSuperview()
            make.bottom.equalToSuperview()
            make.height.equalTo(30)
        }
        
        idLabel.font = .body_xs
        idLabel.textColor = .text_1
        cover.addSubview(idLabel)
        idLabel.snp.makeConstraints { make in
            make.leading.equalToSuperview().offset(8)
            make.bottom.equalToSuperview().offset(-6)
        }
        
        hotIconView.isHidden = true
        hotIconView.image = UIImage(systemName: "flame.fill")?.withRenderingMode(.alwaysTemplate)
        hotIconView.tintColor = .white
        cover.addSubview(hotIconView)
        hotIconView.snp.makeConstraints { make in
            make.trailing.equalToSuperview().offset(-20)
            make.bottom.equalToSuperview().offset(-8)
            make.width.height.equalTo(10)
        }
        
        hotCountLabel.isHidden = true
        hotCountLabel.font = .body_xs
        hotCountLabel.textColor = .text_1
        cover.addSubview(hotCountLabel)
        hotCountLabel.snp.makeConstraints { make in
            make.leading.equalTo(hotIconView.snp.trailing).offset(2)
            make.centerY.equalTo(hotIconView)
            make.trailing.equalToSuperview().offset(-8)
        }
        
        titleLabel.font = .heading_h4
        titleLabel.textColor = .text_5
        titleLabel.numberOfLines = 1
        addSubview(titleLabel)
        titleLabel.snp.makeConstraints { make in
            make.leading.trailing.equalToSuperview()
            make.top.equalTo(cover.snp.bottom).offset(6)
        }
        
        subtitleLabel.font = .body_xs
        subtitleLabel.textColor = .text_4
        subtitleLabel.numberOfLines = 1
        addSubview(subtitleLabel)
        subtitleLabel.snp.makeConstraints { make in
            make.leading.trailing.equalToSuperview()
            make.top.equalTo(titleLabel.snp.bottom).offset(2)
            make.bottom.equalToSuperview()
        }
    }
}
