//
//  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
    
    var selectedTabModel = SelectedTabModel()
    var body: some View {
        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)
        })
    }
    
    ///ログイン認証が必要かチェック
    private static func LoginCheck() -> Bool {
        let lastUnixTime = Preferences.lastLoginDate_Int64 ?? 0
        let lastDate = DateTextLib.UnixTime2Date(lastUnixTime)
        
        let interval = Date.now.timeIntervalSince(lastDate)
        return interval < 2592000
    }
}

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