//
//  LNOrderRoomInviteSeatCell.swift
//  Gami
//
//  Created by OneeChan on 2026/3/20.
//

import Foundation
import UIKit
import SnapKit


class LNOrderRoomInviteSeatCell: UITableViewCell {
    private let indexLabel = UILabel()
    private let avatarView = UIImageView()
    private let nameLabel = UILabel()
    private let genderView = UIImageView()
    private let inviteButton = UIButton()
    private let separator = UIView()
    
    private weak var roomSession: LNRoomSession?
    private var seatItem: LNRoomSeatItem?
    private var curItem: LNRoomUserListItemVO?
    
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        
        setupViews()
    }
    
    func update(room: LNRoomSession?, seat: LNRoomSeatItem?, item: LNRoomUserListItemVO, index: Int) {
        indexLabel.text = "\(index)"
        avatarView.showAvatar(item.user.avatar)
        nameLabel.text = item.user.nickname
        
        genderView.image = switch item.user.gender {
        case .unknow: nil
        case .male: .icGenderMale
        case .female: .icGenderFemale
        }
        genderView.isHidden = item.user.gender == .unknow
        
        roomSession = room
        seatItem = seat
        curItem = item
        
        updateInviteButton()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}

private extension LNOrderRoomInviteSeatCell {
    func updateInviteButton() {
        if curItem?.hasInvited == true {
            inviteButton.isUserInteractionEnabled = false
            inviteButton.backgroundColor = .white.withAlphaComponent(0.2)
            inviteButton.setBackgroundImage(nil, for: .normal)
            inviteButton.setTitleColor(.fill_7, for: .normal)
            inviteButton.titleLabel?.font = .body_xs
        } else {
            inviteButton.isUserInteractionEnabled = true
            inviteButton.backgroundColor = .clear
            inviteButton.setBackgroundImage(.primary_8, for: .normal)
            inviteButton.setTitleColor(.text_1, for: .normal)
            inviteButton.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)
        }
        
        inviteButton.backgroundColor = .white.withAlphaComponent(0.2)
        inviteButton.setTitle(.init(key: "A00341"), for: .normal)
        inviteButton.setTitleColor(.text_1, for: .normal)
        inviteButton.titleLabel?.font = .heading_h5
        inviteButton.layer.cornerRadius = 11
        inviteButton.clipsToBounds = true
        inviteButton.contentEdgeInsets = .init(top: 0, left: 7, bottom: 0, right: 7)
        inviteButton.addAction(UIAction(handler: { [weak self] _ in
            guard let self else { return }
            guard let roomSession, let curItem, let seatItem else { return }
            roomSession.inviteUserToSeat(uid: curItem.user.userNo,
                                         index: seatItem.index)
            { [weak self] success in
                guard let self, success else { return }
                curItem.hasInvited = true
                updateInviteButton()
            }
        }), for: .touchUpInside)
        contentView.addSubview(inviteButton)
        inviteButton.snp.makeConstraints { make in
            make.trailing.equalToSuperview().offset(-16)
            make.centerY.equalToSuperview()
            make.width.greaterThanOrEqualTo(56)
            make.height.equalTo(22)
        }
        
        let nameRow = UIView()
        contentView.addSubview(nameRow)
        nameRow.snp.makeConstraints { make in
            make.leading.equalTo(avatarView.snp.trailing).offset(12)
            make.trailing.lessThanOrEqualTo(inviteButton.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)
        }
    }
}
