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

import SwiftUI

enum Tab: String, CaseIterable{
#if CANARY
    case chat = "tab_chat"
#endif
    case task = "tab_task"
    case alert = "tab_notification"
    case menu = "tab_menu"
    
    var title: String{
        ""
    }
}

struct MainTabView: View {
    @EnvironmentObject var selectedTabModel: SelectedTabModel
    @EnvironmentObject private var sceneDelegate: SceneDelegate
    @State var isSignout = false
    @State var isLogin = false

    init() {
        let appearance: UITabBarAppearance = UITabBarAppearance()
        appearance.backgroundColor = .clear
        UITabBar.appearance().scrollEdgeAppearance = appearance
        UITabBar.appearance().standardAppearance = appearance
    }
    
    var body: some View {
        @State var isTabShow: Bool = sceneDelegate.tabWindow != nil
        @State var isTaskSel: Bool = selectedTabModel.activeTab == .task
        TabView(selection: $selectedTabModel.activeTab){
#if CANARY
            ChatView()
                .tag(Tab.chat)
#endif
            MapRepresentable()
                .ignoresSafeArea()
                .tag(Tab.task)
            
            NotificationView()
                .tag(Tab.alert)
            
            MenuView(isSignout: $isSignout)
                .tag(Tab.menu)
        }
        .hideNativeTabBar()
        .sheet(isPresented: .constant(isTaskSel && isTabShow), content: {
            MapTaskView()
            .zIndex(0)
            .presentationDragIndicator(.hidden)
            .presentationDetents([.height(150), .medium, .fraction(0.99)])
            .presentationCornerRadius(15)
            .presentationBackgroundInteraction(.enabled(upThrough: .medium))
            .presentationBackground(ColorSet.BackgroundSecondary.color)
            .interactiveDismissDisabled()
        })
    }
}

struct CustomTabBar: View {
    @EnvironmentObject private var selectedTabModel: SelectedTabModel
    @State var isLocationAlert = false
    @Environment(\.openURL) var openURL
    @ObservedObject var my = SharingData.my

    var body: some View {
        VStack(spacing: 0){
            Divider()
            
            HStack(spacing: 0){
                ForEach(Tab.allCases, id: \.rawValue) { tab in
                    Button(action: {
                        selectedTabModel.activeTab = tab
                    }, label: {
                        VStack{
                            Image(selectedTabModel.activeTab == tab ? tab.rawValue + "_selected" : tab.rawValue)
                                .font(.title2)
                            
                            Text(tab.title)
                                .font(.caption)

                        }
                        .frame(maxWidth: .infinity, maxHeight: .infinity)
                        .contentShape(.rect)
                    })
                        
                }
            }
            .frame(height: 50)
        }
        .background(ColorSet.BottomNav.color)
        .alert("", isPresented: $selectedTabModel.isShowChangeEmrMode) {
            Button("Yes"){
                SharingData.message.changeMode()
                let signalRService = SignalR()
                signalRService.chatMode(mode: SharingData.message.mode)
            }
            Button("No"){
                
            }
        } message: {
            Text("Do you change an emargency mode?")
        }
        .alert("Location", isPresented: $selectedTabModel.isLocationAlert) {
            Button("OK") {
            }
        } message: {
            Text("To use ECA,set the permission to use location information to ALWAYS.")
        }
        .alert("Update", isPresented: $my.isUpDate) {
            Button("Go to app page"){
                openURL(URL(string: HttpRequestType.AppStore.rawValue)!)
            }
        } message: {
            Text("A new version of this app is available.")
        }
    }
}

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