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

import SwiftUI

struct HomeViewTwo: 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
    
    @ObservedObject var viewModel = ContentModeViewModel(addMonthStartWeekdayPadding: true)
   
    @StateObject private var selectedDetail = StupidAssDetailViewObservableObject()
    
    class StupidAssDetailViewObservableObject: ObservableObject {
        @Published var fuckingWrapped: MonthDetailView? = nil
        @Published var showFuckingSheet = false
    }
    
    let columns = [
        GridItem(.flexible(minimum: 5, maximum: 400)),
        GridItem(.flexible(minimum: 5, maximum: 400)),
        GridItem(.flexible(minimum: 5, maximum: 400)),
        GridItem(.flexible(minimum: 5, maximum: 400)),
        GridItem(.flexible(minimum: 5, maximum: 400)),
        GridItem(.flexible(minimum: 5, maximum: 400)),
        GridItem(.flexible(minimum: 5, maximum: 400))
    ]
    
    var body: some View {
        ScrollView {
            topView
                .frame(maxWidth: .infinity, alignment: .leading)
                .frame(height: 200)
                .background(
                    .blue
                )
            LazyVStack(spacing: 5, pinnedViews: [.sectionHeaders]) {
                ForEach(viewModel.grouped.sorted(by: { $0.key > $1.key }), id: \.key) { year, months in
                    
                    // for reach month
                    ForEach(months.sorted(by: { $0.key > $1.key }), id: \.key) { month, entries in
                        Section(header: homeViewTwoSectionHeaderView(month: month, year: year)) {
                            homeViewTwoMonthListView(month: month, year: year, entries: entries)
                        }
                        .onTapGesture{
                            let deailView = MonthDetailView(monthInt: month,
                                                            yearInt: year,
                                                            entries: entries)
                            
                            selectedDetail.fuckingWrapped = deailView
                            selectedDetail.showFuckingSheet = true
                        }
                    }
                    .padding(.bottom)
                }
            }
            .padding([.leading, .trailing])
        }.sheet(isPresented: $selectedDetail.showFuckingSheet,
                onDismiss: didDismiss) {
            selectedDetail.fuckingWrapped
        }
    }
    
    func didDismiss() {
        selectedDetail.showFuckingSheet = false
        selectedDetail.fuckingWrapped = nil
    }
}

extension HomeViewTwo {
    private var topView: some View {
        HStack {
            Text("dis top")
        }
    }
}

// view that make up the list body
extension HomeViewTwo {
    private func homeViewTwoSectionHeaderView(month: Int, year: Int) -> some View {
        Text("\(Random.monthName(fromMonthInt: month)) \(String(year))")
            .font(.body)
            .foregroundColor(Color(UIColor.label))
            .frame(maxWidth: .infinity, alignment: .leading)
            .background(
                Color(theme.currentTheme.secondaryBGColor)
            )
    }
    
    private func homeViewTwoMonthListView(month: Int, year: Int, entries: [MoodEntry]) -> some View {
        LazyVGrid(columns: columns, spacing: 15) {
            ForEach(entries, id: \.self) { entry in
                Circle()
                    .foregroundColor(entry.mood.color)
                    .frame(minHeight: 5, idealHeight: 20, maxHeight: 50, alignment: .center)
            }
        }
    }
}

struct HomeViewTwo_Previews: PreviewProvider {
    static var previews: some View {
        HomeViewTwo()
    }
}
