//
//  ActionsView.swift
//  Werkout_ios
//
//  Created by Trey Tartt on 7/7/23.
//

import SwiftUI

struct ActionsView: View {
    @ObservedObject var bridgeModule = BridgeModule.shared
    var completedWorkout: (() -> Void)?
    var planWorkout: ((Workout) -> Void)?
    
    var workout: Workout
    @Environment(\.dismiss) var dismiss
    var showAddToCalendar: Bool
    var startWorkoutAction: (() -> Void)
    
    var body: some View {
        HStack {
            if bridgeModule.isInWorkout == false {
                Button(action: {
                    bridgeModule.resetCurrentWorkout()
                    dismiss()
                }, label: {
                    Image(systemName: "xmark.octagon.fill")
                        .font(.title)
                        .frame(maxWidth: .infinity, maxHeight: .infinity)
                })
                .frame(maxWidth: .infinity, maxHeight: .infinity)
                .background(.red)
                .foregroundColor(.white)
                
                if showAddToCalendar {
                    Button(action: {
                        planWorkout?(workout)
                    }, label: {
                        Image(systemName: "calendar.badge.plus")
                            .font(.title)
                            .frame(maxWidth: .infinity, maxHeight: .infinity)
                    })
                    .frame(maxWidth: .infinity, maxHeight: .infinity)
                    .background(.blue)
                    .foregroundColor(.white)
                }
                
                Button(action: {
                    startWorkoutAction()
                }, label: {
                    Image(systemName: "arrowtriangle.forward.fill")
                        .font(.title)
                        .frame(maxWidth: .infinity, maxHeight: .infinity)
                })
                .frame(maxWidth: .infinity, maxHeight: .infinity)
                .background(.green)
                .foregroundColor(.white)
            } else {
                Button(action: {
                    AudioEngine.shared.playFinished()
                    nextExercise()
                }, label: {
                    Image(systemName: "arrow.forward")
                        .font(.title)
                        .frame(maxWidth: .infinity, maxHeight: .infinity)
                })
                .frame(maxWidth: .infinity, maxHeight: .infinity)
                .background(.green)
                .foregroundColor(.white)
                
                Button(action: {
                    AudioEngine.shared.playFinished()
                    bridgeModule.pauseWorkout()
                }, label: {
                   bridgeModule.isPaused ?
                        Image(systemName: "play.circle.fill")
                            .font(.title)
                            .frame(maxWidth: .infinity, maxHeight: .infinity)
                    :
                        Image(systemName: "pause.circle.fill")
                            .font(.title)
                            .frame(maxWidth: .infinity, maxHeight: .infinity)
                })
                .frame(maxWidth: .infinity, maxHeight: .infinity)
                .background(bridgeModule.isPaused  ? .mint : .yellow)
                .foregroundColor(.white)
                
                Button(action: {
                    AudioEngine.shared.playFinished()
                    completedWorkout?()
                }, label: {
                    Image(systemName: "checkmark")
                        .font(.title)
                        .frame(maxWidth: .infinity, maxHeight: .infinity)
                })
                .frame(maxWidth: .infinity, maxHeight: .infinity)
                .background(.blue)
                .foregroundColor(.white)
            }
        }
    }
    
    func nextExercise() {
        bridgeModule.nextExercise()
    }
}


struct ActionsView_Previews: PreviewProvider {
    static var previews: some View {
        ActionsView(workout: PreviewData.workout(), showAddToCalendar: true, startWorkoutAction: {})
    }
}
