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

import SwiftUI

struct WelcomeView: View {
    @Environment(\.presentationMode) var presentationMode
    @EnvironmentObject var bluetoothManager: BluetoothManager
    @EnvironmentObject var activityRecorder: ActivityRecorder
   
    @State private var step = 0
    
    private var steps : [WelcomeStep] = [
        WelcomeStep(title: "Welcome to Gran Fondo",
                    text: "Gran Fondo is designed to help track your runs and rides in the simplest possible way.",
                    buttonText: "Get Started",
                    imageName: "figure.dance",
                    color: Color.black),
        WelcomeStep(title: "Location",
                    text: "Gran Fondo needs to know where you are to track your rides and runs.",
                    buttonText: "Next",
                    imageName: "location.fill",
                    color: Color.blue),
        WelcomeStep(title: "Health",
                    text: "Gran Fondo saves your activities on your device in Apple Health.",
                    buttonText: "Next",
                    imageName: "heart.fill",
                    color: Color.red),
        WelcomeStep(title: "Sensors",
                    text: "Gran Fondo connects to heart, speed, cadence and power sensors.",
                    buttonText: "Next",
                    imageName: "dot.radiowaves.left.and.right",
                    color: Color.blue),
        WelcomeStep(title: "Enjoy!",
                    text: "You're all set! Tap the green START button to record your run or ride",
                    buttonText: "Done",
                    imageName: "fireworks",
                    color: Color.black)]
    
    var body: some View {
        VStack(alignment: .leading, spacing: 10){
            HStack(alignment: .center){
                Image("icon")
                    .resizable()
                    .aspectRatio(contentMode: .fill)
                    .frame(width: 85, height: 85)
                    .clipped()
                    .cornerRadius(8)
                Spacer()
                
                Image(systemName: steps[step].imageName)
                    .foregroundColor(steps[step].color)
                    .frame(width: 85, height: 85)
                    .cornerRadius(8)
                    //.resizable()
                    .scaledToFit()
                    .font(.system(size:50))
                    
            }
            
            Text(steps[step].title)
                .font(.title)
                .foregroundColor(.black)
                .multilineTextAlignment(.leading)
                .lineLimit(2)
                .fontDesign(.rounded)
                .font(.system(size: 20))
                .fontWeight(.bold)
                .padding(.bottom, 10)
                .fixedSize(horizontal: false, vertical: true)
            
            Text(steps[step].text)
                .foregroundColor(.gray)
                .multilineTextAlignment(.leading)
                .font(.system(size: 18))
                .fontWeight(.semibold)
                .padding(.bottom, 10)
                .lineLimit(4)
                .fixedSize(horizontal: false, vertical: true)
            
            Button(action:{
                performAction(step)
                if(step + 1 < steps.count){
                    step = step + 1
                }else{
                    presentationMode.wrappedValue.dismiss()
                }
            }){
                Text(steps[step].buttonText)
                    .foregroundColor(Color.white)
                    .padding()
                    .frame(maxWidth: .infinity)
                    .font(.system(size: 18))
                    .fontWeight(.semibold)
                    .fontDesign(.rounded)
            }
            
            .background(steps[step].color)
            .cornerRadius(8)
            .contentShape(Rectangle())
            
        }
        .padding(50)
        //.background(.ultraThinMaterial)
        .background(Color.white)
        .presentationBackground(.clear)
        .cornerRadius(20)
        .animation(.snappy, value: step)
        .interactiveDismissDisabled()
    }
    
    func performAction(_ step: Int){
        switch(step){
        case 1:
            print("WelcomeView: performAction 1: Location")
            activityRecorder.startLocation()
        case 2:
            print("WelcomeView: performAction 2: Health")
            
            activityRecorder.startHealthKit()
        case 3:
            print("WelcomeView: performAction 3: Bluetooth")
            bluetoothManager.startBluetooth()
        case 4:
            print("performAction 4")
            activityRecorder.markAsWelcomed()
        default:
            print("No action.")
        }
        
    }
    
    struct WelcomeStep{
        let title: String
        let text: String
        let buttonText: String
        let imageName: String
        let color: Color
    }
}
