//
//  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()
                .frame(height: 55)
                .textFieldStyle(OvalTextFieldStyle())
            
            TextField("Description", text: $viewModel.description)
                .padding()
                .frame(height: 55)
                .textFieldStyle(OvalTextFieldStyle())
            ScrollViewReader { proxy in
                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 != nil && 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.increaseRandomNumberForUpdating()
                                    viewModel.objectWillChange.send()
                                }, onDecrement: {
                                    superset.wrappedValue.decreaseNumberOfRounds()
                                    viewModel.increaseRandomNumberForUpdating()
                                    viewModel.objectWillChange.send()
                                })
                               
                                Text("\(superset.wrappedValue.numberOfRounds)")
                                    .foregroundColor(superset.numberOfRounds.wrappedValue > 0 ? .black : .red)
                                    .bold()
                            }
                            
                            CreateWorkoutSupersetActionsView(workoutSuperSet: superset.wrappedValue,
                                                             showAddExercise: $showAddExercise,
                                                             viewModel: viewModel,
                                                             selectedCreateWorkoutSuperSet: $selectedCreateWorkoutSuperSet)
                        }
                    }
                    Text("this is the bottom 🤷‍♂️")
                        .id(999)
                        
                    .listRowSeparator(.hidden)
                }
                .onChange(of: viewModel.randomValueForUpdatingValue, perform: { newValue in
                    withAnimation {
                        proxy.scrollTo(999, anchor: .bottom)
                    }
                })
            }
//            .overlay(Group {
//                if($viewModel.superSets.isEmpty) {
//                    ZStack() {
//                        Color(uiColor: .secondarySystemBackground)
//                    }
//                }
//            })
            
            Divider()
            
            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)
                .disabled(viewModel.title.isEmpty)
            }
            .frame(height: 44)
            .padding(.bottom)
        }
        .sheet(isPresented: $showAddExercise) {
            AddExerciseView(selectedExercise: { exercise in
                let workoutExercise = CreateWorkoutExercise(exercise: exercise)
                selectedCreateWorkoutSuperSet?.exercises.append(workoutExercise)
                
                
                // if left or right auto add the other side
                // with a recover in between b/c its
                // eaiser to delete a recover than add one
                if exercise.side != nil && exercise.side!.count > 0 {
                    let exercises = DataStore.shared.allExercise?.filter({
                        $0.name == exercise.name
                    })
                    let recover = DataStore.shared.allExercise?.first(where: {
                        $0.name.lowercased() == "recover"
                    })
                    if let exercises = exercises, let recover = recover {
                        if exercises.count == 2 {
                            let recoverWorkoutExercise = CreateWorkoutExercise(exercise: recover)
                            selectedCreateWorkoutSuperSet?.exercises.append(recoverWorkoutExercise)
                            for LRExercise in exercises {
                                if LRExercise.id != exercise.id {
                                    let otherSideExercise = CreateWorkoutExercise(exercise: LRExercise)
                                    selectedCreateWorkoutSuperSet?.exercises.append(otherSideExercise)
                                }
                            }
                        }
                    }
                }
                
                viewModel.increaseRandomNumberForUpdating()
                viewModel.objectWillChange.send()
                selectedCreateWorkoutSuperSet = nil
            })
        }
    }
}

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