//
//  WorkoutHistoryView.swift
//  Werkout_ios
//
//  Created by Trey Tartt on 6/26/23.
//

import SwiftUI

struct WorkoutHistoryView: View {
    enum DifficltyString: Int {
        case easy = 1
        case moderate
        case average
        case hard
        case death
        
        var stringValue: String {
            switch self {
                
            case .easy:
                return "Easy"
            case .moderate:
                return "Moderate"
            case .average:
                return "Average"
            case .hard:
                return "Hard"
            case .death:
                return "Death"
            }
        }
    }
    
    let completedWorkouts: [CompletedWorkout]
    
    @State private var selectedPlannedWorkout: Workout?
    
    var body: some View {
        List {
            ForEach(completedWorkouts, id:\.self.id) { completedWorkout in
                HStack {
                    VStack {
                        if let date = completedWorkout.workoutStartTime.dateFromServerDate {
                            Text(DateFormatter().shortMonthSymbols[date.get(.month) - 1])
                            
                            Text("\(date.get(.day))")
                            Text("\(date.get(.hour))")
                        }
                    }
                    
                    VStack(alignment: .leading) {
                        Text(completedWorkout.workout.name)
                            .font(.title3)
                        
                        if let desc = completedWorkout.workout.description {
                            Text(desc)
                                .font(.footnote)
                        }
                        
                        Divider()
                        
                        if let difficulty = completedWorkout.difficulty,
                           let string = DifficltyString.init(rawValue: difficulty)?.stringValue {
                            Text(string)
                        }
                        
                        if let notes = completedWorkout.notes {
                            Text(notes)
                        }
                    }
                    .padding(.leading)
                    .contentShape(Rectangle())
                    .onTapGesture {
                        selectedPlannedWorkout = completedWorkout.workout
                    }
                   
                }
            }
        }
        .sheet(item: $selectedPlannedWorkout) { item in
            let viewModel = WorkoutDetailViewModel(workout: item, isPreview: true)
            WorkoutDetailView(viewModel: viewModel)
        }
    }
}

struct WorkoutHistoryView_Previews: PreviewProvider {
    static let fakeHistory = PreviewData.parseCompletedWorkouts()
    static var previews: some View {
        WorkoutHistoryView(completedWorkouts: fakeHistory)
    }
}
