//
//  HomeView.swift
//  LeCountdown
//
//  Created by Laurent Morvillier on 15/02/2023.
//

import SwiftUI

struct CompactHomeView: View {

    @Environment(\.managedObjectContext) private var viewContext

    @State private var tabSelection: Int = 1
    
    var body: some View {
        
        NavigationStack {
            TabView(selection: $tabSelection) {
                PresetsView(tabSelection: $tabSelection)
                    .environment(\.managedObjectContext, viewContext)
                    .tabItem { Label("Presets", systemImage: "globe") }
                    .tag(0)
                ContentView<AbstractTimer>()
                    .environment(\.managedObjectContext, viewContext)
                    .environmentObject(Conductor.maestro)
                    .tabItem { Label("Home", systemImage: "clock.fill") }
                    .tag(1)
                ActivitiesView()
                    .environment(\.managedObjectContext, viewContext)
                    .tabItem { Label("Stats", systemImage: "chart.bar.fill") }
                    .tag(2)
            }
            .tabViewStyle(.page)
            .onOpenURL { _ in
                self.tabSelection = 1
            }
        }
    }
    
}

struct RegularHomeView: View {
    
    @Environment(\.managedObjectContext) private var viewContext

    @State private var tabSelection: Int = 1
    
    var body: some View {
        
        NavigationStack {
            TabView(selection: $tabSelection) {
                PresetsView(tabSelection: $tabSelection)
                    .environment(\.managedObjectContext, viewContext)
                    .tabItem { Label("Presets", systemImage: "globe") }
                    .tag(0)
                ContentView<AbstractTimer>()
                    .environment(\.managedObjectContext, viewContext)
                    .environmentObject(Conductor.maestro)
                    .tabItem { Label("Home", systemImage: "clock.fill") }
                    .tag(1)
                ActivitiesView()
                    .environment(\.managedObjectContext, viewContext)
                    .tabItem { Label("Stats", systemImage: "chart.bar.fill") }
                    .tag(2)
            }
        }
        .tabViewStyle(.page)
        .onOpenURL { _ in
            self.tabSelection = 1
        }
    }
    
}

struct CompactHomeView_Previews: PreviewProvider {
    static var previews: some View {
        CompactHomeView()
            .environmentObject(Conductor.maestro)
            .environment(\.managedObjectContext, PersistenceController.preview.container.viewContext)

    }
}

struct RegularHomeView_Previews: PreviewProvider {
    static var previews: some View {
        RegularHomeView()
            .environmentObject(Conductor.maestro)
            .environment(\.managedObjectContext, PersistenceController.preview.container.viewContext)

    }
}
