//
//  LNOrderRoomManageSeatCell.swift
//  Gami
//
//  Created by OneeChan on 2026/3/13.
//

import Foundation
import UIKit
import SnapKit


class LNOrderRoomManageSeatCell: UITableViewCell {
    private let indexLabel = UILabel()
    private let avatarView = UIImageView()
    private let nameLabel = UILabel()
    private let genderView = UIImageView()
    private let timeLabel = UILabel()
    private let acceptButton = UIButton()
    
    private weak var roomSession: LNOrderRoomViewModel?
    private var curItem: LNRoomMicApplyPageVO?
    
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        
        setupViews()
    }
    
    func update(room: LNOrderRoomViewModel?, 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
        
        roomSession = room
        curItem = item
        
        updateAcceptButton()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}

private extension LNOrderRoomManageSeatCell {
    func updateAcceptButton() {
        if curItem?.hasAccept == true {
            acceptButton.setBackgroundImage(nil, for: .normal)
            acceptButton.setTitleColor(.fill_7, for: .normal)
            acceptButton.titleLabel?.font = .body_xs
        } else {
            acceptButton.setBackgroundImage(.primary_8, for: .normal)
            acceptButton.setTitleColor(.text_1, for: .normal)
            acceptButton.titleLabel?.font = .heading_h5
        }
    }
    
    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)
        }
        
        acceptButton.backgroundColor = .white.withAlphaComponent(0.2)
        acceptButton.setBackgroundImage(.primary_8, for: .normal)
        acceptButton.setTitle(.init(key: "A00083"), for: .normal)
        acceptButton.setTitleColor(.text_1, for: .normal)
        acceptButton.titleLabel?.font = .heading_h5
        acceptButton.layer.cornerRadius = 11
        acceptButton.clipsToBounds = true
        acceptButton.contentEdgeInsets = .init(top: 0, left: 7, bottom: 0, right: 7)
        acceptButton.addAction(UIAction(handler: { [weak self] _ in
            guard let self else { return }
            guard let curItem else { return }
            roomSession?.acceptSeatApply(applyId: curItem.applyId) { [weak self] success in
                guard let self else { return }
                guard success else { return }
                curItem.hasAccept = true
                updateAcceptButton()
            }
        }), for: .touchUpInside)
        contentView.addSubview(acceptButton)
        acceptButton.snp.makeConstraints { make in
            make.trailing.equalToSuperview().offset(-16)
            make.centerY.equalToSuperview()
            make.width.greaterThanOrEqualTo(56)
            make.height.equalTo(22)
        }
        
        let infoView = UIView()
        contentView.addSubview(infoView)
        infoView.snp.makeConstraints { make in
            make.leading.equalTo(avatarView.snp.trailing).offset(12)
            make.trailing.lessThanOrEqualTo(acceptButton.snp.leading).offset(-12)
            make.centerY.equalToSuperview()
        }
        
        let nameRow = UIView()
        infoView.addSubview(nameRow)
        nameRow.snp.makeConstraints { make in
            make.top.leading.trailing.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)
        }
        
        timeLabel.font = .body_s
        timeLabel.textColor = .text_2
        infoView.addSubview(timeLabel)
        timeLabel.snp.makeConstraints { make in
            make.leading.equalToSuperview()
            make.top.equalTo(nameRow.snp.bottom).offset(4)
            make.bottom.equalToSuperview()
            make.trailing.lessThanOrEqualToSuperview()
        }
        
        let separator = UIView()
        separator.backgroundColor = .fill.withAlphaComponent(0.08)
        contentView.addSubview(separator)
        separator.snp.makeConstraints { make in
            make.leading.equalTo(avatarView.snp.leading)
            make.trailing.equalToSuperview()
            make.bottom.equalToSuperview()
            make.height.equalTo(0.5)
        }
    }
}
