package com.fastest.pass.home.presentation.viewmodels

import android.util.Log
import androidx.compose.runtime.State
import androidx.compose.runtime.mutableStateOf
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.fastest.pass.app.Result
import com.fastest.pass.app.UIState
import com.fastest.pass.app.WebResponse
import com.fastest.pass.home.data.model.NewItemFormField
import com.fastest.pass.home.data.model.NewItemFormRequest
import com.fastest.pass.home.data.model.NewItemFormResponse
import com.fastest.pass.home.domain.model.CountryInfo
import com.fastest.pass.home.domain.usecase.NewItemFormUseCase
import com.fastest.pass.home.utils.NewItemFormRoute
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.launch
import javax.inject.Inject

@HiltViewModel
class NewItemFormViewModel @Inject constructor(
    private val newItemFormUseCase: NewItemFormUseCase
): ViewModel() {

    private val _router = MutableStateFlow<NewItemFormRoute>(NewItemFormRoute.OpenNoneScreen)
    val router: MutableStateFlow<NewItemFormRoute> = _router

    private val _searchText = MutableStateFlow<String>("")
    val searchText: MutableStateFlow<String> = _searchText

    private val _countriesList = MutableStateFlow<List<CountryInfo>>(emptyList())
    val countriesList: MutableStateFlow<List<CountryInfo>> = _countriesList

    private val _newItemFormResponse = mutableStateOf(UIState<WebResponse<NewItemFormResponse>>())
    val newItemFormResponse: State<UIState<WebResponse<NewItemFormResponse>>> = _newItemFormResponse

    fun addItems(vault: String, fields: List<NewItemFormField>) {
        viewModelScope.launch {
            val request = NewItemFormRequest(fields)
            newItemFormUseCase(Pair(vault, request)) { result ->
                Log.d("addItems_api", "result = $result")
                when (result) {
                    is Result.Loading -> {
                        _newItemFormResponse.value = UIState(true)
                    }
                    is Result.Success -> {
                        _newItemFormResponse.value = UIState(false, result.data)
                    }
                    is Result.Failure -> {
                        _newItemFormResponse.value = UIState(false)
                    }
                }
            }
        }
    }

    fun navigateTo(newItemFormRoute: NewItemFormRoute) {
        _router.value = newItemFormRoute
    }

    fun updateSearchText(searchText: String) {
        _searchText.value = searchText
    }

    fun getCountries(text: String, countryList: List<CountryInfo>) : List<CountryInfo> {
        var searchFilterCountries = listOf<CountryInfo>()
        var filterCountries = ArrayList<CountryInfo>()

        Log.d("searchText", "NI VM => countryList = ${countryList.size}")

        countryList.forEachIndexed { index, countryInfo ->
            if (countryInfo.name.lowercase().contains(text.lowercase()) ||
                countryInfo.flag.lowercase().contains(text.lowercase()) ||
                countryInfo.timezone.lowercase().contains(text.lowercase())) {
                Log.d("searchText", "NI VM => contains = $text")
                filterCountries.add(countryInfo)
            }
        }

        searchFilterCountries = if (text.isEmpty()) {
            countryList
        } else {
            if (filterCountries.size > 0) {
                filterCountries
            } else {
                emptyList()
            }
        }

        Log.d("searchText", "NIFS => searchFilterCountries = ${searchFilterCountries.size}")

        _countriesList.value = searchFilterCountries
        return searchFilterCountries
    }
}