package com.zxhyis.toolss.ui.page

import android.annotation.SuppressLint
import android.app.Activity
import android.content.Intent
import android.view.Gravity
import android.view.ViewGroup
import android.widget.PopupWindow
import androidx.core.view.updatePadding
import com.blankj.utilcode.util.BarUtils
import com.blankj.utilcode.util.SPUtils
import com.zxhyis.toolss.R
import com.zxhyis.toolss.ad.AdmobUtils
import com.zxhyis.toolss.ad.EventHelper
import com.zxhyis.toolss.databinding.LayoutExitWlBinding
import com.zxhyis.toolss.logic.ActivityCollector
import com.zxhyis.toolss.logic.setOnClick
import com.zxhyis.toolss.ui.AFunOb
import com.zxhyis.toolss.ui.page.appmanager.AppManager2Activity
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 org.json.JSONObject


class PopExitWindow(private val activity: Activity) : PopupWindow() {
    private var lastExitType = -1
        get() {
            return SPUtils.getInstance().getInt("lastExitType", field)
        }
        set(value) {
            field = value
            SPUtils.getInstance().put("lastExitType", value)
        }
    private val type = ++lastExitType % 11

    companion object {
        fun show(activity: Activity) {
            PopExitWindow(activity).show()
        }
    }

    init {
        width = ViewGroup.LayoutParams.MATCH_PARENT
        height = ViewGroup.LayoutParams.MATCH_PARENT
        isOutsideTouchable = false
        isFocusable = true
        isClippingEnabled = false
    }

    private val binding by lazy {
        LayoutExitWlBinding.inflate(activity.layoutInflater)
    }

    @SuppressLint("ClickableViewAccessibility")
    fun show() {
        contentView = binding.root
        activity.window.decorView.post {
            binding.root.updatePadding(top = BarUtils.getStatusBarHeight())
            BarUtils.setStatusBarLightMode(activity, false)
            showAtLocation(activity.window.decorView, Gravity.CENTER, 0, 0)
        }
        AdmobUtils.showNativeAd(activity, binding.idFlAd)
        //  MaxAdUtils.showMaxNativeAd(activity,binding.idFlAd)
        listOf(
            listOf(AFunOb.JUNK_CLEANER, PrepareScanActivity::class.java,"Clean up remaining junk files","Clean up", R.drawable.lajiqingl22144i),//垃圾清理
            listOf(AFunOb.Antivirus, VirusActivity::class.java,"Protect your device and privacy, scan for virus threats now!","Scan",R.drawable.bingdu231),//病毒
            listOf(AFunOb.LARGE_FILE_CLEANER, VirusActivity::class.java,"Clean big files to free up storage space","Clean up",R.drawable.dawenjian21455),//大文件
            listOf(AFunOb.PHOTO_COMPRESS, StartCompressionPhotoActivity::class.java,"Compress images to release more space","Compress",R.drawable.yasuo1241i2),//图片压缩
            listOf(AFunOb.SIMILAR_PHOTOS, RepeaterdPhotoActivity::class.java,"Check similar photos to release more space","Clean up",R.drawable.xiangsi1249928),//相似图片
            listOf(AFunOb.APP_MANAGER, AppManager2Activity::class.java,"Check apps size and uninstall junk apps to release storage space","Manage",R.drawable.yinyong12042),//app管理
            listOf(AFunOb.NETWORK_TRAFFIC, NetWorkActivity::class.java,"View network traffic usage and stop traffic-consuming apps.","View",R.drawable.liuliang215),//流量管理
            listOf(AFunOb.BATTERY_INFO, BatteryInfoActivity::class.java,"View phone battery consumption recently","View",R.drawable.dianch124i),//电池信息
            listOf(
                AFunOb.NOTIFICATION_CLEANER,
                if (SPUtils.getInstance().getBoolean("notification_guest", false)) {
                    NotificationCleanActivity::class.java
                } else {
                    NotificationGuestActivity::class.java
                }
            ,"Too many annoying notifications? Block and clean","View",R.drawable.tongzhi19992),//通知栏清理
            listOf(AFunOb.RECENT_APP_USAGE, RecentAppActivity::class.java,"Check and manage recently active apps","Manage",R.drawable.zuijin10092), //最近使用的app
            listOf(AFunOb.SPEAK_CLEANER, SpeakerCleanerActivity::class.java,"Clean the speaker dust and fix the low volume problem","Clean up",R.drawable.yangshenqi12414), //扬声器清理
        ).let {
            val item = it[type % it.size]
            binding.idExitImg.setImageResource(item[4] as Int)
            binding.idTvText.text="${item[2]}"
            binding.idExperienceNow.text = "${item[3]}"
            binding.idExperienceNow.setOnClickListener {
                val obj = JSONObject()
                obj.put("page", "${item[1]}")
                EventHelper.event("exit_jump_page", ext = obj)
             //   Log.e("MXL", "sssss: "+"${item[1]}" )
                dismiss()
                activity.startActivity(Intent(activity, item[1] as Class<*>))
            }
        }
        binding.idGuanbi.setOnClick(javaClass.simpleName){
            dismiss()
        }

        binding.idQuit.setOnClickListener {
            dismiss()
            ActivityCollector.finishAll()
        }
    }

    override fun dismiss() {
        super.dismiss()
        BarUtils.setStatusBarLightMode(activity, true)
    }
}