package com.example.exvesta.Screans.Responses

import android.util.Log
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.setValue
import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import androidx.navigation.NavHostController
import com.example.exvesta.domain.utils.Constants
import com.example.exvesta.model.Publicthings
import com.example.exvesta.model.Season_dublicate
import com.example.exvesta.model.Style_dublicate
import com.example.exvesta.model.Type_dublicate
import com.example.exvesta.model.Type_subtype_dublicate
import io.github.jan.supabase.gotrue.auth
import io.github.jan.supabase.gotrue.providers.builtin.Email
import io.github.jan.supabase.gotrue.providers.builtin.OTP
import io.github.jan.supabase.postgrest.from
import kotlinx.coroutines.launch


class MainViewModel(): ViewModel() {
    private val _loginResult = MutableLiveData<Boolean>()
    val loginResult: LiveData<Boolean> = _loginResult

    fun onSignInEmailCode(emailUser: String) {
        viewModelScope.launch {
            try {
                Constants.supabase.auth.signInWith(OTP) {
                    email = emailUser
                    createUser = false
                }

            } catch (e: Exception) {
                println(e.message.toString())

            }

        }
    }
    fun drop(){
        var selectedStyle by  mutableStateOf<Style_dublicate?>(null)
        var selectedSubtype by mutableStateOf<Type_subtype_dublicate?>(null)
        var selectedSeason by  mutableStateOf<Season_dublicate?>(null)
        selectedStyle = null
        selectedSubtype = null
        selectedSeason = null
    }

    fun onSignInEmailPassword(emailUser: String, passwordUser: String): Boolean {
        viewModelScope.launch {
            try {
                val user = Constants.supabase.auth.signInWith(Email) {
                    email = emailUser
                    password = passwordUser
                }
                println(user.toString())
                println(Constants.supabase.auth.currentUserOrNull()!!.id)
                println("Success")
                _loginResult.postValue(true)
            } catch (e: Exception) {
                println("Error")
                println(e.message.toString())
                _loginResult.postValue(false)
            }
        }
        return _loginResult.value ?: false
    }
    fun onSignUpEmail(
        emailUser: String,
        passwordUser: String,
        value: String,
        navHost: NavHostController
    ): Boolean {
        viewModelScope.launch {
            try{
                var  user =  Constants.supabase.auth.signUpWith(Email) {
                    email = emailUser
                    password = passwordUser
                }
                println(user.toString())
                println(Constants.supabase.auth.currentUserOrNull()!!.id)
                println("Success")
                _loginResult.postValue(true)
            }
            catch (e: Exception) {
                println("Error")
                println(e.message.toString())
                _loginResult.postValue(false)
            }

        }
        return _loginResult.value ?: false
    }

    fun getPublicthings(seson_id: Int, id_subtype:Int): List<Publicthings> {
        var recpubthingsList: List<Publicthings>
        var selectedType by mutableStateOf<Type_dublicate?>(null)
        var selectedSubType by mutableStateOf<Type_subtype_dublicate?>(null)
        var filteredData by mutableStateOf<List<Publicthings>>(listOf())
        var selectedSeasondub by  mutableStateOf<Season_dublicate?>(null)
        recpubthingsList = ArrayList()
        viewModelScope.launch {

            try{
                recpubthingsList = Constants.supabase.from("Publicthings").select() {
                    filter {
                        if(id_subtype != null && seson_id == null){
                            Publicthings::id_subtype eq id_subtype
                        }
                        if(seson_id != null && id_subtype == null){
                            Publicthings::seson_id eq id_subtype
                        }
                    }

                }.decodeList<Publicthings>()
         Log.d("Категори ", recpubthingsList.toString())
            }
            catch(e:Exception){
                Log.d("ошибка ", e.message.toString())
            }




        }
        return recpubthingsList
    }




}

