//
//  CreateWorkout.swift
//  Werkout_ios
//
//  Created by Trey Tartt on 6/15/23.
//

import Foundation
import SwiftUI
import Combine

struct AddExerciseView: View {
    enum CreateWorkoutItemPickerViewType {
        case muscles
        case equipment
    }
    
    @State var selectedMuscles = [Muscle]()
    @State var selectedEquipment = [Equipment]()
    @State var filteredExercises = [Exercise]()
    
    @StateObject var bridgeModule = BridgeModule.shared
    let selectedExercise: ((Exercise) -> Void)
    
    var body: some View {
        VStack {
            AllExerciseView(filteredExercises: $filteredExercises,
                            selectedExercise: { excercise in
                selectedExercise(excercise)
            })
            .padding(.top)
            
            HStack {
                AllMusclesView(selectedMuscles: $selectedMuscles)
                    .frame(maxWidth: .infinity)
                
                Divider()
                
                AllEquipmentView(selectedEquipment: $selectedEquipment)
                    .frame(maxWidth: .infinity)
            }
            .padding(.top)
            .frame(height: 44)
        }
        .onChange(of: selectedMuscles, perform: { _ in
            filterExercises()
        }) .onChange(of: selectedEquipment, perform: { _ in
            filterExercises()
        })
    }
    
    func filterExercises() {
        guard let exercises = DataStore.shared.allExercise else {
            filteredExercises = []
            return
        }
        
        let filtered = exercises.filter({ exercise in
            var hasCorrectMuscles = false
            if selectedMuscles.count == 0 {
                hasCorrectMuscles = true
            } else {
                let exerciseMuscleIds = exercise.muscles.map({ $0.muscle ?? -1 })
                let selctedMuscleIds = selectedMuscles.map({ $0.id })
                // if one items match
                if exerciseMuscleIds.contains(where: selctedMuscleIds.contains) {
                // if all items match
                    hasCorrectMuscles = true
                }
            }
            
            var hasCorrectEquipment = false
            if selectedEquipment.count == 0 {
                hasCorrectEquipment = true
            } else {
                let exerciseEquipmentIds = exercise.equipment.map({ $0.equipment ?? -1 })
                let selctedEquipmentIds = selectedEquipment.map({ $0.id })
                // if one items match
                if exerciseEquipmentIds.contains(where: selctedEquipmentIds.contains) {
                // if all items match
                    hasCorrectEquipment = true
                }
            }
            return hasCorrectMuscles && hasCorrectEquipment
        })
        
        filteredExercises = filtered
    }
}

//struct AddExerciseView_Previews: PreviewProvider {
//    static var previews: some View {
//        AddExerciseView(selectedExercise: { _ in })
//    }
//}
