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

import Foundation

struct SupersetExercise: Identifiable, Codable, Equatable, Hashable {
    var id: Int?
    
    let workout: Int?
    let exercise: Exercise
    let weight: Int?
    let reps: Int?
    let duration: Int?
    let durationAudio: String?
    let weightAudio: String?
    let createdAt: String?
    let order, superset: Int?
    let uniqueID: String?
    let description: String?
    let audioQueues: [AudioQueue]?
    
    enum CodingKeys: String, CodingKey {
        case workout, exercise, weight, reps, duration, order, superset, id, description
        case durationAudio = "duration_audio"
        case weightAudio = "weight_audio"
        case createdAt = "created_at"
        case uniqueID = "unique_id"
        case audioQueues = "audio_queues"
    }
    
    public func hash(into hasher: inout Hasher) {
        return hasher.combine(id)
    }
}

struct Exercise: Identifiable, Codable, Equatable, Hashable {
    static func == (lhs: Exercise, rhs: Exercise) -> Bool {
        lhs.id == rhs.id
    }
    
    public func hash(into hasher: inout Hasher) {
        return hasher.combine(id)
    }
    
    let id: Int
    let equipment: [Equipment]
    let muscles: [Muscle]
    let audioURL, videoURL, createdAt, updatedAt: String
    let name, description: String
    let side, jointsUsed, movementPatterns: String?
    let isTwoDumbbells, isTrackableDistance, isAlternating, isWeight: Bool
    let isDistance, isDuration, isReps: Bool
    let equipmentRequired, muscleGroups: String?
    let synonyms: String?

    enum CodingKeys: String, CodingKey {
        case id, muscles, equipment
        case audioURL = "audio_url"
        case videoURL = "video_url"
        case createdAt = "created_at"
        case updatedAt = "updated_at"
        case name, description, side
        case isTwoDumbbells = "is_two_dumbbells"
        case isTrackableDistance = "is_trackable_distance"
        case isAlternating = "is_alternating"
        case isWeight = "is_weight"
        case isDistance = "is_distance"
        case isDuration = "is_duration"
        case isReps = "is_reps"
        case jointsUsed = "joints_used"
        case movementPatterns = "movement_patterns"
        case equipmentRequired = "equipment_required"
        case muscleGroups = "muscle_groups"
        case synonyms
    }
    
    var extName: String {
        if side != nil && side!.count > 0 {
            var returnString = name + " - " + side!
            returnString = returnString.replacingOccurrences(of: "_", with: " ")
            return returnString.capitalized
        }
        return name
    }
    
    var spacedMuscleGroups: String {
        return muscleGroups?.replacingOccurrences(of: ",", with: ", ") ?? ""
    }
    
    var spacedEquipmentRequired: String {
        return equipmentRequired?.replacingOccurrences(of: ",", with: ", ") ?? ""
    }
}
