//
//  CurrentWorkoutInfo.swift
//  Werkout_ios
//
//  Created by Trey Tartt on 7/17/23.
//

import Foundation

class CurrentWorkoutInfo {
    var supersetIndex: Int = 0
    var exerciseIndex: Int = -1
    var workout: Workout?
    var complete: (() -> Void)?
    
    var currentRound = 1
    var allSupersetExecerciseIndex = 0
    
    var superset: [Superset] {
        return workout?.supersets?.sorted(by: { $0.order < $1.order }) ?? [Superset]()
    }
    
    var numberOfRoundsInCurrentSuperSet: Int {
        guard let workout = workout else { return -1 }
        guard let supersets = workout.supersets else { return -1 }
        
        if supersetIndex >= supersets.count {
            return -1
        }
        let currentSuperSet = supersets[supersetIndex]
        
        if exerciseIndex >= currentSuperSet.exercises.count {
            return -1
        }
        
        return currentSuperSet.rounds
    }
    
    var currentExercise: SupersetExercise? {
        guard let supersets = workout?.supersets else { return nil }
        
        if supersetIndex >= supersets.count { return nil }
        let superset = supersets[supersetIndex]
        
        // will be -1 for a moment while going to previous workout / superset
        if exerciseIndex < 0 { return nil }
        if exerciseIndex > superset.exercises.count { return nil }
        let exercise = superset.exercises[exerciseIndex]
        return exercise
    }
    
    var allSupersetExecercise: SupersetExercise? {
        let obj = workout?.allSupersetExecercise?[allSupersetExecerciseIndex]
        return obj
    }
    
    // this is setting nothing so we can half ass it
    var nextExerciseInfo: SupersetExercise? {
        guard let workout = workout else { return nil }
        
        let _exerciseIndex = allSupersetExecerciseIndex + 1
        if _exerciseIndex >= workout.allSupersetExecercise?.count ?? -1 {
            return nil
        }
        return workout.allSupersetExecercise?[_exerciseIndex]
    }
    
    // this needs to set stuff for iphone
    var goToNextExercise: SupersetExercise? {
        guard let workout = workout else { return nil }
        guard let supersets = workout.supersets else { return nil }
        
        exerciseIndex += 1
        let currentSuperSet = supersets[supersetIndex]
        
        if exerciseIndex >= currentSuperSet.exercises.count {
            currentRound += 1
            
            if currentRound > currentSuperSet.rounds {
                supersetIndex += 1
                currentRound = 1
                
                if supersetIndex >= supersets.count {
                    return nil
                }
            }
            
            exerciseIndex = 0
        }
        
        let superset = supersets[supersetIndex]
        let exercise = superset.exercises[exerciseIndex]
        allSupersetExecerciseIndex += 1
        return exercise
    }
    
    var previousExercise: SupersetExercise? {
        guard let workout = workout else { return nil }
        guard let supersets = workout.supersets else { return nil }
        
        exerciseIndex -= 1
        if exerciseIndex < 0 {
            if currentRound > 1 {
                currentRound -= 1
                let superset = supersets[supersetIndex]
                exerciseIndex = superset.exercises.count-1
            } else {
                if supersetIndex > 0 {
                    supersetIndex -= 1
                    let superset = supersets[supersetIndex]
                    currentRound = superset.rounds
                    exerciseIndex = superset.exercises.count-1
                } else {
                    exerciseIndex = 0
                }
            }
        }

        let superset = supersets[supersetIndex]
        let exercise = superset.exercises[exerciseIndex]
        
        allSupersetExecerciseIndex -= 1
        if allSupersetExecerciseIndex < 0 {
            allSupersetExecerciseIndex = 0
        }
        
        return exercise
    }
    
    func start(workout: Workout) {
        reset()
        self.workout = workout
    }
    
    func reset() {
        supersetIndex = 0
        exerciseIndex = 0
        currentRound = 1
        allSupersetExecerciseIndex = 0
        self.workout = nil
    }
    
    func goToWorkoutAt(supersetIndex: Int, exerciseIndex: Int) -> SupersetExercise? {
        self.supersetIndex = supersetIndex
        self.exerciseIndex = exerciseIndex
        self.currentRound = 1
        
        let currentExercise = currentExercise
        if let firstIdx = workout?.allSupersetExecercise?.firstIndex(where: {
            $0.exercise.id == currentExercise?.exercise.id
        }) {
            allSupersetExecerciseIndex = firstIdx
        }
        
        return currentExercise
    }
}
