//
//  TimerModule.swift
//  Werkout_ios
//
//  Created by Trey Tartt on 6/14/23.
//

import Foundation

class BridgeModule: ObservableObject {
    static let shared = BridgeModule()
    @Published var isShowingOnExternalDisplay = false
    
    private var timer: Timer?
    @Published var timeLeft: Int = 0
    
    @Published var currentExercise: ExerciseElement?
    var currentWorkout: Workout?
    var currentExerciseIdx: Int = -1
    
    @Published var currentWorkoutRunTimeInSeconds: Int = -1
    private var currentWorkoutRunTimer: Timer?
    
    func start(workout: Workout, atExerciseIndex: Int = 0) {
        self.currentWorkout = workout
        currentWorkoutRunTimeInSeconds = 0
        currentWorkoutRunTimer?.invalidate()
        currentWorkoutRunTimer = nil
        
        currentExerciseIdx = 0
        let exercise = workout.exercises[currentExerciseIdx]
        updateCurrent(exercise: exercise)
        startWorkoutTimer()
    }
    
    func completeWorkout() {
        currentWorkoutRunTimeInSeconds = 0
        currentWorkoutRunTimer?.invalidate()
        currentWorkoutRunTimer = nil
        
        currentWorkoutRunTimer?.invalidate()
        currentWorkoutRunTimer = nil
        
        currentWorkoutRunTimeInSeconds = -1
        currentExerciseIdx = -1
        
        currentExercise = nil
        currentWorkout = nil
    }
    
    private func startWorkoutTimer() {
        currentWorkoutRunTimer?.invalidate()
        currentWorkoutRunTimer = nil
        currentWorkoutRunTimer = Timer.scheduledTimer(timeInterval: 1,
                                     target: self,
                                     selector: #selector(addOneToWorkoutRunTime),
                                     userInfo: nil,
                                     repeats: true)
        currentWorkoutRunTimer?.fire()
    }
    
    private func startTimerWith(duration: Int) {
        timer?.invalidate()
        timer = nil
        timeLeft = duration
        timer = Timer.scheduledTimer(timeInterval: 1,
                                     target: self,
                                     selector: #selector(updateCounter),
                                     userInfo: nil,
                                     repeats: true)
        timer?.fire()
    }
    
    @objc func updateCounter() {
        if timeLeft > 0 {
            timeLeft -= 1
        } else {
            timer?.invalidate()
            timer = nil
            
            currentExerciseIdx += 1
            if let currentWorkout = currentWorkout {
                if currentExerciseIdx < currentWorkout.exercises.count {
                    let nextExercise = currentWorkout.exercises[currentExerciseIdx]
                    updateCurrent(exercise: nextExercise)
                }
            }
        }
    }
    
    @objc func addOneToWorkoutRunTime() {
        currentWorkoutRunTimeInSeconds += 1
    }
    
    func updateCurrent(exercise: ExerciseElement) {
        self.currentExercise = exercise

        if let duration = exercise.duration {
            startTimerWith(duration: duration)
        }
    }
}
