//
//  LNOrderRoomApplySeatPanel.swift
//  Gami
//
//  Created by OneeChan on 2026/3/11.
//

import Foundation
import UIKit
import SnapKit


class LNOrderRoomApplySeatPanel: LNPopupView {
    private let titleLabel = UILabel()
    private let avatarView = UIImageView()
    private let nameLabel = UILabel()
    private let applyButton = UIButton()
    
    private weak var roomSession: LNRoomSession?
    private var targetSeat: LNOrderRoomSeatNum?
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        setupViews()
    }
    
    func update(_ room: LNRoomSession?, seat: LNOrderRoomSeatNum? = nil) {
        roomSession = room
        targetSeat = seat
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}

extension LNOrderRoomApplySeatPanel {
    private func setupViews() {
        container.backgroundColor = .fill_7
        
        let header = buildHeader()
        container.addSubview(header)
        header.snp.makeConstraints { make in
            make.top.horizontalEdges.equalToSuperview()
            make.height.equalTo(52)
        }
        
        avatarView.sd_setImage(with: URL(string: myUserInfo.avatar))
        avatarView.clipsToBounds = true
        avatarView.contentMode = .scaleAspectFill
        avatarView.layer.cornerRadius = 38
        avatarView.layer.borderWidth = 1
        avatarView.layer.borderColor = .fill
        container.addSubview(avatarView)
        avatarView.snp.makeConstraints { make in
            make.top.equalTo(header.snp.bottom).offset(20)
            make.centerX.equalToSuperview()
            make.width.height.equalTo(76)
        }
        
        nameLabel.text = myUserInfo.nickname
        nameLabel.font = .heading_h2
        nameLabel.textColor = .text_1
        nameLabel.textAlignment = .center
        container.addSubview(nameLabel)
        nameLabel.snp.makeConstraints { make in
            make.horizontalEdges.equalToSuperview()
            make.top.equalTo(avatarView.snp.bottom).offset(13)
        }
        
        applyButton.setTitle(.init(key: "A00322"), for: .normal)
        applyButton.setTitleColor(.text_1, for: .normal)
        applyButton.titleLabel?.font = .heading_h3
        applyButton.setBackgroundImage(.primary_8, for: .normal)
        applyButton.layer.cornerRadius = 23.5
        applyButton.clipsToBounds = true
        applyButton.addAction(UIAction(handler: { [weak self] _ in
            guard let self else { return }
            guard let roomSession else { return }
            let seatNum: LNOrderRoomSeatNum = targetSeat ?? (myUserInfo.playmate ? .none : .guest)
            roomSession.applySeat(index: seatNum.rawValue) { [weak self] success in
                guard let self else { return }
                guard success else { return }
                dismiss()
            }
        }), for: .touchUpInside)
        container.addSubview(applyButton)
        applyButton.snp.makeConstraints { make in
            make.horizontalEdges.equalToSuperview().inset(16)
            make.top.equalTo(nameLabel.snp.bottom).offset(46)
            make.height.equalTo(47)
            make.bottom.equalToSuperview().offset(commonBottomInset)
        }
    }
    
    private func buildHeader() -> UIView {
        let header = UIView()
        
        titleLabel.font = .heading_h3
        titleLabel.textColor = .text_1
        titleLabel.text = .init(key: "A00330")
        header.addSubview(titleLabel)
        titleLabel.snp.makeConstraints { make in
            make.center.equalToSuperview()
        }
        
        return header
    }
}
