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

import SwiftUI

struct CreateWorkoutMainView: View {
    @ObservedObject var viewModel = WorkoutViewModel()
    @State private var showAddExercise = false
    @State var selectedCreateWorkoutSuperSet: CreateWorkoutSuperSet?
    
    var body: some View {
        VStack {
            TextField("Title", text: $viewModel.title)
                .padding()

            HStack {
                Button("Add Superset", action: {
                    viewModel.addNewSuperset()
                })
                .frame(maxWidth: .infinity, alignment: .center)
                .frame(height: 44)
                .foregroundColor(.blue)
                .background(.yellow)
                .cornerRadius(8)
                .padding()
                .frame(maxWidth: .infinity)
                
                Divider()
                
                Button("Done", action: {
                    viewModel.uploadWorkout()
                })
                .frame(maxWidth: .infinity, alignment: .center)
                .frame(height: 44)
                .foregroundColor(.white)
                .background(.blue)
                .cornerRadius(8)
                .padding()
                .frame(maxWidth: .infinity)
            }
            .frame(height: 44)
            
            List() {
                ForEach($viewModel.superSets, id: \.id) { superset in
                    Section {
                        ForEach(superset.exercises, id: \.id) { exercise in
                            HStack {
                                VStack {
                                    Text(exercise.wrappedValue.exercise.name)
                                        .font(.title2)
                                        .frame(maxWidth: .infinity)
                                    if exercise.wrappedValue.exercise.side.count > 0 {
                                        Text(exercise.wrappedValue.exercise.side)
                                            .font(.title3)
                                            .frame(maxWidth: .infinity, alignment: .center)
                                    }
                                    CreateExerciseActionsView(workoutExercise: exercise.wrappedValue,
                                                              superset: superset.wrappedValue,
                                                              viewModel: viewModel)
                                    
                                }
                            }
                        }
                        

                        HStack {
                            Stepper("Number of rounds", onIncrement: {
                                superset.wrappedValue.increaseNumberOfRounds()
                                viewModel.objectWillChange.send()
                            }, onDecrement: {
                                superset.wrappedValue.decreaseNumberOfRounds()
                                viewModel.objectWillChange.send()
                            })
                            Text("\(superset.wrappedValue.numberOfRounds)")
                        }
                        
                        CreateWorkoutSupersetActionsView(workoutSuperSet: superset.wrappedValue,
                                                         showAddExercise: $showAddExercise,
                                                         viewModel: viewModel,
                                                         selectedCreateWorkoutSuperSet: $selectedCreateWorkoutSuperSet)
                    }
                }
                .listRowSeparator(.hidden)
            }
        }
        .sheet(isPresented: $showAddExercise) {
            AddExerciseView(selectedWorkout: { exercise in
                let workoutExercise = CreateWorkoutExercise(exercise: exercise)
                selectedCreateWorkoutSuperSet?.exercises.append(workoutExercise)
                viewModel.objectWillChange.send()
                selectedCreateWorkoutSuperSet = nil
            })
        }
    }
}

//struct CreateWorkoutView_Previews: PreviewProvider {
//    static var previews: some View {
//        CreateWorkoutView()
//    }
//}
