//
//  DayFilterPickerView.swift
//  Feels (iOS)
//
//  Created by Trey Tartt on 4/2/22.
//

import SwiftUI

struct DayFilterPickerView: View {
    @AppStorage(UserDefaultsStore.Keys.theme.rawValue, store: GroupUserDefaults.groupDefaults) private var theme: Theme = .system
    @AppStorage(UserDefaultsStore.Keys.textColor.rawValue, store: GroupUserDefaults.groupDefaults) private var textColor: Color = DefaultTextColor.textColor
    @StateObject private var filteredDays = DaysFilterClass.shared
    
    let weekdays = [(Calendar.current.shortWeekdaySymbols[0], 1),
                    (Calendar.current.shortWeekdaySymbols[1], 2),
                    (Calendar.current.shortWeekdaySymbols[2], 3),
                    (Calendar.current.shortWeekdaySymbols[3], 4),
                    (Calendar.current.shortWeekdaySymbols[4], 5),
                    (Calendar.current.shortWeekdaySymbols[5], 6),
                    (Calendar.current.shortWeekdaySymbols[6], 7)]
    
    var body: some View {
        ZStack {
            theme.currentTheme.secondaryBGColor
            
            VStack {
                HStack {
                    ForEach(weekdays.indices, id: \.self) { dayIdx in
                        let day = String(weekdays[dayIdx].0)
                        let value = weekdays[dayIdx].1
                        Button(day.capitalized, action: {
                            if filteredDays.currentFilters.contains(value) {
                                filteredDays.removeFilter(filter: value)
                            } else {
                                filteredDays.addFilter(newFilter: value)
                            }
                            let impactMed = UIImpactFeedbackGenerator(style: .heavy)
                            impactMed.impactOccurred()
                        })
                        .frame(maxWidth: .infinity)
                        .foregroundColor(filteredDays.currentFilters.contains(value) ? .green : .red)
                    }
                }
                Text(String(localized: "day_picker_view_text"))
                    .padding(.top)
                    .foregroundColor(textColor)
            }
            .padding()
        }
        .fixedSize(horizontal: false, vertical: true)
        .cornerRadius(Constants.viewsCornerRaidus, corners: [.topLeft, .topRight, .bottomLeft, .bottomRight])
    }
}

struct DayFilterPickerView_Previews: PreviewProvider {
    static var previews: some View {
        DayFilterPickerView()
    }
}
