package com.base.appzxhy.ui.main

import android.content.Context
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.base.appzxhy.SpConstObject.isScoreRated
import com.base.appzxhy.ui.recyclebin.RecycleBinUtils.getRecycleBinData
import com.base.appzxhy.utils.PermissionUtils.checkNotificationPermission
import com.base.appzxhy.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() }
    }
}