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

import SwiftUI
import CoreSpotlight
import Combine

struct CompactHomeView: View {

    @Environment(\.managedObjectContext) private var viewContext

    @FetchRequest(
        sortDescriptors: [NSSortDescriptor(keyPath: \AbstractTimer.order, ascending: true)],
        animation: .default)
    private var timers: FetchedResults<AbstractTimer>

    @State private var tabSelection: Int = 0
    
    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)
            }
            .navigationBarTitleDisplayMode(NavigationBarItem.TitleDisplayMode.large)
            .tabViewStyle(.page(indexDisplayMode: .never))
        }
        .onAppear {
            if self.timers.count > 0 {
                self.tabSelection = 1
            }
        }
        .onOpenURL { _ in
            self.tabSelection = 1
        }
    }
    
}

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

    @State private var tabSelection: Int = 1
    
    @State private var columnVisibility: NavigationSplitViewVisibility = .detailOnly
    
    var body: some View {
        
        NavigationSplitView(columnVisibility: $columnVisibility) {
            PresetsView(tabSelection: $tabSelection)
                .environment(\.managedObjectContext, viewContext)
                .tabItem { Label("Presets", systemImage: "globe") }
                .tag(0)
        } detail: {
            NavigationStack {
                TabView(selection: $tabSelection) {
                    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(indexDisplayMode: .never))
            }
        }
        .onAppear {
            self._setColumnVisibility()
        }
        .onContinueUserActivity(Shortcut.newCountdown.rawValue, perform: _handleNewCountdown)
        .onOpenURL { _ in
            self.tabSelection = 1
        }
    }
    
    fileprivate func _setColumnVisibility() {
        do {
            let count = try viewContext.count(entityName: "AbstractTimer")
            if count > 0 {
                self.columnVisibility = .detailOnly
            } else {
                self.columnVisibility = .automatic
            }
        } catch {
            Logger.error(error)
        }
    }
    
    fileprivate func _handleNewCountdown(_ userActivity: NSUserActivity) {
        print(">>> new countdown")
    }
    
}

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)

    }
}
