package com.base.datarecovery.activity

import android.annotation.SuppressLint
import android.content.Intent
import android.graphics.Color
import androidx.activity.OnBackPressedCallback
import androidx.core.view.updatePadding
import com.base.datarecovery.R
import com.base.datarecovery.activity.appmanager.AppManagerAnimationActivity
import com.base.datarecovery.activity.appprocess.AppProcessAnimationActivity
import com.base.datarecovery.activity.battery.BatteryInfoAnimationActivity
import com.base.datarecovery.activity.junkclean.ScanJunkActivity
import com.base.datarecovery.activity.largefile.LargeFileAnimationActivity
import com.base.datarecovery.activity.privacyspace.PrivacyPinOneActivity
import com.base.datarecovery.activity.privacyspace.PrivacySpaceActivity
import com.base.datarecovery.activity.recovery.FileScanResultActivity
import com.base.datarecovery.activity.repeat.RepeatAnimationActivity
import com.base.datarecovery.activity.screenshot.ScreenShotAnimationActivity
import com.base.datarecovery.activity.whatsapp.WhatsAppCleanerActivity
import com.base.datarecovery.activity.whatsapp.WhatsAppCleanerAnimationActivity
import com.base.datarecovery.adapter.AppFunctionAdapter
import com.base.datarecovery.ads.AdmobMaxHelper
import com.base.datarecovery.ads.admob.AdmobInterstitialUtils
import com.base.datarecovery.ads.admob.AdmobNativeUtils
import com.base.datarecovery.bean.ConstObject.APP_MANAGER
import com.base.datarecovery.bean.ConstObject.APP_PROCESS
import com.base.datarecovery.bean.ConstObject.BATTERY_INFO
import com.base.datarecovery.bean.ConstObject.JUNK_CLEANER
import com.base.datarecovery.bean.ConstObject.LARGE_FILE
import com.base.datarecovery.bean.ConstObject.PRIVACY_SPACE
import com.base.datarecovery.bean.ConstObject.RECOVERY_AUDIOS
import com.base.datarecovery.bean.ConstObject.RECOVERY_DOCUMENTS
import com.base.datarecovery.bean.ConstObject.RECOVERY_PHOTOS
import com.base.datarecovery.bean.ConstObject.RECOVERY_VIDEOS
import com.base.datarecovery.bean.ConstObject.REPEAT_PHOTOS
import com.base.datarecovery.bean.ConstObject.SCAN_AUDIOS
import com.base.datarecovery.bean.ConstObject.SCAN_DOCUMENTS
import com.base.datarecovery.bean.ConstObject.SCAN_PHOTOS
import com.base.datarecovery.bean.ConstObject.SCAN_VIDEOS
import com.base.datarecovery.bean.ConstObject.SCREENSHOT_CLEANER
import com.base.datarecovery.bean.ConstObject.WHATSAPP_CLEANER
import com.base.datarecovery.bean.ConstObject.privacyPinPassword
import com.base.datarecovery.databinding.ActivityLayoutResultBinding
import com.base.datarecovery.help.BaseActivity
import com.base.datarecovery.help.KotlinExt.toFormatSize
import com.base.datarecovery.utils.BarUtils
import com.base.datarecovery.utils.AppPreferences
import com.base.datarecovery.utils.LogEx

class ResultActivity : BaseActivity<ActivityLayoutResultBinding>() {
    private val TAG = "ResultActivity"

    override val binding: ActivityLayoutResultBinding by lazy {
        ActivityLayoutResultBinding.inflate(layoutInflater)
    }
    private lateinit var adapter: AppFunctionAdapter

    @SuppressLint("SetTextI18n", "NotifyDataSetChanged")
    override fun initView() {
        BarUtils.setStatusBarColor(this, Color.TRANSPARENT)
        binding.clTop.updatePadding(top = BarUtils.getStatusBarHeight())
        adapter = AppFunctionAdapter {
            when (it) {

                JUNK_CLEANER -> {
                    startActivity(Intent(this, ScanJunkActivity::class.java))
                }

                REPEAT_PHOTOS -> {
                    startActivity(Intent(this, RepeatAnimationActivity::class.java))
                }

                SCREENSHOT_CLEANER -> {
                    startActivity(Intent(this, ScreenShotAnimationActivity::class.java))
                }

                RECOVERY_PHOTOS -> {
                    startActivity(Intent(this, FileScanResultActivity::class.java).apply {
                        putExtra("ScanType", SCAN_PHOTOS)
                    })
                }

                RECOVERY_VIDEOS -> {
                    startActivity(Intent(this, FileScanResultActivity::class.java).apply {
                        putExtra("ScanType", SCAN_VIDEOS)
                    })
                }

                RECOVERY_DOCUMENTS -> {
                    startActivity(Intent(this, FileScanResultActivity::class.java).apply {
                        putExtra("ScanType", SCAN_DOCUMENTS)
                    })
                }

                RECOVERY_AUDIOS -> {
                    startActivity(Intent(this, FileScanResultActivity::class.java).apply {
                        putExtra("ScanType", SCAN_AUDIOS)
                    })
                }

                PRIVACY_SPACE -> {
                    if (privacyPinPassword.isNotEmpty()) {
                        startActivity(Intent(this, PrivacyPinOneActivity::class.java))
                    } else {
                        startActivity(Intent(this, PrivacySpaceActivity::class.java).apply {

                        })
                    }
                }

                WHATSAPP_CLEANER -> {
                    startActivity(Intent(this, WhatsAppCleanerAnimationActivity::class.java))
                }

                LARGE_FILE -> {
                    startActivity(Intent(this, LargeFileAnimationActivity::class.java))
                }

                APP_PROCESS -> {
                    startActivity(Intent(this, AppProcessAnimationActivity::class.java))
                }

                APP_MANAGER -> {
                    startActivity(Intent(this, AppManagerAnimationActivity::class.java))
                }

                BATTERY_INFO -> {
                    startActivity(Intent(this, BatteryInfoAnimationActivity::class.java))
                }

            }
            finish()
        }
        binding.rvFun.adapter = adapter
        adapter.updateListPosition()
        adapter.notifyDataSetChanged()

        val from = intent.extras?.getString("from")
        LogEx.logDebug(TAG, "from=$from")
        when (from) {
            JUNK_CLEANER -> {
                if (intent.getLongExtra("clean_size", 0L) > 0) {
                    val size = intent.getLongExtra("clean_size", 0L).toFormatSize(1)
                    binding.tvInfo.text = "Cleaned up $size"
                } else {
                    binding.tvInfo.text = "No junk files found."
                }
                AppPreferences.getInstance()
                    .put("last_use_junk_cleaner", System.currentTimeMillis())
            }

            else -> {
                binding.tvInfo.text = "Completed!"
            }
        }
        from?.let {
            binding.tvTitle.text = it
            adapter.removeItem(it)
        }


        onBackPressedDispatcher.addCallback(object : OnBackPressedCallback(true) {
            override fun handleOnBackPressed() {
                val s = AppPreferences.getInstance().getString("isShowBackIntAd", "0").toIntOrNull()
                    ?: 0
                if (s == 1) {
                    AdmobMaxHelper.admobMaxShowInterstitialAd(this@ResultActivity) {
                        finishToMain()
                    }
                } else {
                    finishToMain()

                }
            }
        })
        binding.ivBack.setOnClickListener {
            onBackPressedDispatcher.onBackPressed()
        }
        AdmobMaxHelper.admobMaxShowNativeAd(this@ResultActivity, binding.flAd)
    }

    override fun onDestroy() {
        super.onDestroy()
        binding.icSuccess.clearAnimation()
    }
}