package com.simplecleaner.app.ui.main

import android.content.Context
import androidx.lifecycle.ViewModel
import com.simplecleaner.app.SpConstObject.isScoreRated
import com.simplecleaner.app.SpConstObject.mainPageCount
import com.simplecleaner.app.bean.FeatureBean.Companion.isTodayUsedFunction
import com.simplecleaner.app.utils.PermissionUtils.areNotificationsEnabled
import com.simplecleaner.app.utils.PermissionUtils.checkNotificationPermission
import com.simplecleaner.app.utils.PermissionUtils.checkStorePermission
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.update

class MainViewModel : ViewModel() {

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

    fun checkDialog(context: Context) {

        if (!nextShowDialog) {
            nextShowDialog = true
            return
        }
        if (!isTodayUsedFunction()) {
            dialogFlow.update { "BatteryDialog" }
            return
        }

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

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

        if (!isScoreRated) {
            if (mainPageCount % 4 == 0L) {
                dialogFlow.update { "RateDialog" }
            }
            return
        }
    }
}