package com.epfl.esl.sportstracker

import androidx.annotation.DrawableRes
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import com.google.firebase.database.DatabaseReference
import com.google.firebase.database.FirebaseDatabase
import java.text.SimpleDateFormat
import java.util.Calendar
import java.util.Locale
import java.util.Random

enum class SPORT(val activity: String, @DrawableRes val imageResource: Int) {
    RUNNING("Running", R.drawable.running),
    CYCLING("Cycling", R.drawable.cycling),
    SKIING("Skiing", R.drawable.skiing),
    CLIMBING("Climbing", R.drawable.climbing),
    NO_SPORT("Select activity", R.drawable.no_activity)
}

class NewRecordingViewModel: ViewModel() {

    val database: FirebaseDatabase = FirebaseDatabase.getInstance()
    val profileRef: DatabaseReference = database.getReference("Profiles")

    private var _selectedSport = MutableLiveData<SPORT>(SPORT.NO_SPORT)
    val selectedSport: LiveData<SPORT>
        get() = _selectedSport

    fun updateSelectedSport(sport: SPORT, userKey: String, device: DEVICE) {
        _selectedSport.postValue(sport)

        val activityKey = profileRef.child(userKey).child("Activities").push().key.toString()
       // val activityKey: String = Random().nextInt().toString()
        val rightNow = Calendar.getInstance()
        val df = SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.UK)
        val formattedDate: String = df.format(rightNow.time)
        profileRef.child(userKey).child("Activities").child(activityKey)
            .child("activity").setValue(sport.activity)
        profileRef.child(userKey).child("Activities").child(activityKey)
            .child("registrationTime").setValue(formattedDate)
        profileRef.child(userKey).child("Activities").child(activityKey)
            .child("device").setValue(device.value)
    }

}