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

import Foundation
import UIKit
import SnapKit


private enum LNRoomJoinMenuState {
    case normal
    case appling
    case reviewing
    case onMic
}


class LNOrderRoomJoinMenuView: UIView {
    private let joinView = UIStackView()
    private let bgImageView = UIImageView()
    private let seatIc = UIImageView()
    private let titleLabel = UILabel()
    private let redCountView = UIView()
    private let redCountLabel = UILabel()
    
    private var curState: LNRoomJoinMenuState = .normal {
        didSet {
            switch curState {
            case .normal: toBeOffMic()
            case .appling: toBePending()
            case .reviewing: toBeRequests()
            case .onMic: toBeOnMic()
            }
        }
    }
    
    private weak var roomSession: LNOrderRoomViewModel?
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        setupViews()
        toBeOffMic()
        
        LNEventDeliver.addObserver(self)
    }
    
    func update(_ room: LNOrderRoomViewModel?) {
        roomSession = room
        
        if let room {
            onRoomSeatApplyChanged(session: room, applyCount: room.seatApplyCount)
        }
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}

extension LNOrderRoomJoinMenuView: LNOrderRoomViewModelNotify {
    func onRoomSeatsChanged(session: LNRoomSession, changed: [LNRoomSeatItem]) {
        guard roomSession == session else { return }
        checkState()
    }
    
    func onRoomSeatApplyChanged(session: LNRoomSession, applyCount: Int) {
        guard roomSession == session else { return }
        checkState()
    }
    
    func onOrderRoomUserSeatApplyChanged(session: LNOrderRoomViewModel, apply: LNOrderRoomApplyingSeatType) {
        guard roomSession == session else { return }
        checkState()
    }
    
    private func checkState() {
        guard let roomSession else { return }
        
        redCountView.isHidden = true
        
        if roomSession.mySeatInfo == nil { // 还没上麦
            if roomSession.waitingForSeat != .none {
                curState = .appling
                redCountView.isHidden = roomSession.seatApplyCount == 0
            } else {
                curState = .normal
            }
        } else { // 在麦上
            if roomSession.mySeatInfo?.index == LNOrderRoomSeatNum.host.rawValue {
                curState = .reviewing
                redCountView.isHidden = roomSession.seatApplyCount == 0
            } else {
                curState = .onMic
            }
        }
        
        redCountLabel.text = "\(roomSession.seatApplyCount)"
    }
}

extension LNOrderRoomJoinMenuView {
    private func setupViews() {
        LNOrderRoomGuideView.joinView = self

        backgroundColor = .fill.withAlphaComponent(0.15)
        layer.cornerRadius = 15
        onTap { [weak self] in
            guard let self else { return }
            guard let roomSession else { return }
            switch curState {
            case .normal:
                let panel = LNOrderRoomApplySeatPanel()
                panel.update(roomSession)
                panel.popup(self)
            case .appling:
                let panel = LNOrderRoomApplySeatListPanel()
                panel.update(roomSession)
                panel.popup(self)
                break
            case .reviewing:
                let panel = LNOrderRoomManageSeatListPanel()
                panel.update(roomSession)
                panel.popup(self)
                break
            case .onMic:
                roomSession.leaveSeat { _ in }
                break
            }
        }
        snp.makeConstraints { make in
            make.width.greaterThanOrEqualTo(69)
        }
        
        bgImageView.image = .primary_8
        bgImageView.layer.cornerRadius = 15
        bgImageView.clipsToBounds = true
        addSubview(bgImageView)
        bgImageView.snp.makeConstraints { make in
            make.edges.equalToSuperview()
        }
        
        joinView.isUserInteractionEnabled = false
        joinView.spacing = 4
        addSubview(joinView)
        joinView.snp.makeConstraints { make in
            make.centerX.equalToSuperview()
            make.leading.greaterThanOrEqualToSuperview().offset(12)
            make.centerY.equalToSuperview()
        }
        
        seatIc.image = .icSeat
        joinView.addArrangedSubview(seatIc)
        seatIc.snp.makeConstraints { make in
            make.width.height.equalTo(16)
        }
        
        titleLabel.font = .heading_h5
        titleLabel.textColor = .text_1
        joinView.addArrangedSubview(titleLabel)
        
        redCountView.backgroundColor = .fill_6
        redCountView.layer.cornerRadius = 7
        addSubview(redCountView)
        redCountView.snp.makeConstraints { make in
            make.trailing.equalToSuperview()
            make.centerY.equalTo(snp.top)
            make.width.greaterThanOrEqualTo(15)
        }
        
        redCountLabel.font = .body_xs
        redCountLabel.textColor = .text_1
        redCountLabel.textAlignment = .center
        redCountView.addSubview(redCountLabel)
        redCountLabel.snp.makeConstraints { make in
            make.horizontalEdges.equalToSuperview().inset(4)
            make.verticalEdges.equalToSuperview()
        }
    }
    
    private func toBeOffMic() {
        bgImageView.isHidden = false
        seatIc.isHidden = false
        titleLabel.text = .init(key: "A00322")
    }
    
    private func toBePending() {
        bgImageView.isHidden = true
        seatIc.isHidden = true
        titleLabel.text = .init(key: "A00323")
    }
    
    private func toBeRequests() {
        bgImageView.isHidden = true
        seatIc.isHidden = true
        titleLabel.text = .init(key: "A00324")
    }
    
    private func toBeOnMic() {
        bgImageView.isHidden = true
        seatIc.isHidden = true
        titleLabel.text = .init(key: "A00386")
    }
}
