//
//  FilterAllView.swift
//  Werkout_ios
//
//  Created by Trey Tartt on 11/25/24.
//

import SwiftUI

struct FilterAllView: View {
    @Binding var selectedMuscles: Set<String>
    @Binding var selectedEquipment: Set<String>
    @Binding var searchNameString: String
    @Binding var uniqueWorkoutUsers: [RegisteredUser]?
    @Binding var filteredRegisterdUser: RegisteredUser?
    @Binding var filteredWorkouts: [Workout]
    @Binding var workouts: [Workout]
    @Binding var currentSort: SortType?
    
    var body: some View {
        VStack {
            TextField("Search by name" ,text: $searchNameString)
                .padding(.horizontal)
                .textFieldStyle(.roundedBorder)

            HStack {
                if let uniqueWorkoutUsers = uniqueWorkoutUsers {
                    Menu(content: {
                        ForEach(uniqueWorkoutUsers, id: \.self) { index in
                            Button(action: {
                                filteredRegisterdUser = index
                                filteredWorkouts = workouts.filterWorkouts(
                                    nameSearchString: searchNameString,
                                    musclesSearchString: selectedMuscles,
                                    equipmentSearchString: selectedEquipment,
                                    filteredRegisterdUser: filteredRegisterdUser
                                )
                            }, label: {
                                Text((index.firstName ?? "") + " -" + (index.lastName ?? ""))
                            })
                        }
                        
                        Button(action: {
                            filteredRegisterdUser = nil
                            filteredWorkouts = workouts.filterWorkouts(
                                nameSearchString: searchNameString,
                                musclesSearchString: selectedMuscles,
                                equipmentSearchString: selectedEquipment,
                                filteredRegisterdUser: filteredRegisterdUser
                            )
                        }, label: {
                            Text("All")
                        })
                        
                    }, label: {
                        Image(systemName: filteredRegisterdUser == nil ? "person.2" : "person.2.fill")
                            .padding(.trailing)
                    })
                }
                
                Menu(content: {
                    ForEach(SortType.allCases, id: \.self) { index in
                        Button(action: {
                            sortWorkouts(sortType: index)
                        }, label: {
                            Text(index.rawValue)
                        })
                    }
                }, label: {
                    Image(systemName: "list.number")
                        .padding(.trailing)
                })
                
                if let equipments = DataStore.shared.allEquipment {
                    Menu(content: {
                        ForEach(equipments, id: \.id) { equipment in
                            Button(action: {
                                if selectedEquipment.contains(equipment.name) {
                                    selectedEquipment.remove(equipment.name)
                                } else {
                                    selectedEquipment.insert(equipment.name)
                                }
                            }, label: {
                                if selectedEquipment.contains(equipment.name) {
                                    Image(systemName: "checkmark")
                                }
                                Text(equipment.name)
                            })
                        }
                    }, label: {
                        Image(systemName: "scalemass")
                            .padding(.trailing)
                    })
                }
                
                if let muscles = DataStore.shared.allMuscles {
                    Menu(content: {
                        ForEach(muscles, id: \.id) { muscle in
                            Button(action: {
                                if selectedMuscles.contains(muscle.name) {
                                    selectedMuscles.remove(muscle.name)
                                } else {
                                    selectedMuscles.insert(muscle.name)
                                }
                            }, label: {
                                if selectedMuscles.contains(muscle.name) {
                                    Image(systemName: "checkmark")
                                }
                                Text(muscle.name)
                            })
                        }
                    }, label: {
                        Image(systemName: "brain.head.profile")
                            .padding(.trailing)
                    })
                }
                
                Button(action: {
                    selectedMuscles.removeAll()
                    selectedEquipment.removeAll()
                    searchNameString = ""
                    filteredRegisterdUser = nil
                }, label: {
                    Image(systemName: "xmark.circle")
                        .padding(.trailing)
                })
            }
        }
    }
    
    func sortWorkouts(sortType: SortType) {
        if currentSort == sortType {
            filteredWorkouts = filteredWorkouts.reversed()
            return
        }
        switch sortType {
        case .name:
            filteredWorkouts = filteredWorkouts.sorted(by: {
                $0.name < $1.name
            })
        case .createdDate:
            filteredWorkouts = filteredWorkouts.sorted(by: {
                $0.createdAt ?? Date() < $1.createdAt ?? Date()
            })
        }
        currentSort = sortType
    }
}

#Preview {
    FilterAllView(selectedMuscles: .constant([]),
                  selectedEquipment: .constant([]),
                  searchNameString: .constant(""),
                  uniqueWorkoutUsers: .constant(nil),
                  filteredRegisterdUser: .constant(nil),
                  filteredWorkouts: .constant(PreviewData.allWorkouts()),
                  workouts: .constant(PreviewData.allWorkouts()),
                  currentSort: .constant(nil))
}
