//
//  WatchMainViewMoel.swift
//  Werkout_watch Watch App
//
//  Created by Trey Tartt on 6/22/23.
//

import Foundation
import WatchConnectivity
import SwiftUI
import HealthKit

class WatchMainViewModel:  NSObject, ObservableObject  {
    static let shared = WatchMainViewModel()
    
    var session: WCSession
    
    @Published var watchPackageModel = WatchMainViewModel.defualtPackageModle
    
    static var defualtPackageModle: WatchPackageModel {
        WatchPackageModel(currentExerciseName: "", currentExerciseID: -1, currentTimeLeft: -1, workoutStartDate: Date())
    }
        
    override init() {
        session = WCSession.default
        super.init()
        
        session.delegate = self
        session.activate()
        
    }

    // actions from view
    func nextExercise() {
        let nextExerciseAction = WatchActions.nextExercise
        let data = try! JSONEncoder().encode(nextExerciseAction)
        DataSender.send(data)
        WKInterfaceDevice.current().play(.start)
    }
    
    func restartExercise() {
        let nextExerciseAction = WatchActions.restartExercise
        let data = try! JSONEncoder().encode(nextExerciseAction)
        DataSender.send(data)
        WKInterfaceDevice.current().play(.start)
    }
    
    func previousExercise() {
        let nextExerciseAction = WatchActions.previousExercise
        let data = try! JSONEncoder().encode(nextExerciseAction)
        DataSender.send(data)
        WKInterfaceDevice.current().play(.start)
    }
    
    func completeWorkout() {
        let nextExerciseAction = WatchActions.stopWorkout
        let data = try! JSONEncoder().encode(nextExerciseAction)
        DataSender.send(data)
        WKInterfaceDevice.current().play(.start)
    }
    
    func pauseWorkout() {
        let nextExerciseAction = WatchActions.pauseWorkout
        let data = try! JSONEncoder().encode(nextExerciseAction)
        DataSender.send(data)
        WKInterfaceDevice.current().play(.start)
        WatchWorkout.shared.togglePaused()
    }
    
    func dataToAction(messageData: Data) {
        if let model = try? JSONDecoder().decode(PhoneToWatchActions.self, from: messageData) {
            DispatchQueue.main.async {
                switch model {
                case .inExercise(let newWatchPackageModel):
                    WatchWorkout.shared.startWorkout()
                    if self.watchPackageModel.currentExerciseID != newWatchPackageModel.currentExerciseID {
                        WKInterfaceDevice.current().play(.start)
                    }
                    self.watchPackageModel = newWatchPackageModel
                case .reset:
                    self.watchPackageModel = WatchMainViewModel.defualtPackageModle
                    WatchWorkout.shared.stopWorkout(sendDetails: false)
                case .endWorkout:
                    self.watchPackageModel = WatchMainViewModel.defualtPackageModle
                    WatchWorkout.shared.stopWorkout(sendDetails: true)
                case .startWorkout:
                    WatchWorkout.shared.startWorkout()
                }
            }
        }
    }
}
