//
//  LNOrderRoomViewController.swift
//  Gami
//
//  Created by OneeChan on 2026/3/9.
//

import Foundation
import UIKit
import SnapKit


extension UIView {
    func pushToRoom(_ id: String) {
        guard let navigationController else { return }
        if LNRoomManager.shared.curRoom?.roomId == id,
            let liveVC = navigationController.viewControllers.first(where: { $0 is LNOrderRoomViewController }) {
            navigationController.popToViewController(liveVC, animated: true)
            return
        }
        
        let index = navigationController.viewControllers.firstIndex(where: { $0 is LNOrderRoomViewController })
        
        showLoading()
        LNRoomManager.shared.leaveRoom { success in
            guard success else {
                dismissLoading()
                return
            }
            LNRoomManager.shared.joinRoom(roomId: id) { roomId in
                dismissLoading()
                guard let roomId else { return }
                if let index {
                    var viewControllers = Array(navigationController.viewControllers[0..<index])
                    viewControllers.append(LNOrderRoomViewController(roomId))
                    navigationController.setViewControllers(viewControllers, animated: true)
                } else {
                    let vc = LNOrderRoomViewController(roomId)
                    navigationController.pushViewController(vc, animated: true)
                }
            }
        }
    }
}

class LNOrderRoomViewController: LNViewController {
    private let viewModel: LNOrderRoomViewModel
    private let bg = UIImageView()
    private let topMenuView = LNRoomTopMenuView()
    private let bottomMenuView = LNOrderRoomBottomMenuView()
    private let seatsView = LNOrderRoomSeatsView()
    private let messageView = LNRoomMessageView()
    
    init(_ id: String) {
        viewModel = LNOrderRoomViewModel(roomId: id)
        super.init(nibName: nil, bundle: nil)
        
        LNEventDeliver.addObserver(self)
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        setupViews()
    }
}

extension LNOrderRoomViewController: LNRoomViewModelNotify {
    func onRoomClosed(session: LNRoomSession) {
        guard session == viewModel else{ return }
        if view.window != nil {
            navigationController?.popViewController(animated: true)
        } else if let index = navigationController?.viewControllers.firstIndex(of: self) {
            navigationController?.viewControllers.remove(at: index)
        }
    }
}

extension LNOrderRoomViewController {
    private func setupViews() {
        enableDragBack = false
        showNavigationBar = false
        isIdleTimerDisabled = true
        
        bg.image = .icRoomBg
        view.addSubview(bg)
        bg.snp.makeConstraints { make in
            make.edges.equalToSuperview()
        }
        
        topMenuView.update(viewModel)
        view.addSubview(topMenuView)
        topMenuView.snp.makeConstraints { make in
            make.horizontalEdges.equalToSuperview()
            make.top.equalTo(view.safeAreaLayoutGuide.snp.top)
        }
        
        seatsView.update(viewModel)
        view.addSubview(seatsView)
        seatsView.snp.makeConstraints { make in
            make.top.equalTo(topMenuView.snp.bottom).offset(24)
            make.horizontalEdges.equalToSuperview()
        }
        
        bottomMenuView.update(viewModel)
        view.addSubview(bottomMenuView)
        bottomMenuView.snp.makeConstraints { make in
            make.horizontalEdges.equalToSuperview()
            make.bottom.equalToSuperview().offset(view.commonBottomInset)
        }
        
        messageView.update(viewModel)
        view.addSubview(messageView)
        messageView.snp.makeConstraints { make in
            make.leading.equalToSuperview().offset(10)
            make.trailing.equalToSuperview().offset(-85)
            make.bottom.equalTo(bottomMenuView.snp.top).offset(-5)
            make.top.equalTo(seatsView.snp.bottom).offset(22)
        }
    }
}
