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

import SwiftUI

class CreateWorkoutExercise: ObservableObject, Identifiable {
    let id = UUID()
    var exercise: ExerciseExercise
    @Published var reps: Int = 0
    @Published var duration: Int = 0
    @Published var weight: Int = 0
    
    init(exercise: ExerciseExercise, reps: Int = 0, duration: Int = 0, weight: Int = 0) {
        self.exercise = exercise
        self.reps = reps
        self.duration = duration
        self.weight = weight
    }
    
    func increaseReps() {
        self.reps += 1
    }
    
    func decreaseReps() {
        self.reps -= 1
        if self.reps < 0 {
            self.reps = 0
        }
    }
    
    func increaseDuration() {
        self.duration += 15
    }
    
    func decreaseDuration() {
        self.duration -= 15
        if self.duration < 0 {
            self.duration = 0
        }
    }
    
    func increaseWeight() {
        self.weight += 5
    }
    
    func decreaseWeight() {
        self.weight -= 15
        if self.weight < 0 {
            self.weight = 0
        }
    }
}

class CreateWorkoutSuperSet: ObservableObject, Identifiable {
    let id = UUID()
    @Published var exercises = [CreateWorkoutExercise]()
    @Published var numberOfRounds = 1
    
    func increaseNumberOfRounds() {
        self.numberOfRounds += 1
    }
    
    func decreaseNumberOfRounds() {
        self.numberOfRounds -= 1
        if self.numberOfRounds < 1 {
            self.numberOfRounds = 1
        }
    }
    
    func deleteExerciseForChosenSuperset(exercise: CreateWorkoutExercise) {
        if let idx = exercises.firstIndex(where: {
            $0.id == exercise.id
        }) {
            exercises.remove(at: idx)
        }
    }
}

class WorkoutViewModel: ObservableObject {
    @Published var superSets = [CreateWorkoutSuperSet]()
    @Published var title = String()
    
    func addNewSuperset() {
        superSets.append(CreateWorkoutSuperSet())
    }
    
    func delete(superset: CreateWorkoutSuperSet) {
        if let idx = superSets.firstIndex(where: {
            $0.id == superset.id
        }) {
            superSets.remove(at: idx)
        }
    }
    
    func uploadWorkout() {
        var exercises = [[String: Any]]()
        superSets.forEach({ superset in
            for _ in 0 ..< superset.numberOfRounds {
                for exercise in superset.exercises {
                    let item = ["id": exercise.exercise.id, "reps": exercise.reps, "weight": exercise.weight, "duration": exercise.duration] as [String : Any]
                    exercises.append(item)
                }
            }
        })
        let uploadBody = ["name": title, "description": "description", "exercise_data": exercises] as [String : Any]
        CreateWorkoutFetchable(postData: uploadBody).fetch(completion: { result in
            DispatchQueue.main.async {
                switch result {
                case .success(_):
                    self.superSets.removeAll()
                    self.title = ""
                    NotificationCenter.default.post(name: NSNotification.Name("CreatedNewWorkout"), object: nil, userInfo: nil)
                case .failure(let failure):
                    print(failure)
                }
            }
        })
    }
}
