package com.simplecleaner.app.ui.guide

import android.util.Log
import com.simplecleaner.app.R
import com.simplecleaner.app.base.BaseActivity
import com.simplecleaner.app.base.jumpAction
import com.simplecleaner.app.bean.FeatureBean.Companion.BATTERY_INFO
import com.simplecleaner.app.bean.FeatureBean.Companion.JUNK_CLEAN
import com.simplecleaner.app.bean.FeatureBean.Companion.LARGE_FILE_CLEAN
import com.simplecleaner.app.bean.FeatureBean.Companion.PHOTO_COMPRESSION
import com.simplecleaner.app.bean.FeatureBean.Companion.SCREENSHOT_CLEAN
import com.simplecleaner.app.bean.FeatureBean.Companion.SIMILAR_PHOTOS
import com.simplecleaner.app.bean.FeatureBean.Companion.UNINSTALL_APP
import com.simplecleaner.app.business.ads.AdsMgr
import com.simplecleaner.app.business.ads.AdsShowCallBack
import com.simplecleaner.app.databinding.ActivityGuideExperienceBinding
import com.simplecleaner.app.ui.dialog.StoragePermissionDialog
import com.simplecleaner.app.ui.main.MainActivity
import com.simplecleaner.app.utils.PermissionUtils.requestStoragePermission

class GuideExperienceActivity : BaseActivity<ActivityGuideExperienceBinding>(
    ActivityGuideExperienceBinding::inflate
) {

    override fun initView() {
        super.initView()
        val appName = getString(R.string.app_name)
        binding.tvTip.text = getString(R.string.app_function_experience_tip, appName)
        AdsMgr.showNative(binding.flAd, R.layout.layout_admob_native_custom)
    }

    override fun handleBackCallBack() {
        binding.tvSkip.callOnClick()
    }

    override fun initListener() {
        super.initListener()

        binding.tvSkip.setOnClickListener {
            AdsMgr.showInsert(this, false, object : AdsShowCallBack() {
                override fun next() {
                    goToAc(MainActivity::class.java)
                }
            })
        }

        binding.tvBtn.setOnClickListener {

            val dialog = StoragePermissionDialog(this)

            dialog.action = {
                requestStoragePermission(launcher) {
                    Log.e(TAG, "requestStoragePermission callback")
                    if (it) {
                        val key = arrayOf(JUNK_CLEAN, SCREENSHOT_CLEAN, LARGE_FILE_CLEAN, PHOTO_COMPRESSION, SIMILAR_PHOTOS).random()
                        jumpAction(key)
                    } else {
                        val key = arrayOf(BATTERY_INFO, UNINSTALL_APP).random()
                        jumpAction(key)
                    }
                }
            }
            dialog.dialog.setOnDismissListener {
                if (!dialog.actionEd) {
                    val key = arrayOf(BATTERY_INFO, UNINSTALL_APP).random()
                    jumpAction(key)
                }
            }
            dialog.showDialog()

        }
    }
}