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

import SwiftUI

struct WorkoutDetailView: View {
    @StateObject var viewModel:  WorkoutDetailViewModel
    var bridgeModule = BridgeModule.shared
    @Environment(\.dismiss) var dismiss
    
    var body: some View {
        ZStack {
            switch viewModel.status {
            case .loading:
                Text("Loading")
            case .showWorkout(let workout):
                VStack {
                    TopButtonsView(workout: workout)
                    .frame(height: 88)
                   
                    Text(workout.name)
                        .font(.title3)
                    if let desc = workout.description {
                        Text(desc)
                            .font(.body)
                    }
                    CurrentWorkoutElapsedTimeView()
                    ExerciseListView(workout: workout)
                    CountdownView()
                }
                .interactiveDismissDisabled()
            }
        }
    }
}

struct TopButtonsView: View {
    @ObservedObject var bridgeModule = BridgeModule.shared
    var workout: Workout
    @Environment(\.dismiss) var dismiss
    
    var body: some View {
        HStack {
            if bridgeModule.currentWorkoutRunTimeInSeconds == -1 {
                Button(action: {
                    bridgeModule.completeWorkout()
                    dismiss()
                }, label: {
                    Image(systemName: "xmark.octagon.fill")
                        .font(.title)
                        .frame(maxWidth: .infinity, maxHeight: .infinity)
                })
                .frame(maxWidth: .infinity, maxHeight: .infinity)
                .background(.red)
                .foregroundColor(.white)
                
                Button(action: {
                    bridgeModule.start(workout: workout)
                }, label: {
                    Image(systemName: "figure.golf")
                        .font(.title)
                        .frame(maxWidth: .infinity, maxHeight: .infinity)
                })
                .frame(maxWidth: .infinity, maxHeight: .infinity)
                .background(.green)
                .foregroundColor(.white)
            } else {
                Button(action: {
                    bridgeModule.completeWorkout()
                    dismiss()
                }, label: {
                    Image(systemName: "checkmark")
                        .font(.title)
                        .frame(maxWidth: .infinity, maxHeight: .infinity)
                })
                .frame(maxWidth: .infinity, maxHeight: .infinity)
                .background(.blue)
                .foregroundColor(.white)
            }
        }
    }
}

struct CurrentWorkoutElapsedTimeView: View {
    @ObservedObject var bridgeModule = BridgeModule.shared
    
    var body: some View {
        if bridgeModule.currentWorkoutRunTimeInSeconds > -1 {
            Text("\(bridgeModule.currentWorkoutRunTimeInSeconds)")
        }
    }
}

struct ExerciseListView: View {
    @ObservedObject var bridgeModule = BridgeModule.shared
    var workout: Workout
    
    var body: some View {
        List() {
            ForEach(workout.exercisesSortedByCreated_at.indices, id: \.self) { i in
                let obj = workout.exercisesSortedByCreated_at[i]
                Text(obj.exercise.name)
                    .onTapGesture {
                        bridgeModule.start(workout: workout, atExerciseIndex: i)
                    }
            }
        }
    }
}

struct CountdownView: View {
    @StateObject var bridgeModule = BridgeModule.shared
    
    var body: some View {
        VStack {
            if let duration = bridgeModule.currentExercise?.duration {
                HStack {
                    ProgressView(value: Float(bridgeModule.timeLeft), total: Float(duration))
                    Text("\(bridgeModule.timeLeft)")
                }.padding(16)
            }
        }
    }
}

struct WorkoutDetailView_Previews: PreviewProvider {
    static var previews: some View {
        WorkoutDetailView(viewModel: WorkoutDetailViewModel(workout: PreviewWorkout.workout()))
    }
}
