package com.ritoq.quickphone.ui.main

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