//
//  MonthDetailView.swift
//  Feels (iOS)
//
//  Created by Trey Tartt on 2/18/22.
//

import SwiftUI

struct MonthDetailView: View {
    @AppStorage(UserDefaultsStore.Keys.theme.rawValue, store: GroupUserDefaults.groupDefaults) private var theme: Theme = .system
    @AppStorage(UserDefaultsStore.Keys.deleteEnable.rawValue, store: GroupUserDefaults.groupDefaults) private var deleteEnabled = true

    @State private var showingUpdateEntryAlert = false
    
    let monthInt: Int
    let yearInt: Int
    let entries: [MoodEntry]
    
    lazy var dateFormatter: DateFormatter = {
        let dateFormatter = DateFormatter()
        dateFormatter.dateFormat = "dd"
        return dateFormatter
    }()
    
    let columns = [
        GridItem(.flexible(minimum: 5, maximum: 50)),
        GridItem(.flexible(minimum: 5, maximum: 50)),
        GridItem(.flexible(minimum: 5, maximum: 50)),
        GridItem(.flexible(minimum: 5, maximum: 50)),
        GridItem(.flexible(minimum: 5, maximum: 50)),
        GridItem(.flexible(minimum: 5, maximum: 50)),
        GridItem(.flexible(minimum: 5, maximum: 50))
    ]
    
    var body: some View {
        VStack {
            Text("\(Random.monthName(fromMonthInt: monthInt)) \(String(yearInt))")
                .font(.title)
                .foregroundColor(Color(UIColor.label))
                .frame(maxWidth: .infinity, alignment: .leading)
                .padding()
                .background(
                    Color(theme.currentTheme.secondaryBGColor)
                )
            
            ScrollView {
                LazyVGrid(columns: columns, spacing: 25) {
                    ForEach(entries, id: \.self) { entry in
                        VStack {
                            if entry.mood != .placeholder {
                                Text(entry.forDate!,
                                     format: Date.FormatStyle().day())
                                    .font(.title3)
                                    .foregroundColor(Color(UIColor.label))
                                
                                entry.mood.icon
                                    .resizable()
                                    .frame(minWidth: 5,
                                           maxWidth: 50,
                                           minHeight: 5,
                                           maxHeight: 50,
                                           alignment: .center)
                                    .aspectRatio(contentMode: .fit)
                                    .foregroundColor(entry.mood.color)
                            }
                        }
                        .alert(ContentModeViewModel.updateTitleHeader(forEntry: entry),
                               isPresented: $showingUpdateEntryAlert) {
                            ForEach(Mood.allValues) { mood in
                                Button(mood.strValue, action: {
                                    PersistenceController.shared.update(entryDate: entry.forDate!, withModd: mood)
//                                    viewModel.update(entry: selectedEntry, toMood: mood)
                                })
                            }
                            
                            if deleteEnabled,
                               entry.mood != .missing {
                                Button(String(localized: "content_view_delete_entry"), action: {
                                    PersistenceController.shared.update(entryDate: entry.forDate!, withModd: Mood.missing)
//                                    viewModel.update(entry: selectedEntry, toMood: Mood.missing)
                                })
                            }
                            
                            Button(String(localized: "content_view_fill_in_missing_entry_cancel"), role: .cancel,  action: {
                                
                            })
                        }
                    }
                }
            }
            .padding([.leading, .trailing])
            
            monthDetails
                .frame(maxWidth: .infinity, alignment: .leading)
                .background(
                    Color(theme.currentTheme.secondaryBGColor)
                )
        }
    }
    
    private var monthDetails: some View {
        VStack {
            SmallRollUpHeaderView(entries: entries,
                                  viewType: .constant(.total))
            
            SmallRollUpHeaderView(entries: entries,
                                  viewType: .constant(.percentageCircle))
                .padding(.top, -20)
        }
        .padding()
    }
}

struct MonthDetailView_Previews: PreviewProvider {
    static var previews: some View {
        MonthDetailView(monthInt: 5, yearInt: 2022, entries:
                            PersistenceController.shared.randomEntries(count: 30).sorted(by: {
            $0.forDate! < $1.forDate!
        }))
    }
}
