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

import SwiftUI

struct CreateWorkoutMainView: View {
    @StateObject var viewModel = WorkoutViewModel()
    @State var selectedCreateWorkoutSuperSet: CreateWorkoutSuperSet?
    @State private var showAddExercise = false
    
    var body: some View {
        VStack {
            VStack {
                TextField("Title", text: $viewModel.title)
                    .padding(.horizontal)
                    .textFieldStyle(.roundedBorder)
                
                TextField("Description", text: $viewModel.description)
                    .padding(.horizontal)
                    .textFieldStyle(.roundedBorder)
            }
            .padding(.bottom)
            .background(Color(uiColor: .systemGray5))
            
            ScrollViewReader { proxy in
                List() {
                    ForEach($viewModel.superSets, id: \.id) { superset in
                        CreateWorkoutSupersetView(
                            selectedCreateWorkoutSuperSet: $selectedCreateWorkoutSuperSet,
                            showAddExercise: $showAddExercise,
                            superset: superset,
                            viewModel: viewModel)
                    }
                    // after adding new exercise we have to scroll to the bottom
                    // where the new exercise is sooo keep this so we can scroll
                    // to id 999
                    Text("this is the bottom 🤷‍♂️")
                        .id(999)
                        .listRowSeparator(.hidden)
                }
                .onChange(of: viewModel.randomValueForUpdatingValue, perform: { newValue in
                    withAnimation {
                        proxy.scrollTo(999, anchor: .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
                })
            }
            
            HStack {
                Button("Add Superset", action: {
                    viewModel.addNewSuperset()
                })
                .frame(maxWidth: .infinity, alignment: .center)
                .frame(height: 44)
                .foregroundColor(.white)
                .background(.blue)
                .cornerRadius(Constants.buttonRadius)
                .padding()
                .frame(maxWidth: .infinity)
                
                Divider()
                
                Button("Done", action: {
                    viewModel.uploadWorkout()
                })
                .frame(maxWidth: .infinity, alignment: .center)
                .frame(height: 44)
                .foregroundColor(.white)
                .background(.green)
                .cornerRadius(Constants.buttonRadius)
                .padding()
                .frame(maxWidth: .infinity)
                .disabled(viewModel.title.isEmpty)
            }
            .frame(height: 44)
            
            Divider()
        }
        .background(Color(uiColor: .systemGray5))
    }
}

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