//
//  ExternalView.swift
//  Werkout_ios
//
//  Created by Trey Tartt on 6/13/23.
//

import SwiftUI
import AVKit

struct ExternalWorkoutDetailView: View {
    @StateObject var bridgeModule = BridgeModule.shared
    
    var body: some View {
        ZStack {
            if let workout = bridgeModule.currentWorkout {
                GeometryReader { metrics in
                    VStack {
                        HStack {
                            if let currentExercise = bridgeModule.currentExercise {
                                VideoPlayerView(currentExercise: currentExercise.exercise)
                                    .frame(width: metrics.size.width * 0.6, height: metrics.size.height * 0.8)
                            }
                            
                            ExtExerciseList(workout: workout,
                                            currentExerciseIdx: bridgeModule.currentExerciseIdx)
                            .frame(width: metrics.size.width * 0.4, height: metrics.size.height * 0.8)
                        }
                        
                        ExtCountdownView()
                            .frame(width: metrics.size.width-50, height: metrics.size.height * 0.2)
                            .padding([.leading, .trailing], 50)
                            .background(Color(uiColor: UIColor.secondarySystemBackground))
                    }
                }
            } else {
                Text("nothing here bro")
            }
        }
        .frame(maxWidth: .infinity, maxHeight: .infinity)
        .background(.background)
    }
}

struct TitleView: View {
    @ObservedObject var bridgeModule = BridgeModule.shared
    
    var body: some View {
        HStack {
            if let workout = bridgeModule.currentWorkout {
                Text(workout.name)
                    .font(Font.system(size: 100))
                    .frame(maxWidth: .infinity, alignment: .leading)
            }
            
            if bridgeModule.currentWorkoutRunTimeInSeconds > -1 {
                Text("\(bridgeModule.currentWorkoutRunTimeInSeconds)")
                    .font(Font.system(size: 100))
                    .frame(maxWidth: .infinity, alignment: .trailing)
                    .padding(.trailing, 100)
            }
        }
    }
}

struct ExtExerciseList: View {
    var workout: Workout
    var currentExerciseIdx: Int
    
    var body: some View {
        List() {
            ForEach(workout.exercisesSortedByCreated_at.indices, id: \.self) { i in
                let obj = workout.exercisesSortedByCreated_at[i]
                HStack {
                    if i == currentExerciseIdx {
                        Image(systemName: "checkmark")
                            .font(Font.system(size: 75))
                            .foregroundColor(.green)
                    }
                    
                    Text(obj.exercise.name)
                        .font(Font.system(size: 75))
                        .padding()
                }
            }
        }
    }
}

struct ExtCountdownView: View {
    @StateObject var bridgeModule = BridgeModule.shared
    
    var body: some View {
        VStack {
            if let currenExercise = bridgeModule.currentExercise {
                HStack {
                    Text(currenExercise.exercise.name)
                        .font(Font.system(size: 100))
                        .frame(maxWidth: .infinity, alignment: .leading)
                    
                    if bridgeModule.currentWorkoutRunTimeInSeconds > -1 {
                        Text("\(bridgeModule.currentWorkoutRunTimeInSeconds)")
                            .font(Font.system(size: 100))
                            .frame(maxWidth: .infinity, alignment: .trailing)
                            .padding(.trailing, 100)
                    }
                }
                HStack {
                    if let duration = currenExercise.duration {
                        ProgressView(value: Float(bridgeModule.timeLeft), total: Float(duration))
                            .scaleEffect(x: 1, y: 6, anchor: .center)
                        Text("\(bridgeModule.timeLeft)")
                            .font(Font.system(size: 75))
                            .padding([.leading, .trailing])
                    } else if let reps = currenExercise.reps {
                        Text("\(reps)")
                            .font(Font.system(size: 75))
                            .padding([.leading, .trailing])
                    }
                }
            }
        }
    }
}

struct VideoPlayerView: View {
    var currentExercise: ExerciseExercise
    @State var player = AVPlayer()

    var body: some View {
        VideoPlayer(player: player)
            .onAppear{
                player = AVPlayer(url:  Bundle.main.url(forResource: "Straight_Leg_Sit_Up", withExtension: "mp4")!)
                player.play()
                
                NotificationCenter.default.addObserver(forName: .AVPlayerItemDidPlayToEndTime, object: nil, queue: .main) { _ in
                    player.seek(to: .zero)
                    player.play()
                }
            }
    }
}

struct ExternalWorkoutDetailView_Previews: PreviewProvider {
    static var bridge = BridgeModule.shared
    
    static var previews: some View {
        ExternalWorkoutDetailView().environmentObject({ () -> BridgeModule in
            let envObj = BridgeModule.shared
            envObj.currentWorkout = PreviewWorkout.workout()
            bridge.currentExercise = PreviewWorkout.workout().exercisesSortedByCreated_at.first!
            return envObj
        }() )
    }
}
