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

import Foundation

class PreviewData {
    class func workout() -> Workout {
        let filepath = Bundle.main.path(forResource: "WorkoutDetail", ofType: "json")!
        let data = try! Data(NSData(contentsOfFile: filepath))
        let workout = try! JSONDecoder().decode(Workout.self, from: data)
        return workout
    }
    
    class func allWorkouts() -> [Workout] {
        if let filepath = Bundle.main.path(forResource: "AllWorkouts", ofType: "json") {
            do {
                let data = try Data(NSData(contentsOfFile: filepath))
                let workout = try JSONDecoder().decode([Workout].self, from: data)
                return workout
            } catch {
                print(error)
                fatalError()
            }
        } else {
            fatalError()
        }
    }
    
    class func parseExercises() -> [Exercise] {
        if let filepath = Bundle.main.path(forResource: "Exercises", ofType: "json") {
            do {
                let data = try Data(NSData(contentsOfFile: filepath))
                let exercises = try JSONDecoder().decode([Exercise].self, from: data)
                return exercises
            } catch {
                print(error)
                fatalError()
            }
        } else {
            fatalError()
        }
    }
    
    class func parseEquipment() -> [Equipment] {
        if let filepath = Bundle.main.path(forResource: "Equipment", ofType: "json") {
            do {
                let data = try Data(NSData(contentsOfFile: filepath))
                let equipment = try JSONDecoder().decode([Equipment].self, from: data)
                return equipment
            } catch {
                print(error)
                fatalError()
            }
        } else {
            fatalError()
        }
    }
    
    class func parseMuscle() -> [Muscle] {
        if let filepath = Bundle.main.path(forResource: "AllMuscles", ofType: "json") {
            do {
                let data = try Data(NSData(contentsOfFile: filepath))
                let muscles = try JSONDecoder().decode([Muscle].self, from: data)
                return muscles
            } catch {
                print(error)
                fatalError()
            }
        } else {
            fatalError()
        }
    }
    
    class func parseRegisterdUser() -> RegisteredUser {
        if let filepath = Bundle.main.path(forResource: "RegisteredUser", ofType: "json") {
            do {
                let data = try Data(NSData(contentsOfFile: filepath))
                let muscles = try JSONDecoder().decode(RegisteredUser.self, from: data)
                return muscles
            } catch {
                print(error)
                fatalError()
            }
        } else {
            fatalError()
        }
    }
    
    class func parseCompletedWorkouts() -> [CompletedWorkout] {
        if let filepath = Bundle.main.path(forResource: "CompletedWorkouts", ofType: "json") {
            do {
                let data = try Data(NSData(contentsOfFile: filepath))
                let muscles = try JSONDecoder().decode([CompletedWorkout].self, from: data)
                return muscles
            } catch {
                print(error)
                fatalError()
            }
        } else {
            fatalError()
        }
    }
}
