package com.rgt.superr.ui.main

import android.content.Context
import androidx.lifecycle.ViewModel
import com.rgt.superr.SpConstObject.isScoreRated
import com.rgt.superr.SpConstObject.mainPageCount
import com.rgt.superr.bean.FeatureBean.Companion.isTodayUsedFunction
import com.rgt.superr.utils.PermissionUtils.areNotificationsEnabled
import com.rgt.superr.utils.PermissionUtils.checkNotificationPermission
import com.rgt.superr.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
        }
    }
}