package com.gamedog.vididin.main


import android.util.TimeUtils
import androidx.lifecycle.viewModelScope
import com.ama.core.architecture.appBase.vm.AppViewModel
import com.gamedog.vididin.R
import com.gamedog.vididin.beans.MainTabsItem
import com.gamedog.vididin.repository.MainRepository
import com.gamedog.vididin.repository.MainTabType
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.delay
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.map
import kotlinx.coroutines.launch
import javax.inject.Inject
import com.gamedog.vididin.main.MainUiState as UiState


@HiltViewModel
class MainViewModel @Inject constructor(
    mainRepository: MainRepository,
) : AppViewModel<UiState>() {

    override val uiStateInitialValue: UiState = UiState()

    override val uiStateFlow: Flow<UiState> = mainRepository.getMainTabsStream().map {
        it.map { type ->
            val title = when (type) {
                MainTabType.HOME -> R.string.main_tabs_home
                MainTabType.TASKS -> R.string.main_tabs_tasks
                MainTabType.MINE -> R.string.main_tabs_mine
            }

            val normalIcon = when (type) {
                MainTabType.HOME -> R.mipmap.tab_home
                MainTabType.TASKS -> R.mipmap.tab_tasks
                MainTabType.MINE -> R.mipmap.tab_my
            }

            val selectedIcon = when (type) {
                MainTabType.HOME -> R.mipmap.tab_home_selected
                MainTabType.TASKS -> R.mipmap.tab_tasks_selected
                MainTabType.MINE -> R.mipmap.tab_my_selected
            }

            MainTabsItem(title, type, normalIcon, selectedIcon) {
                viewModelScope.launch {
                    // TODO any other action you need
                }
            }
        }
    }.map {
        UiState(tabs = it)
    }



    var canBack = false
    fun startCanBackTimer() {
        viewModelScope.launch {
            canBack = true
            delay(2000)
            canBack = false
        }
    }
}


data class MainUiState(
    val tabs: List<MainTabsItem>? = null,
)