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

import SwiftUI

struct ContentView: View {
    @State var isLogin = false//ContentView.LoginCheck()
    @EnvironmentObject private var sceneDelegate: SceneDelegate
    @StateObject var locationViewModel = LocationViewModel()
    var selectedTabModel = SelectedTabModel()

    var body: some View {
        switch locationViewModel.authorizationStatus {
        case .notDetermined:
            RequestLocationView()
                .environmentObject(locationViewModel)
        case .restricted:
            ErrorView(errorText: "位置情報の使用が制限されています。")
        case .denied:
            ErrorView(errorText: "位置情報を使用できません。")
        case .authorizedAlways, .authorizedWhenInUse:
            NavigationStack{
                MainTabView()
                    .environmentObject(selectedTabModel)
                    .onAppear(){
                        if isLogin{
                            guard sceneDelegate.tabWindow == nil else{ return }
                            sceneDelegate.addTabBar(selectedTabModel)
                            selectedTabModel.activeTab = .task
                        }
                    }
            }
            .fullScreenCover(isPresented: .constant(!isLogin), onDismiss: {
                guard sceneDelegate.tabWindow == nil else{ return }
                sceneDelegate.addTabBar(selectedTabModel)
                selectedTabModel.activeTab = .task
            }, content: {
                LoginView(isLogin: $isLogin)
            })
        default:
            Text("Unexpected status")
        }
    }
}

#Preview {
    ContentView()
        .environmentObject(SceneDelegate())
}
