//
//  MainTabView.swift
//  Feels (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
    
    let onboardingData = OnboardingDataDataManager.shared.savedOnboardingData
    
    let dayView: DayView
    let monthView: MonthView
    let yearView: YearView
    let customizeView: CustomizeView
    
    var body: some View {
        Self._printChanges()
        return TabView {
            dayView
                .tabItem {
                    Label(String(localized: "content_view_tab_main"), systemImage: "list.dash")
                }
            
            monthView
                .tabItem {
                    Label(String(localized: "content_view_tab_month"), systemImage: "calendar")
                }
            
           yearView
                .tabItem {
                    Label(String(localized: "content_view_tab_filter"), systemImage: "line.3.horizontal.decrease.circle")
                }
            
            customizeView
                .tabItem {
                    Label(String(localized: "content_view_tab_customize"), systemImage: "pencil")
                }
        }
        .accentColor(moodTint.color(forMood: .average))
        .sheet(isPresented: $needsOnboarding, onDismiss: { }, content: {
            OnboardingMain(onboardingData: onboardingData,
                           updateBoardingDataClosure: { onboardingData in
                needsOnboarding = false
                OnboardingDataDataManager.shared.updateOnboardingData(onboardingData: onboardingData)
            })
        })
        .onAppear(perform: {
            switch theme {
            case .system:
                UIApplication.shared.windows.first?.overrideUserInterfaceStyle = .unspecified
            case .iFeel:
                UIApplication.shared.windows.first?.overrideUserInterfaceStyle = .unspecified
            case .dark:
                UIApplication.shared.windows.first?.overrideUserInterfaceStyle = .dark
            case .light:
                UIApplication.shared.windows.first?.overrideUserInterfaceStyle = .light
            }
        })
        .onChange(of: theme, perform: { value in
            switch theme {
            case .system:
                UIApplication.shared.windows.first?.overrideUserInterfaceStyle = .unspecified
            case .iFeel:
                UIApplication.shared.windows.first?.overrideUserInterfaceStyle = .unspecified
            case .dark:
                UIApplication.shared.windows.first?.overrideUserInterfaceStyle = .dark
            case .light:
                UIApplication.shared.windows.first?.overrideUserInterfaceStyle = .light
            }
        })
    }
}

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()),
                    customizeView: CustomizeView())
    }
}
