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

import SwiftUI

class LoginViewModel: ObservableObject{
    @Published var isLogin: Bool = false
}

struct ContentView: View {
    @StateObject private var loginViewModel = LoginViewModel()
    @EnvironmentObject private var sceneDelegate: SceneDelegate

    let selectedTabModel = SelectedTabModel()

    var body: some View {
        MainTabView()
            .environmentObject(selectedTabModel)
            .environmentObject(loginViewModel)
            .onAppear(){
                if loginViewModel.isLogin{
                    guard sceneDelegate.tabWindow == nil else{ return }
                    sceneDelegate.addTabBar(selectedTabModel)
                    selectedTabModel.activeTab = .task
                }
            }
            .fullScreenCover(isPresented: .constant(!loginViewModel.isLogin), onDismiss: {
                guard sceneDelegate.tabWindow == nil else{
                    sceneDelegate.tabWindow?.isHidden = false
                    return
                }
                sceneDelegate.addTabBar(selectedTabModel)
                selectedTabModel.activeTab = .task
            }, content: {
                LoginView(isLogin: $loginViewModel.isLogin)
                    .environmentObject(selectedTabModel)
            })
    }
}

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