//
//  AllWorkoutsListView.swift
//  Werkout_ios
//
//  Created by Trey Tartt on 7/7/23.
//

import SwiftUI

enum SortType: String, CaseIterable {
    case name = "Name"
    case createdDate = "Created Date"
}

struct AllWorkoutsListView: View {
    @State var searchNameString: String = ""
    @State var selectedMuscles: Set<String> = []
    @State var selectedEquipment: Set<String> = []
    @Binding var uniqueWorkoutUsers: [RegisteredUser]?
    @State private var filteredRegisterdUser: RegisteredUser?
    
    @State var workouts: [Workout]
    let selectedWorkout: ((Workout) -> Void)
    @State var filteredWorkouts = [Workout]()
    var refresh: (() -> Void)
    @State var currentSort: SortType?
    
    var body: some View {
        VStack {
            VStack {
                if let filteredRegisterdUser = filteredRegisterdUser {
                    Text((filteredRegisterdUser.firstName ?? "NA") + "'s Workouts")
                }
                
                FilterAllView(selectedMuscles: $selectedMuscles,
                              selectedEquipment: $selectedEquipment,
                              searchNameString: $searchNameString,
                              uniqueWorkoutUsers: $uniqueWorkoutUsers,
                              filteredRegisterdUser: $filteredRegisterdUser,
                              filteredWorkouts: $filteredWorkouts,
                              workouts: $workouts,
                              currentSort: $currentSort)
            }
            
            ScrollView {
                LazyVStack(spacing: 10) {
                    ForEach(filteredWorkouts, id:\.id) { workout in
                        WorkoutOverviewView(workout: workout)
                            .padding([.leading, .trailing], 4)
                            .contentShape(Rectangle())
                            .onTapGesture {
                                selectedWorkout(workout)
                            }
                    }
                }
            }
            .background(Color(uiColor: .systemBackground))
            .refreshable {
                refresh()
            }
        }
        .onChange(of: searchNameString) { newValue in
            filterWorkouts()
        }
        .onChange(of: selectedMuscles) { newValue in
            filterWorkouts()
        }
        .onChange(of: selectedEquipment) { newValue in
            filterWorkouts()
        }
        .onAppear{
            filterWorkouts()
        }
    }
    
    func filterWorkouts() {
        filteredWorkouts = workouts.filterWorkouts(
            nameSearchString: searchNameString,
            musclesSearchString: selectedMuscles,
            equipmentSearchString: selectedEquipment,
            filteredRegisterdUser: filteredRegisterdUser
        )
    }
}

struct AllWorkoutsListView_Previews: PreviewProvider {
    static var previews: some View {
        AllWorkoutsListView(uniqueWorkoutUsers: .constant([]),
                            workouts: PreviewData.allWorkouts(),
                            selectedWorkout: { workout in },
                            refresh: {  })
    }
}
