package com.tgv.malware.ui.main

import android.content.Context
import androidx.lifecycle.ViewModel
import com.tgv.malware.SpConstObject.isScoreRated
import com.tgv.malware.SpConstObject.mainPageCount
import com.tgv.malware.utils.PermissionUtils.areNotificationsEnabled
import com.tgv.malware.utils.PermissionUtils.checkNotificationPermission
import com.tgv.malware.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 (!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
        }
    }
}