//
//  AddMoodHeaderView.swift
//  Feels
//
//  Created by Trey Tartt on 1/5/22.
//

import Foundation

import SwiftUI
import CoreData

struct AddMoodHeaderView: View {
    private let savedOnboardingData = UserDefaultsStore.getOnboarding()
    @AppStorage(UserDefaultsStore.Keys.theme.rawValue, store: GroupUserDefaults.groupDefaults) private var theme: Theme = .system
    
    let addItemHeaderClosure: ((Mood, Date) -> Void)
    
    var overrideDay: DayOptions?
    
    var body: some View {
        ZStack {
            Color(theme.secondaryBGColor)
            
            VStack {
                Text(self.getTitle())
                    .font(.title)
                    .foregroundColor(Color(UIColor.label))
                    .padding()
                HStack{
                    ForEach(Mood.allValues) { mood in
                        VStack {
                            Button(action: {
                                addItem(withMood: mood)
                            }, label: {
                                mood.icon
                                    .resizable()
                                    .frame(width: CGFloat(50), height: CGFloat(50), alignment: .center)
                                    .foregroundColor(mood.color)
                            })
                            
                            //Text(mood.strValue)
                        }.frame(minWidth: 0, maxWidth: .infinity)
                    }
                }
            }
            .padding([.leading, .trailing, .bottom])
        }
        .background(
            Color(theme.secondaryBGColor)
        )
        .cornerRadius(10, corners: [.topLeft, .topRight, .bottomLeft, .bottomRight])
        .frame(minHeight: 88, maxHeight: 150)
        .frame(minWidth: 0, maxWidth: .infinity)
    }
    
    private func getTitle() -> String {
        if let overrideDay = overrideDay {
            switch overrideDay {
            case .Today:
                return String(localized: "add_mood_header_view_title_today")
            case .Previous:
                return String(localized: "add_mood_header_view_title_yesterday")
            }
        } else {
            switch savedOnboardingData.inputDay {
            case .Today:
                return String(localized: "add_mood_header_view_title_today")
            case .Previous:
                return String(localized: "add_mood_header_view_title_yesterday")
            }
        }
    }
    
    private func addItem(withMood mood: Mood) {
        if let overrideDay = overrideDay {
            switch overrideDay {
            case .Today:
                addItemHeaderClosure(mood, Date())
            case .Previous:
                let date = Calendar.current.date(byAdding: .day, value: -1, to: Date())!
                addItemHeaderClosure(mood, date)
            }
        } else {
            switch savedOnboardingData.inputDay {
            case .Today:
                addItemHeaderClosure(mood, Date())
            case .Previous:
                let date = Calendar.current.date(byAdding: .day, value: -1, to: Date())!
                addItemHeaderClosure(mood, date)
            }
        }
    }
}

struct AddMoodHeaderView_Previews: PreviewProvider {
    static var previews: some View {
        Group {
            AddMoodHeaderView(addItemHeaderClosure: { (_,_) in
                
            }).environment(\.managedObjectContext, PersistenceController.shared.viewContext)
            
            AddMoodHeaderView(addItemHeaderClosure: { (_,_) in
                
            }).preferredColorScheme(.dark).environment(\.managedObjectContext, PersistenceController.shared.viewContext)
            
            AddMoodHeaderView(addItemHeaderClosure: { (_,_) in
                
            }).environment(\.managedObjectContext, PersistenceController.shared.viewContext)
            
            AddMoodHeaderView(addItemHeaderClosure: { (_,_) in
                
            }).preferredColorScheme(.dark).environment(\.managedObjectContext, PersistenceController.shared.viewContext)
        }
    }
}
