//
//  MainTabView.swift
//  Reflect (iOS)
//
//  Created by Trey Tartt on 2/18/22.
//

import SwiftUI

struct MainTabView: View {
    @AppStorage(UserDefaultsStore.Keys.needsOnboarding.rawValue, store: GroupUserDefaults.groupDefaults) private var needsOnboarding = true
    @AppStorage(UserDefaultsStore.Keys.theme.rawValue, store: GroupUserDefaults.groupDefaults) private var theme: Theme = .system
    @AppStorage(UserDefaultsStore.Keys.moodTint.rawValue, store: GroupUserDefaults.groupDefaults) private var moodTint: MoodTints = .Default

    private var textColor: Color { theme.currentTheme.labelColor }
    
    
    let onboardingData = OnboardingDataDataManager.shared.savedOnboardingData
    
    let dayView: DayView
    let monthView: MonthView
    let yearView: YearView
    let insightsView: InsightsView

    var body: some View {
        return TabView {
            dayView
                .tabItem {
                    Label(String(localized: "content_view_tab_main"), systemImage: "list.dash")
                        .accessibilityIdentifier(AccessibilityID.Tab.day)
                }

            monthView
                .tabItem {
                    Label(String(localized: "content_view_tab_month"), systemImage: "calendar")
                        .accessibilityIdentifier(AccessibilityID.Tab.month)
                }

           yearView
                .tabItem {
                    Label(String(localized: "content_view_tab_filter"), systemImage: "line.3.horizontal.decrease.circle")
                        .accessibilityIdentifier(AccessibilityID.Tab.year)
                }

            insightsView
                .tabItem {
                    Label(String(localized: "content_view_tab_insights"), systemImage: "lightbulb.fill")
                        .accessibilityIdentifier(AccessibilityID.Tab.insights)
                }

            SettingsTabView()
                .tabItem {
                    Label("Settings", systemImage: "gear")
                        .accessibilityIdentifier(AccessibilityID.Tab.settings)
                }
        }
        .accentColor(textColor)
        .sheet(isPresented: $needsOnboarding, onDismiss: { }, content: {
            OnboardingMain(onboardingData: onboardingData,
                           updateBoardingDataClosure: { onboardingData in
                needsOnboarding = false
                OnboardingDataDataManager.shared.updateOnboardingData(onboardingData: onboardingData)
            })
        })
        .onAppear(perform: {
            applyTheme(theme)
        })
        .onChange(of: theme) { _, newTheme in
            applyTheme(newTheme)
        }
    }

    private func applyTheme(_ theme: Theme) {
        let style: UIUserInterfaceStyle
        switch theme {
        case .system, .iFeel:
            style = .unspecified
        case .dark:
            style = .dark
        case .light:
            style = .light
        }

        guard let windowScene = UIApplication.shared.connectedScenes.first as? UIWindowScene,
              let window = windowScene.windows.first else { return }
        window.overrideUserInterfaceStyle = style
    }
}

struct MainTabView_Previews: PreviewProvider {
    static var previews: some View {
        MainTabView(dayView: DayView(viewModel: DayViewViewModel(addMonthStartWeekdayPadding: false)),
                    monthView: MonthView(viewModel: DayViewViewModel(addMonthStartWeekdayPadding: true)),
                    yearView: YearView(viewModel: YearViewModel()),
                    insightsView: InsightsView())
    }
}
