import SwiftUI
import Foundation
import UIKit
import Combine

class CustomTabBarViewModel: ObservableObject {
    @Published var chatBadgeViewModel = NotificationBadgeViewModel()
    @Published var alertBadgeViewModel = NotificationBadgeViewModel()
    @Published var isMenuTaskViewVisible = false
    @Published var isSignalrRestart = false
    @Published var isModeAlert = false

    private let messageService = GetMessage()

    func handleTabSelection(
        selectedTabModel: SelectedTabModel,
        tab: Tab,
        message: SharingData.Message,
        history: SharingData.PushHistory,
        location: SharingData.Location
    ) {
        selectedTabModel.activeTab = tab

        switch tab {
        case .map:
            selectedTabModel.isPoppver.toggle()
            location.focusOwnShip = true
            if UIDevice.current.userInterfaceIdiom == .pad {
                isMenuTaskViewVisible.toggle()
                updateMapMenuVisibility()
            }

        case .chat:
            messageService.start()
            messageService.readNotification()
            messageService.checkUnreadMessages()

            message.viewCnt = 0
            chatBadgeViewModel.resetBadge()
            resetPopoverAndMenu(selectedTabModel: selectedTabModel)

        case .alert:
            history.viewCnt = 0
            alertBadgeViewModel.resetBadge()
            resetPopoverAndMenu(selectedTabModel: selectedTabModel)

        default:
            resetPopoverAndMenu(selectedTabModel: selectedTabModel)
        }
    }

    private func resetPopoverAndMenu(selectedTabModel: SelectedTabModel) {
        selectedTabModel.isPoppver = false
        if UIDevice.current.userInterfaceIdiom == .pad {
            isMenuTaskViewVisible = false
            updateMapMenuVisibility()
        }
    }

    //MARK: - マップメニュー表示の切り替え
    private func updateMapMenuVisibility() {
        let screenWidth = UIScreen.main.bounds.width
        let screenHeight = UIScreen.main.bounds.height
        let menuWidth = min(400, screenWidth * 0.3)
        let menuHeight = screenHeight * 0.8

        if isMenuTaskViewVisible {
            MenuWindowManager.shared.showMenuView(
                MenuTaskView()
                    .frame(width: menuWidth, height: menuHeight)
                    .background(ColorSet.BackgroundSecondary.color)
                    .cornerRadius(15),
                frame: CGRect(x: 5, y: screenHeight * 0.12, width: menuWidth, height: menuHeight)
            )
        } else {
            MenuWindowManager.shared.hideMenuView()
        }
    }
}

class MenuWindowManager {
    static let shared = MenuWindowManager()
    private var window: UIWindow?

    func showMenuView<Content: View>(_ content: Content, frame: CGRect) {
        guard window == nil else { return }

        let hostingController = UIHostingController(rootView: content)
        if let windowScene = UIApplication.shared.connectedScenes.first as? UIWindowScene {
            var rFrame = frame
            rFrame.size.height -= 20

            let newWindow = UIWindow(windowScene: windowScene)
            newWindow.rootViewController = hostingController
            newWindow.windowLevel = .alert + 1
            newWindow.backgroundColor = UIColor.clear
            newWindow.makeKeyAndVisible()
            newWindow.alpha = 0.8
            newWindow.frame = rFrame

            newWindow.layer.cornerRadius = 15
            newWindow.layer.masksToBounds = true

            self.window = newWindow
        }
    }

    func hideMenuView() {
        guard let window = window else { return }

        window.isHidden = true
        window.rootViewController = nil
        self.window = nil
    }
}
