package com.easy.clean.ui.main

import android.content.Context
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.easy.clean.SpConstObject.isScoreRated
import com.easy.clean.ui.recyclebin.RecycleBinUtils.getRecycleBinData
import com.easy.clean.utils.PermissionUtils.checkNotificationPermission
import com.easy.clean.utils.PermissionUtils.checkStorePermission
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.update
import kotlinx.coroutines.launch
import java.io.File

class MainViewModel : ViewModel() {

    var dialogFlow: MutableStateFlow<String> = MutableStateFlow("")
    var nextShowDialog: Boolean = true

    fun checkDialog(context: Context) {

        if (!nextShowDialog) {
            nextShowDialog = true
            return
        }

        if (!context.checkStorePermission()) {
            dialogFlow.update { "StoreDialog" }
            return
        }

        if (!context.checkNotificationPermission()) {
            dialogFlow.update { "NotificationDialog" }
            return
        }

        if (!isScoreRated) {
            dialogFlow.update { "RateDialog" }
            return
        }
    }

    var recycleBinFileFlow: MutableStateFlow<List<File>> = MutableStateFlow(listOf())

    fun refreshRecycleBin() = viewModelScope.launch(Dispatchers.IO) {
        recycleBinFileFlow.update { getRecycleBinData() }
    }
}