//
//  LNOrderRoomSettingMenuPanel.swift
//  Gami
//
//  Created by OneeChan on 2026/3/16.
//

import Foundation
import UIKit
import SnapKit


class LNOrderRoomSettingMenuPanel: LNPopupView {
    private var editView: UIView?
    private var closeView: UIView?
    private weak var roomSession: LNRoomSession?
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        setupViews()
        LNEventDeliver.addObserver(self)
    }
    
    func update(_ room: LNRoomSession?) {
        roomSession = room
        
        if let room {
            onRoomSeatsChanged(session: room, changed: room.seatsInfo)
        }
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}

extension LNOrderRoomSettingMenuPanel: LNRoomViewModelNotify {
    func onRoomSeatsChanged(session: LNRoomSession, changed: [LNRoomSeatItem]) {
        guard roomSession == session else { return }
        closeView?.isHidden = roomSession?.roomInfo.owner.isMyUid != true
    }
}

extension LNOrderRoomSettingMenuPanel {
    private func setupViews() {
        container.backgroundColor = .fill_7
        
        let header = buildHeader()
        container.addSubview(header)
        header.snp.makeConstraints { make in
            make.horizontalEdges.top.equalToSuperview()
            make.height.equalTo(52)
        }
        
        let stackView = UIStackView()
        stackView.axis = .horizontal
        stackView.alignment = .top
        stackView.spacing = 2
        stackView.distribution = .fillEqually
        container.addSubview(stackView)
        stackView.snp.makeConstraints { make in
            make.top.equalTo(header.snp.bottom)
            make.leading.equalToSuperview().offset(10)
            make.trailing.lessThanOrEqualToSuperview().offset(-10)
            make.bottom.equalToSuperview().offset(commonBottomInset)
            make.height.equalTo(85)
        }
        
        let editView = buildActionItem(
            icon: .icSettingsRoom,
            title: .init(key: "B00095")
        ) { [weak self] in
            guard let self else { return }
            guard let roomSession else { return }
            dismiss()
            
            let panel = LNRoomInfoEditPanel()
            panel.update(roomSession)
            panel.popup(self)
        }
        stackView.addArrangedSubview(editView)
        self.editView = editView
        
        let closeView = buildActionItem(
            icon: .icShutdown,
            title: .init(key: "A00338")
        ) { [weak self] in
            guard let self else { return }
            guard let roomSession else { return }
            
            dismiss()
            
            LNCommonAlertView.showCloseRoomAlert(roomSession)
        }
        stackView.addArrangedSubview(closeView)
        closeView.isHidden = true
        self.closeView = closeView
    }
    
    private func buildHeader() -> UIView {
        let view = UIView()
        
        let titleLabel = UILabel()
        titleLabel.text = .init(key: "A00337")
        titleLabel.font = .heading_h3
        titleLabel.textColor = .text_1
        view.addSubview(titleLabel)
        titleLabel.snp.makeConstraints { make in
            make.leading.equalToSuperview().offset(16)
            make.centerY.equalToSuperview()
        }
        
        return view
    }
    
    private func buildActionItem(icon: UIImage, title: String, handler: @escaping () -> Void) -> UIView {
        let view = UIView()
        view.snp.makeConstraints { make in
            make.width.equalTo(70)
            make.height.equalTo(85)
        }
        view.onTap(handler)
        
        let buttonHolder = UIView()
        buttonHolder.backgroundColor = UIColor.primary_1.withAlphaComponent(0.24)
        buttonHolder.layer.cornerRadius = 24
        buttonHolder.isUserInteractionEnabled = false
        view.addSubview(buttonHolder)
        buttonHolder.snp.makeConstraints { make in
            make.top.equalToSuperview().offset(1)
            make.centerX.equalToSuperview()
            make.width.height.equalTo(48)
        }
        
        let iconView = UIImageView()
        iconView.image = icon
        iconView.tintColor = .primary_1
        buttonHolder.addSubview(iconView)
        iconView.snp.makeConstraints { make in
            make.center.equalToSuperview()
            make.width.height.equalTo(28)
        }
        
        let titleLabel = UILabel()
        titleLabel.text = title
        titleLabel.font = .body_s
        titleLabel.textColor = .text_2
        titleLabel.textAlignment = .center
        titleLabel.numberOfLines = 2
        view.addSubview(titleLabel)
        titleLabel.snp.makeConstraints { make in
            make.top.equalTo(buttonHolder.snp.bottom).offset(6)
            make.horizontalEdges.equalToSuperview()
        }
        
        return view
    }
}
