//
//  MainTabView.swift
//  forShip
//
//  Created by Mamoru Sugita on 2023/10/13.
//

import SwiftUI

enum Tab: String, CaseIterable{
    case map = "tab_map"
    case chat = "tab_chat"
    case alert = "tab_notification"
    case menu = "tab_menu"

    var title: String{
        switch self {
        case .map: return "Map"
        case .chat: return "Chat"
        case .alert: return "Alert"
        case .menu: return "Menu"
        }
    }
}

struct MainTabView: View {
    @EnvironmentObject var selectedTabModel: SelectedTabModel
    @EnvironmentObject private var sceneDelegate: SceneDelegate
    @ObservedObject var location = SharingData.location
    @State private var offset = CGSize.zero
    @State var isSignout = false

    var isTabWindowActive: Bool {
        sceneDelegate.tabWindow != nil
    }

    var isTaskSel: Bool {
        selectedTabModel.activeTab == .map
    }

    var body: some View {
        Group {
            if UIDevice.current.userInterfaceIdiom == .phone {
                phoneView
            } else {
                padView
            }
        }
        .onAppear {
            SharingData.location.focusOwnShip = true
            configureTabBarAppearance()
        }
    }

    private var phoneView: some View {
        ZStack(alignment: .bottom) {
            TabView(selection: $selectedTabModel.activeTab) {
                mapTab
                if SharingData.my.isCommunication {
                    ChatView().tag(Tab.chat)
                }
                NotificationView().tag(Tab.alert)
                MenuView(isSignout: $isSignout).tag(Tab.menu)
            }
            .sheet(isPresented: .constant(isTaskSel && isTabWindowActive)) {
                MenuTaskView()
                    .zIndex(0)
                    .presentationDragIndicator(.hidden)
                    .presentationDetents([.height(150), .medium, .fraction(0.99)])
                    .presentationCornerRadius(15)
                    .presentationBackgroundInteraction(.enabled(upThrough: .medium))
                    .presentationBackground(ColorSet.BackgroundSecondary.color)
                    .interactiveDismissDisabled()
            }
        }
    }

    private var padView: some View {
        ZStack(alignment: .bottom) {
            TabView(selection: $selectedTabModel.activeTab) {
                GeometryReader { geometry in
                    mapTab.padding(.bottom, geometry.safeAreaInsets.bottom + 10)
                }
                if SharingData.my.isCommunication {
                    ChatView().padding(.bottom, 50).tag(Tab.chat)
                }
                NotificationView().padding(.bottom, 50).tag(Tab.alert)
                MenuView(isSignout: $isSignout).padding(.bottom, 50).tag(Tab.menu)
            }
            .tabViewStyle(PageTabViewStyle(indexDisplayMode: .never))
        }
    }

    @ViewBuilder
    private var mapTab: some View {
        ZStack {
            MapRepresentable()
            MapInformation().zIndex(1)
        }
        .ignoresSafeArea(edges: .top)
        .tag(Tab.map)
    }

    private func configureTabBarAppearance() {
        let appearance = UITabBarAppearance()
        appearance.backgroundColor = .clear
        UITabBar.appearance().scrollEdgeAppearance = appearance
        UITabBar.appearance().standardAppearance = appearance
    }
}

#Preview {
    MainTabView()
        .environmentObject(SelectedTabModel())
        .environmentObject(SceneDelegate())
}
