import SwiftUI
import SwiftData

private enum MainScreenSelection: Int {
    case unlock = 0
    case settings = 1
    case history = 2
}

@main
struct SesameWatchApp: App {
    
    @State
    var modelContainer: ModelContainer
    
    @ObservedObject
    var coordinator: RequestCoordinator
    
    let keyManagement = KeyManagement()

    @State
    private var selectedScreen: MainScreenSelection = .unlock

    @State
    var didLaunchFromComplication = false
    
    init() {
        let modelContainer = SesameWatchApp.loadModelContainer()
        self.modelContainer = modelContainer
        self.coordinator = .init(modelContext: modelContainer.mainContext)
    }
    
    var body: some Scene {
        WindowGroup {
            TabView(selection: $selectedScreen) {
                ContentView(coordinator: coordinator, didLaunchFromComplication: $didLaunchFromComplication)
                    .tag(MainScreenSelection.unlock)
                SettingsView()
                    .environmentObject(keyManagement)
                    .tag(MainScreenSelection.settings)
                HistoryView()
                    .tag(MainScreenSelection.history)
            }
            .tabViewStyle(PageTabViewStyle())
            .onOpenURL { url in
                didLaunchFromComplication = true
                selectedScreen = .unlock
            }
        }
        .modelContainer(modelContainer)
    }
}
