//
//  DataView.swift
//  Gran Fondo
//
//  Created by Willem L. Middelkoop on 09/05/2024.
//

import SwiftUI
import HealthKit

struct DataView: View {
    
    @Environment(\.presentationMode) var presentationMode
    @EnvironmentObject var bluetoothManager: BluetoothManager
    @EnvironmentObject var activityRecorder: ActivityRecorder
    
    private var healthStore: HKHealthStore?
   
    
    var body: some View {
        
        VStack(alignment: .center, spacing: 0){
            HStack{
                VStack{
                    Text("total distance")
                        .font(.system(size: 20))
                        .fontWeight(.bold)
                        .textCase(.uppercase)
                        .foregroundColor(.gray)
                    Text(activityRecorder.getDistanceString(activityRecorder.totalDistanceLifetime))
                        .font(.system(size: 50).monospacedDigit())
                        .foregroundColor(.white)
                        .lineLimit(1)
                        .allowsTightening(false)
                        .frame(minWidth: 100, alignment: .center)
                }
                .padding()
                .frame(maxWidth: .infinity)
                
            }
            .background(Color(red: 0.2, green: 0.2, blue: 0.2))
            .padding()
            .cornerRadius(20)
            
            List {
                
                ForEach($activityRecorder.workouts, id: \.self) { $workout in
                    
                    VStack(alignment: .leading){
                        Text("\(activityRecorder.getActivityTypeDescription(for: workout.workoutActivityType)) -  \(activityRecorder.getDistanceString(workout.totalDistance?.doubleValue(for: .meter()) ?? 0.0)) - \(activityRecorder.getSpeedString( ((workout.totalDistance?.doubleValue(for: .meter()) ?? 0.0) / 1000 ) / (workout.duration / 3600 ), workoutType: workout.workoutActivityType))")
                             
                        // -  \(workout.startDate) test")
                            .font(.system(size: 14))
                            .fontWeight(.bold)
                            .textCase(.uppercase)
                            .foregroundColor(.primary)
                        
                        Text("\(activityRecorder.getFormattedDateString(workout.startDate))")
                            .font(.system(size: 10))
                            .fontWeight(.bold)
                            .textCase(.uppercase)
                            .foregroundColor(.gray)
                        
                        /*Text("\(activityRecorder.getSpeedString( ((workout.totalDistance?.doubleValue(for: .meter()) ?? 0.0) / 1000 ) / (workout.duration / 3600 ), workoutType: workout.workoutActivityType))")
                            .font(.system(size: 10))
                            .fontWeight(.bold)
                            .textCase(.uppercase)
                            .foregroundColor(.gray)*/
                    }
                }
            }
            .padding(0)
            
           /* List{
                ForEach(workouts) { workout in
                   Text("\(workout.type.rawValue) - \(workout.startDate)")
               }
                
            }*/
            
            
         /*  Form {
                Section(header: Text("Rides and Runs")) {
                    Text("Your activity data is stored securely on your iPhone within the Health app, fully encrypted and offline, ensuring your privacy.")
                    
                    Button("Open Apple Health app") {
                        if let url = URL(string: "x-apple-health://") {
                            UIApplication.shared.open(url)
                        }
                    }
                    
                    Text("To view your activities, you can use the Apple Health app. For deeper analysis, consider using the Health Fit app to gain more detailed insights.")
                    
                    Button("Open HealthFit app") {
                        let healthFitURL = URL(string: "healthfit://") // Assuming 'healthfit://' is the URL scheme for HealthFit
                        if UIApplication.shared.canOpenURL(healthFitURL!) {
                            UIApplication.shared.open(healthFitURL!)
                        } else {
                            // Fallback to opening in the App Store if the app isn't installed
                            let appStoreURL = URL(string: "https://apps.apple.com/us/app/healthfit/id1202650514")!
                            UIApplication.shared.open(appStoreURL)
                        }
                    }
                }
           }.frame(minHeight:400) // form
            */
        }
        
    }
    
    
}
