package com.zxhyis.toolss.common.notification

import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
import android.content.Intent.FLAG_ACTIVITY_NEW_TASK
import androidx.core.app.NotificationManagerCompat
import com.blankj.utilcode.util.SPUtils
import com.zxhyis.toolss.ad.EventHelper
import com.zxhyis.toolss.ad.bean.PushCfg
import com.zxhyis.toolss.logic.LogEx
import com.zxhyis.toolss.logic.startActivity
import com.zxhyis.toolss.ui.page.BatteryInfoActivity
import com.zxhyis.toolss.ui.page.NetWorkActivity
import com.zxhyis.toolss.ui.page.PrepareScanActivity
import com.zxhyis.toolss.ui.page.RepeaterdPhotoActivity
import com.zxhyis.toolss.ui.page.SpeakerCleanerActivity
import com.zxhyis.toolss.ui.page.VirusActivity
import com.zxhyis.toolss.ui.page.appmanager.AppManager2Activity
import com.zxhyis.toolss.ui.page.bigfileclean.BigFileCleanActivity
import com.zxhyis.toolss.ui.page.notifaction.NotificationCleanActivity
import com.zxhyis.toolss.ui.page.notifaction.NotificationGuestActivity
import com.zxhyis.toolss.ui.page.photocompress.photo.StartCompressionPhotoActivity
import com.zxhyis.toolss.ui.page.recentusage.RecentAppActivity
import com.zxhyis.toolss.ui.page.speed.BatteryOptimizerActivity
import com.zxhyis.toolss.ui.page.speed.SpeedActivity


/**
 * 响应通知RemoteViews的点击事件
 */
class NotificationBroadcast : BroadcastReceiver() {
    private val TAG = "NotificationBroadcast"
    override fun onReceive(context: Context, intent: Intent?) {
        val action = intent?.action
        LogEx.logDebug(TAG, "onReceive action=$action")

        val cancel = intent?.extras?.getString("cancel")
        if (cancel != null) {
            cancelNotification(context)
        }
        val actionId = intent?.extras?.getInt("actionId")
        LogEx.logDebug(TAG, "onReceive actionId=$actionId")
        when (actionId) {
            PushCfg.ID_JUNK_CLEAN_PUSH -> {
                context.startActivity<PrepareScanActivity>(flag = FLAG_ACTIVITY_NEW_TASK)
                cancelNotification(context)
            }

            PushCfg.ID_VIRUS_PUSH -> {
                context.startActivity<VirusActivity>(flag = FLAG_ACTIVITY_NEW_TASK)
                cancelNotification(context)
            }

            PushCfg.ID_BIGFILE_PUSH -> {
                context.startActivity<BigFileCleanActivity>(flag = FLAG_ACTIVITY_NEW_TASK)
                cancelNotification(context)
            }

            PushCfg.ID_PHOTO_COMPRESS -> {
                context.startActivity<StartCompressionPhotoActivity>(flag = FLAG_ACTIVITY_NEW_TASK)
                cancelNotification(context)
            }

            PushCfg.ID_APP_MANAGER -> {
                context.startActivity<AppManager2Activity>(flag = FLAG_ACTIVITY_NEW_TASK)
                cancelNotification(context)
            }

            PushCfg.ID_NETWORK_TRAFFIC -> {
                context.startActivity<NetWorkActivity>(flag = FLAG_ACTIVITY_NEW_TASK)
                cancelNotification(context)
            }

            PushCfg.ID_CLEAN_NOTIFICATION -> {
                if (SPUtils.getInstance().getBoolean("notification_guest", false)) {
                    context.startActivity<NotificationCleanActivity>(flag = FLAG_ACTIVITY_NEW_TASK)
                } else {
                    context.startActivity<NotificationGuestActivity>(flag = FLAG_ACTIVITY_NEW_TASK)
                }
                cancelNotification(context)
            }

            PushCfg.ID_RECENT_USE_APP -> {
                context.startActivity<RecentAppActivity>(flag = FLAG_ACTIVITY_NEW_TASK)
                cancelNotification(context)
            }

            PushCfg.ID_SIMILAR_IMAGE -> {
                context.startActivity<RepeaterdPhotoActivity>(flag = FLAG_ACTIVITY_NEW_TASK)
                cancelNotification(context)
            }

            PushCfg.ID_CLEAN_SPEAKER -> {
                context.startActivity<SpeakerCleanerActivity>(flag = FLAG_ACTIVITY_NEW_TASK)
                cancelNotification(context)
            }

            PushCfg.ID_BATTERY_OPTIMIZATION -> {
                context.startActivity<BatteryOptimizerActivity>(flag = FLAG_ACTIVITY_NEW_TASK)
                cancelNotification(context)
            }

//================================被动广播=========================================

            PushCfg.ID_INSTALL_PACKAGE_PUSH -> {
                context.startActivity<VirusActivity>(flag = FLAG_ACTIVITY_NEW_TASK)
                cancelNotification(context)
            }

            PushCfg.ID_UNINSTALL_PACKAGE_PUSH -> {
                context.startActivity<PrepareScanActivity>(flag = FLAG_ACTIVITY_NEW_TASK)
                cancelNotification(context)
            }

            PushCfg.ID_CHARGE -> {
                context.startActivity<BatteryInfoActivity>(flag = FLAG_ACTIVITY_NEW_TASK)
                cancelNotification(context)
            }

            PushCfg.ID_LOW_BATTERY_PUSH -> {
                context.startActivity<BatteryInfoActivity>(flag = FLAG_ACTIVITY_NEW_TASK)
                cancelNotification(context)
            }

            PushCfg.ID_PHONE_ACCELERATE -> {
                context.startActivity<SpeedActivity>(flag = FLAG_ACTIVITY_NEW_TASK)
                cancelNotification(context)
            }
        }
        if (actionId != null) {
            EventHelper.event("NotificationBroadcast action_click actionId=$actionId")
        }
    }


    companion object {
        const val action = "com.file.notification.NotificationBroadcast"
        fun cancelNotification(context: Context) {
            val id = NotificationHelper.notificationId
            val notificationManager = NotificationManagerCompat.from(context)
            notificationManager.cancel(id)
        }

    }
}