package com.base.filerecoveryrecyclebin.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.filerecoveryrecyclebin.activity.battery.BatteryInfoAnimationActivity
import com.base.filerecoveryrecyclebin.activity.junkclean.ScanJunkActivity
import com.base.filerecoveryrecyclebin.activity.largefile.LargeFileAnimationActivity
import com.base.filerecoveryrecyclebin.activity.recovery.FileScanResultActivity
import com.base.filerecoveryrecyclebin.activity.repeat.RepeatAnimationActivity
import com.base.filerecoveryrecyclebin.activity.screenshot.ScreenShotAnimationActivity
import com.base.filerecoveryrecyclebin.adapter.AppFunctionAdapter
import com.base.filerecoveryrecyclebin.ads.AdmobMaxHelper
import com.base.filerecoveryrecyclebin.bean.ConstObject
import com.base.filerecoveryrecyclebin.bean.ConstObject.BATTERY_INFO
import com.base.filerecoveryrecyclebin.bean.ConstObject.JUNK_CLEANER
import com.base.filerecoveryrecyclebin.bean.ConstObject.LARGE_FILE
import com.base.filerecoveryrecyclebin.bean.ConstObject.RECOVERY_AUDIOS
import com.base.filerecoveryrecyclebin.bean.ConstObject.RECOVERY_DOCUMENTS
import com.base.filerecoveryrecyclebin.bean.ConstObject.RECOVERY_PHOTOS
import com.base.filerecoveryrecyclebin.bean.ConstObject.RECOVERY_VIDEOS
import com.base.filerecoveryrecyclebin.bean.ConstObject.RECYCLE_BIN
import com.base.filerecoveryrecyclebin.bean.ConstObject.REPEAT_PHOTOS
import com.base.filerecoveryrecyclebin.bean.ConstObject.SCAN_DOCUMENTS
import com.base.filerecoveryrecyclebin.bean.ConstObject.SCAN_PHOTOS
import com.base.filerecoveryrecyclebin.bean.ConstObject.SCAN_VIDEOS
import com.base.filerecoveryrecyclebin.bean.ConstObject.SCREENSHOT_CLEANER
import com.base.filerecoveryrecyclebin.databinding.LayoutResult2Binding
import com.base.filerecoveryrecyclebin.help.BaseActivity
import com.base.filerecoveryrecyclebin.help.KotlinExt.toFormatSize
import com.base.filerecoveryrecyclebin.utils.AppPreferences
import com.base.filerecoveryrecyclebin.utils.BarUtils

class ResultActivity : BaseActivity<LayoutResult2Binding>() {

    override val binding: LayoutResult2Binding by lazy {
        LayoutResult2Binding.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(this) {
            when (it) {

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

                BATTERY_INFO -> {
                    startActivity(Intent(this, BatteryInfoAnimationActivity::class.java))
                }
                LARGE_FILE -> {
                    startActivity(Intent(this, LargeFileAnimationActivity::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", ConstObject.SCAN_AUDIOS)
                    })
                }




            }
            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())
            }
            RECYCLE_BIN -> {
                binding.tvInfo.text = "Restore Success."
            }

            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,
                        isLoading = false
                    ) {
                        finishToMain()
                    }
                } else {
                    finishToMain()
                }
            }

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

       // AdmobMaxHelper.admobMaxShowNativeAd(this@ResultActivity, binding.flAd, 0)
    }

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