//
//  AllExerciseView.swift
//  Werkout_ios
//
//  Created by Trey Tartt on 6/16/24.
//

import SwiftUI
import AVKit

struct AllExerciseView: View {
    @Environment(\.dismiss) var dismiss
    @State var searchString: String = ""
    @Binding var filteredExercises: [Exercise]
    var selectedExercise: ((Exercise) -> Void)
    
    @State var avPlayer = AVPlayer(url: URL(string: "https://dev.werkout.fitness/media/exercise_videos/2_Dumbbell_Lateral_Lunges.mp4")!)
    @State var videoExercise: Exercise? {
        didSet {
            if let viddd = self.videoExercise?.videoURL,
               let url = URL(string: BaseURLs.currentBaseURL + viddd) {
                self.avPlayer = AVPlayer(url: url)
            }
        }
    }
    
    var body: some View {
        VStack {
            TextField("Filter", text: $searchString)
                .padding()

            List() {
                ForEach(filteredExercises, id: \.self) { exercise in
                    if searchString.isEmpty || (exercise.name.lowercased().contains(searchString.lowercased()) || (exercise.muscleGroups ?? "").lowercased().contains(searchString.lowercased())) {
                        HStack {
                            VStack {
                                Text(exercise.name)
                                    .frame(maxWidth: .infinity, alignment: .leading)
                                
                                if exercise.side != nil && !exercise.side!.isEmpty {
                                    Text(exercise.side!)
                                        .font(.footnote)
                                        .frame(maxWidth: .infinity, alignment: .leading)
                                }
                                
                                if exercise.equipmentRequired != nil && !exercise.equipmentRequired!.isEmpty {
                                    Text(exercise.spacedEquipmentRequired)
                                        .font(.footnote)
                                        .frame(maxWidth: .infinity, alignment: .leading)
                                }
                                
                                if exercise.muscleGroups != nil && !exercise.muscleGroups!.isEmpty {
                                    Text(exercise.spacedMuscleGroups)
                                        .font(.footnote)
                                        .frame(maxWidth: .infinity, alignment: .leading)
                                }
                            }
                            .contentShape(Rectangle())
                            .onTapGesture {
                                selectedExercise(exercise)
                                dismiss()
                            }
                            Button(action: {
                                videoExercise = exercise
                            }) {
                                ZStack {
                                    Circle()
                                        .fill(.blue)
                                        .frame(width: 33, height: 33)
                                    Image(systemName: "video.fill")
                                        .frame(width: 33, height: 33)
                                        .foregroundColor(.white )
                                }
                            }
                            .frame(width: 33, height: 33)
                        }
                    }
                }
            }
        }
        .sheet(item: $videoExercise) { exercise in
            PlayerView(player: $avPlayer)
                .onAppear{
                    avPlayer.play()
                }
        }
    }
}
//
//#Preview {
//    AllExerciseView()
//}
