//
//  BridgeModule+WorkoutActions.swift
//  Werkout_ios
//
//  Created by Trey Tartt on 6/21/24.
//

import Foundation
import WatchConnectivity

extension BridgeModule {
    func pauseWorkout() {
        if let _ = currentExerciseTimer {
            currentExerciseTimer?.invalidate()
            currentExerciseTimer = nil
            isPaused = true
        } else {
            isPaused = false
            startExerciseTimerWith(duration: currentExerciseTimeLeft)
        }
    }
    
    func nextExercise() {
        if let nextSupersetExercise = currentExerciseInfo.goToNextExercise {
            updateCurrent(exercise: nextSupersetExercise)
        } else {
            completeWorkout()
        }
    }
    
    func previousExercise() {
        if let nextSupersetExercise = currentExerciseInfo.previousExercise {
            updateCurrent(exercise: nextSupersetExercise)
        } else {
            completeWorkout()
        }
    }
    
    func restartExercise() {
        if let currentExercise = currentExerciseInfo.currentExercise {
            updateCurrent(exercise: currentExercise)
        }
    }
    
    func updateCurrent(exercise: SupersetExercise) {
        DispatchQueue.main.async {
            self.currentExerciseTimer?.invalidate()
            self.currentExerciseTimer = nil
            
            if let duration = exercise.duration, duration > 0 {
                self.startExerciseTimerWith(duration: duration)
            }
        }
    }
    
    func completeWorkout() {
        self.currentExerciseTimer?.invalidate()
        self.currentExerciseTimer = nil
        self.isInWorkout = false
        
        workoutEndDate = Date()
        if let completedWorkout = completedWorkout {
            completedWorkout()
            self.completedWorkout = nil
        }
        resetCurrentWorkout()
    }
    
    func start(workout: Workout) {
        currentExerciseInfo.complete = {
            self.completeWorkout()
        }
        
        currentExerciseInfo.start(workout: workout)
        currentWorkoutRunTimeInSeconds = 0
        currentWorkoutRunTimer?.invalidate()
        currentWorkoutRunTimer = nil
        isPaused = false
        
        if let superetExercise = currentExerciseInfo.currentExercise {
            updateCurrent(exercise: superetExercise)
            startWorkoutTimer()
            workoutStartDate = Date()
            isInWorkout = true
            
            if WCSession.isSupported() {
                session.delegate = self
                session.activate()
                sendStartWorkoutToWatch()
            }
        }
    }
    
    func goToExerciseAt(section: Int, row: Int) {
        if let superetExercise = currentExerciseInfo.goToWorkoutAt(supersetIndex: section,
                                                                   exerciseIndex: row) {
            updateCurrent(exercise: superetExercise)
        }
    }
    
    var nextExerciseObject: SupersetExercise? {
        currentExerciseInfo.goToNextExercise
    }
    
    func resetCurrentWorkout() {
        DispatchQueue.main.async {
            if self.isInWorkout {
                self.sendWorkoutCompleteToWatch()
            }
            self.currentWorkoutRunTimeInSeconds = 0
            self.currentWorkoutRunTimer?.invalidate()
            self.currentWorkoutRunTimer = nil
            
            self.currentExerciseTimer?.invalidate()
            self.currentExerciseTimer = nil
            
            self.currentWorkoutRunTimeInSeconds = -1
            self.currentExerciseInfo.reset()
            
            self.isInWorkout = false
            self.workoutStartDate = nil
            self.workoutEndDate = nil
        }
    }
}
