//
//  CreateViewRepsWeightView.swift
//  Werkout_ios
//
//  Created by Trey Tartt on 6/18/23.
//

import SwiftUI
import AVFoundation

struct CreateExerciseActionsView: View {
    @ObservedObject var workoutExercise: CreateWorkoutExercise
    var superset: CreateWorkoutSuperSet
    var viewModel: WorkoutViewModel
   
    @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 {
            VStack {
                HStack {
                    Text("Reps: ")
                    Text("\(workoutExercise.reps)")
                        .foregroundColor(workoutExercise.reps == 0 && workoutExercise.duration == 0 ? .red : Color(uiColor: .label))
                        .bold()
                    
                    Stepper("", onIncrement: {
                        workoutExercise.increaseReps()
                    }, onDecrement: {
                        workoutExercise.decreaseReps()
                    })
                }
            }
            
            HStack {
                Text("Weight: ")
                Text("\(workoutExercise.weight)")
                
                Stepper("", onIncrement: {
                    workoutExercise.increaseWeight()
                }, onDecrement: {
                    workoutExercise.decreaseWeight()
                })
            }
            
            HStack {
                Text("Duration: ")
                Text("\(workoutExercise.duration)")
                    .foregroundColor(
                        workoutExercise.reps == 0 && workoutExercise.duration == 0 ? .red : Color(
                            uiColor: .label
                        )
                    )
                    .bold()
                
                Stepper("", onIncrement: {
                    workoutExercise.increaseDuration()
                }, onDecrement: {
                    workoutExercise.decreaseDuration()
                })
            }
            
            HStack {
                Spacer()
                Button(action: {
                    videoExercise = workoutExercise.exercise
                }) {
                    Image(systemName: "video.fill")
                }
                .frame(width: 88, height: 44)
                .foregroundColor(.white)
                .background(.blue)
                .cornerRadius(Constants.buttonRadius)
                .buttonStyle(BorderlessButtonStyle())
                
                Spacer()
                
                Spacer()
                
                Button(action: {
                    superset
                        .deleteExerciseForChosenSuperset(exercise: workoutExercise)
                    viewModel.increaseRandomNumberForUpdating()
                    viewModel.objectWillChange.send()
                }) {
                    Image(systemName: "trash.fill")
                }
                .frame(width: 88, height: 44)
                .foregroundColor(.white)
                .background(.red)
                .cornerRadius(Constants.buttonRadius)
                .buttonStyle(BorderlessButtonStyle())
                
                Spacer()
            }
        }
        .sheet(item: $videoExercise) { exercise in
            PlayerView(player: $avPlayer)
                .onAppear{
                    avPlayer.isMuted = true
                    avPlayer.play()
                }
        }
    }
}

//struct CreateViewRepsWeightView_Previews: PreviewProvider {
//    static var previews: some View {
//        CreateViewRepsWeightView()
//    }
//}
