//
//  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)
    
    init(addItemHeaderClosure: @escaping ((Mood, Date) -> Void)) {
        self.addItemHeaderClosure = addItemHeaderClosure
    }
    
    var body: some View {
        ZStack {
            Color(theme.currentTheme.secondaryBGColor)
            
            VStack {
                Text(ShowBasedOnVoteLogics.getVotingTitle())
                    .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.currentTheme.secondaryBGColor)
        )
        .cornerRadius(10, corners: [.topLeft, .topRight, .bottomLeft, .bottomRight])
        .frame(minHeight: 88, maxHeight: 150)
        .frame(minWidth: 0, maxWidth: .infinity)
    }
    
    private func addItem(withMood mood: Mood) {
        if let date = ShowBasedOnVoteLogics.dateForHeaderVote() {
            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)
        }
    }
}
