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.junkclean.ScanJunkActivity
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.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.JUNK_CLEANER
import com.base.datarecovery.bean.ConstObject.PRIVACY_SPACE
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_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.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

class ResultActivity : BaseActivity<ActivityLayoutResultBinding>() {

    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)
                    })
                }

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

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

        val from = intent.getStringExtra("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 -> {}
        }
        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()
        }
        AdmobNativeUtils.showNativeAd(
            this@ResultActivity,
            binding.flAd,
            R.layout.layout_native_custom_white
        )
    }

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