//
//  CompletedWorkoutView.swift
//  Werkout_ios
//
//  Created by Trey Tartt on 6/22/23.
//

import SwiftUI
import HealthKit

struct CompletedWorkoutView: View {
    @ObservedObject var bridgeModule = BridgeModule.shared
    @State var healthKitWorkoutData: HealthKitWorkoutData?
    @State var difficulty: Float = 0
    @State var notes: String = ""
    @State var isUploading: Bool = false
    @State var gettingHealthKitData: Bool = false
    
    var postData: [String: Any]
    let healthKitHelper = HealthKitHelper()
    let workout: Workout
    let completedWorkoutDismissed: ((Bool) -> Void)?

    @Environment(\.dismiss) var dismiss

    var body: some View {
        ZStack {
            if isUploading {
                ProgressView("Uploading")
            }
            VStack {
                WorkoutInfoView(workout: workout)
                
                Divider()
                
                HStack {
                    if let calsBurned = healthKitWorkoutData?.caloriesBurned {
                        CaloriesBurnedView(healthKitWorkoutData: $healthKitWorkoutData,
                                           calsBurned: calsBurned)
                    }
                }
                
                RateWorkoutView(difficulty: $difficulty)
                    .frame(maxHeight: 88)
                
                Divider()
                
                TextField("Notes", text: $notes)
                    .frame(height: 55)
                    .textFieldStyle(PlainTextFieldStyle())
                    .padding([.horizontal], 4)
                    .overlay(RoundedRectangle(cornerRadius: 16).stroke(Color(uiColor: .clear))).background(Color(uiColor: .init(red: 200/255, green: 200/255, blue: 200/255, alpha: 0.2)))
                    .cornerRadius(8)
                
                if gettingHealthKitData {
                    ProgressView("Getting HealthKit data")
                        .padding()
                }
                
                Spacer()
                
                Button("Upload", action: {
                    isUploading = true
                    upload(postBody: postData)
                })
                .frame(maxWidth: .infinity, alignment: .center)
                .frame(height: 44)
                .foregroundColor(.blue)
                .background(.yellow)
                .cornerRadius(8)
                .padding()
                .frame(maxWidth: .infinity)
            }
            .padding([.leading, .trailing])
        }
        .onAppear{
            bridgeModule.sendWorkoutCompleteToWatch()
        }
        .onChange(of: bridgeModule.healthKitUUID, perform: { healthKitUUID in
            if let healthKitUUID = healthKitUUID {
                gettingHealthKitData = true
                healthKitHelper.getDetails(forHealthKitUUID: healthKitUUID,
                                           completion: { healthKitWorkoutData in
                    self.healthKitWorkoutData = healthKitWorkoutData
                    gettingHealthKitData = false
                })
            }
        })
    }

    func upload(postBody: [String: Any]) {
        var _postBody = postBody
        _postBody["difficulty"] = difficulty
        _postBody["notes"] = notes
        if let healthKitUUID = bridgeModule.healthKitUUID {
            _postBody["health_kit_workout_uuid"] = healthKitUUID.uuidString
        }

        CompleteWorkoutFetchable(postData: _postBody).fetch(completion: { result in
            switch result {
            case .success(_):
                DispatchQueue.main.async {
                    bridgeModule.resetCurrentWorkout()
                    dismiss()
                    completedWorkoutDismissed?(true)
                }
            case .failure(let failure):
                DispatchQueue.main.async {
                    self.isUploading = false
                }
                print(failure)
            }
        })
    }
}

struct CompletedWorkoutView_Previews: PreviewProvider {
    static let postBody = [
        "difficulty": 1,
        "workout_start_time": Date().timeFormatForUpload,
        "workout": 1,
        "total_time": 140,
        "total_calories": Float(120.0),
        "heart_rates": [65,65,4,54,232,12]
    ] as [String : Any]
    
    static let workout = PreviewData.workout()
    
    static var previews: some View {
        CompletedWorkoutView(postData: CompletedWorkoutView_Previews.postBody,
                             workout: workout,
                             completedWorkoutDismissed: { _ in })
    }
}
