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.junkclean.ScanJunkActivity
import com.base.filerecoveryrecyclebin.activity.privacyspace.PrivacyPinOneActivity
import com.base.filerecoveryrecyclebin.activity.privacyspace.PrivacySpaceActivity
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.JUNK_CLEANER
import com.base.filerecoveryrecyclebin.bean.ConstObject.PRIVACY_SPACE
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.bean.ConstObject.WHATSAPP_CLEANER
import com.base.filerecoveryrecyclebin.bean.ConstObject.privacyPinPassword
import com.base.filerecoveryrecyclebin.databinding.ActivityLayoutResultBinding
import com.base.filerecoveryrecyclebin.help.BaseActivity
import com.base.filerecoveryrecyclebin.help.KotlinExt.toFormatSize
import com.base.filerecoveryrecyclebin.utils.BarUtils
import com.base.filerecoveryrecyclebin.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())
            }

            WHATSAPP_CLEANER -> {
                binding.tvInfo.text = "Complete."
            }

            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) {
                        finishToMain()
                    }
                }else{
                    finishToMain()
                }
            }

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

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

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