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

import Foundation
import UIKit
import SnapKit


class LNOrderRoomBottomMenuView: UIView {
    private let messageInput = LNRoomMessageInputView()
    
    private let micButton = LNRoomBottomMicView()
    private let giftButton = UIButton()
    private let menuButton = UIButton()
    private let joinButton = LNOrderRoomJoinMenuView()
    
    private weak var roomSession: LNOrderRoomViewModel?
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        setupViews()
        LNEventDeliver.addObserver(self)
    }
    
    func update(_ room: LNOrderRoomViewModel?) {
        roomSession = room
        
        joinButton.update(room)
        messageInput.update(room)
        micButton.update(room)
        
        if let room {
            onRoomSeatsChanged(session: room, changed: room.seatsInfo)
        }
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}

extension LNOrderRoomBottomMenuView: LNRoomViewModelNotify {
    func onRoomInfoChanged(session: LNRoomSession, roomInfo: LNRoomInfo) {
        guard roomSession == session else { return }
        checkMenu()
    }
    
    func onRoomSeatsChanged(session: LNRoomSession, changed: [LNRoomSeatItem]) {
        guard roomSession == session else { return }
        checkMenu()
    }
    
    private func checkMenu() {
        menuButton.isHidden = roomSession?.mySeatInfo?.index != LNOrderRoomSeatNum.host.rawValue // 非主播
        && roomSession?.roomInfo.owner.isMyUid != true // 非房主
    }
}

extension LNOrderRoomBottomMenuView {
    private func setupViews() {
        snp.makeConstraints { make in
            make.height.equalTo(50)
        }
        
        let menuView = buildMenuView()
        addSubview(menuView)
        menuView.snp.makeConstraints { make in
            make.trailing.equalToSuperview().offset(-10)
            make.centerY.equalToSuperview()
        }
        
        let input = buildMessageInput()
        addSubview(input)
        input.snp.makeConstraints { make in
            make.centerY.equalToSuperview()
            make.leading.equalToSuperview().offset(10)
        }
    }
    
    private func buildMenuView() -> UIView {
        let stackView = UIStackView()
        stackView.axis = .horizontal
        stackView.spacing = 8
        stackView.snp.makeConstraints { make in
            make.height.equalTo(32)
        }
        
        stackView.addArrangedSubview(micButton)
        micButton.snp.makeConstraints { make in
            make.width.height.equalTo(32)
        }
        
        giftButton.isHidden = false
        giftButton.setBackgroundImage(.icGift, for: .normal)
        giftButton.addAction(UIAction(handler: { [weak self] _ in
            guard let self else { return }
            let panel = LNRoomGiftPanel()
            panel.update(roomSession)
            panel.popup(self)
        }), for: .touchUpInside)
        stackView.addArrangedSubview(giftButton)
        giftButton.snp.makeConstraints { make in
            make.width.height.equalTo(32)
        }
        
        menuButton.isHidden = true
        menuButton.setBackgroundImage(.icMoreWithBg, for: .normal)
        menuButton.addAction(UIAction(handler: { [weak self] _ in
            guard let self else { return }
            let panel = LNOrderRoomSettingMenuPanel()
            panel.update(roomSession)
            panel.popup(self)
        }), for: .touchUpInside)
        stackView.addArrangedSubview(menuButton)
        menuButton.snp.makeConstraints { make in
            make.width.height.equalTo(32)
        }
        
        stackView.addArrangedSubview(joinButton)
        
        return stackView
    }
    
    private func buildMessageInput() -> UIView {
        return messageInput
    }
}
