package com.base.scanqrclear.luma

import android.app.Activity
import android.content.Context
import android.content.Intent
import com.base.scanqrclear.R
import com.base.scanqrclear.luma.AdmobHelps.ID_CLEAN_JUNK
import com.base.scanqrclear.luma.AdmobHelps.ID_LARGE_FILE_CLEANER
import com.base.scanqrclear.luma.AdmobHelps.ID_PHOTO_COMPRESSION
import com.base.scanqrclear.luma.AdmobHelps.ID_SCREENSHOT_CLEAN
import com.base.scanqrclear.luma.AdmobHelps.ID_WHATSAPP
import com.base.scanqrclear.utils.ActivityLauncher

object ActivityJumpHelps {

    private var isShowStoragePermission = false

    fun start(
        context: Context,
        launcher: ActivityLauncher,
        nameId: Int
    ) {
        when(nameId) {
            R.string.junk_scan -> requestPermission(context, launcher) { CleanJunkActivity.start(context) }
            R.string.app_process -> AppProcessActivity.start(context)
            R.string.whatsapp_clean -> requestPermission(context, launcher) { WhatsappCleanActivity.start(context) }
            R.string.screenshot_clean -> requestPermission(context, launcher) { ScreenshotCleanActivity.start(context) }
            R.string.similar_photos -> requestPermission(context, launcher) { SimilarPhotosActivity.start(context) }
        }
    }

    fun startFunction(
        context: Context,
        launcher: ActivityLauncher,
        notificationId: Int
    ) {
        when (notificationId) {
//            ID_CLEAN_JUNK -> requestPermission(context, launcher) { CleanJunkActivity.start(context) }
//            ID_WHATSAPP -> requestPermission(context, launcher) { com.base.scanqrclear.luma.WhatsappCleanActivity.start(context) }
//            ID_LARGE_FILE_CLEANER -> requestPermission(context, launcher) { LargeFileCleanActivity.start(context) }
//            ID_PHOTO_COMPRESSION -> requestPermission(context, launcher) { PhotoCompressionActivity.start(context) }
//            ID_SCREENSHOT_CLEAN -> requestPermission(context, launcher) { ScreenshotCleanActivity.start(context) }
        }
    }

    fun toFunction(context: Activity, notificationId: Int) {
        if (!PermissionHelps.checkFilesAccessPermission(context)
            && (notificationId == ID_CLEAN_JUNK
                    || notificationId == ID_LARGE_FILE_CLEANER
                    || notificationId == ID_PHOTO_COMPRESSION
                    || notificationId == ID_WHATSAPP
                    || notificationId == ID_SCREENSHOT_CLEAN)) {
//            context.startActivity(Intent(context, MainActivity::class.java).apply { putExtra(KEY_NOTIFICATION_ID, notificationId) })
            return
        }

        when (notificationId) {
//            ID_CLEAN_JUNK -> CleanJunkActivity.start(context)
//            ID_WHATSAPP -> com.base.scanqrclear.luma.WhatsappCleanActivity.start(context)
//            ID_BATTERY_INFO -> BatteryInfoActivity.start(context)
//            ID_LARGE_FILE_CLEANER -> LargeFileCleanActivity.start(context)
//            ID_PHOTO_COMPRESSION -> PhotoCompressionActivity.start(context)
//            ID_SCREENSHOT_CLEAN -> ScreenshotCleanActivity.start(context)
//            ID_APP_INSTALL -> AppManagerActivity.start(context)
//            ID_APP_UNINSTALL -> AppManagerActivity.start(context)
//            ID_NOT_CLEANED -> CleanJunkActivity.start(context)
//            ID_BATTERY_LEVEL -> BatteryInfoActivity.start(context)
//            ID_APP_MANAGER -> AppManagerActivity.start(context)
//            ID_HOME -> MainActivity.start(context)
        }
    }

    fun requestPermission(
        context: Context,
        launcher: ActivityLauncher,
        block: (() -> Unit)? = null
    ) {
        if (PermissionHelps.checkFilesAccessPermission(context)) {
            block?.invoke()
        } else {
            if (isShowStoragePermission) return
            isShowStoragePermission = true
            DialogHelps.showStoragePermissionDialog(
                context,
                {
                    PermissionHelps.requestFilesAccessPermission(context, launcher, result = {
                        if (it) block?.invoke()
                        isShowStoragePermission = false
                    })
                },
                {
                    isShowStoragePermission = false
                }
            )
        }
    }
}