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.activity.junkclean.ScanJunkActivity
import com.base.datarecovery.adapter.AppFunctionAdapter
import com.base.datarecovery.ads.AdmobInterstitialUtils
import com.base.datarecovery.ads.AdmobNativeUtils
import com.base.datarecovery.bean.ConstObject.JUNK_CLEANER
import com.base.datarecovery.bean.ConstObject.REPEAT_PHOTOS
import com.base.datarecovery.bean.ConstObject.SCREENSHOT_CLEANER
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.SPUtils

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, RepeatActivity::class.java))
                }

                SCREENSHOT_CLEANER -> {
                    startActivity(Intent(this, ScreenShotActivity::class.java))
                }
            }
            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."
                }
                SPUtils.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() {
                AdmobInterstitialUtils.showInterstitialAd(this@ResultActivity) {
                    finishToMain()
                }
            }

        })
        binding.ivBack.setOnClickListener {
            onBackPressedDispatcher.onBackPressed()
        }

        AdmobNativeUtils.showNativeAd(this@ResultActivity, binding.flAd)
    }

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