//
//  LNOrderRoomApplySeatCell.swift
//  Gami
//
//  Created by OneeChan on 2026/3/18.
//

import Foundation
import UIKit
import SnapKit


class LNOrderRoomApplySeatCell: UITableViewCell {
    private let indexLabel = UILabel()
    private let avatarView = UIImageView()
    private let nameLabel = UILabel()
    private let genderView = UIImageView()
    private let timeLabel = UILabel()
    private let separator = UIView()
    
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        
        setupViews()
    }
    
    func update(item: LNRoomMicApplyPageVO, index: Int) {
        indexLabel.text = "\(index)"
        avatarView.showAvatar(item.user.avatar)
        nameLabel.text = item.user.nickname
        timeLabel.text = TimeInterval(item.applyTime / 1_000).relativeTimeText
        
        genderView.image = switch item.user.gender {
        case .unknow: nil
        case .male: .icGenderMale
        case .female: .icGenderFemale
        }
        genderView.isHidden = item.user.gender == .unknow
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}

private extension LNOrderRoomApplySeatCell {
    func setupViews() {
        backgroundColor = .clear
        contentView.backgroundColor = .clear
        selectionStyle = .none
        
        indexLabel.font = .heading_h3
        indexLabel.textColor = UIColor.text_2.withAlphaComponent(0.7)
        indexLabel.textAlignment = .center
        contentView.addSubview(indexLabel)
        indexLabel.snp.makeConstraints { make in
            make.leading.equalToSuperview().offset(16)
            make.centerY.equalToSuperview()
            make.width.equalTo(18)
        }
        
        avatarView.layer.cornerRadius = 16
        avatarView.layer.borderWidth = 0.5
        avatarView.layer.borderColor = UIColor.fill.cgColor
        avatarView.clipsToBounds = true
        avatarView.contentMode = .scaleAspectFill
        contentView.addSubview(avatarView)
        avatarView.snp.makeConstraints { make in
            make.leading.equalTo(indexLabel.snp.trailing).offset(12)
            make.verticalEdges.equalToSuperview().inset(14)
            make.width.height.equalTo(32)
        }
        
        timeLabel.font = .body_s
        timeLabel.textColor = .text_2
        timeLabel.textAlignment = .right
        contentView.addSubview(timeLabel)
        timeLabel.snp.makeConstraints { make in
            make.trailing.equalToSuperview().offset(-16)
            make.centerY.equalToSuperview()
        }
        
        let nameRow = UIView()
        contentView.addSubview(nameRow)
        nameRow.snp.makeConstraints { make in
            make.leading.equalTo(avatarView.snp.trailing).offset(12)
            make.trailing.lessThanOrEqualTo(timeLabel.snp.leading).offset(-12)
            make.centerY.equalToSuperview()
        }
        
        nameLabel.font = .heading_h4
        nameLabel.textColor = .text_1
        nameRow.addSubview(nameLabel)
        nameLabel.snp.makeConstraints { make in
            make.leading.top.bottom.equalToSuperview()
        }
        
        genderView.contentMode = .scaleAspectFit
        nameRow.addSubview(genderView)
        genderView.snp.makeConstraints { make in
            make.leading.equalTo(nameLabel.snp.trailing).offset(2)
            make.centerY.equalTo(nameLabel)
            make.trailing.lessThanOrEqualToSuperview()
            make.width.height.equalTo(14)
        }
        
        separator.backgroundColor = .fill.withAlphaComponent(0.08)
        contentView.addSubview(separator)
        separator.snp.makeConstraints { make in
            make.horizontalEdges.equalToSuperview().inset(16)
            make.bottom.equalToSuperview()
            make.height.equalTo(0.5)
        }
    }
}
