package com.zxhyis.toolss.ui.page.result


import android.annotation.SuppressLint
import android.content.Intent
import androidx.activity.OnBackPressedCallback
import androidx.core.content.ContextCompat
import androidx.core.view.updatePadding
import com.blankj.utilcode.util.BarUtils
import com.blankj.utilcode.util.SPUtils
import com.zxhyis.toolss.R
import com.zxhyis.toolss.base.TopActivity
import com.zxhyis.toolss.cx.FileHelps.toFormatSize
import com.zxhyis.toolss.databinding.ActivityResultBinding
import com.zxhyis.toolss.logic.setOnClick
import com.zxhyis.toolss.ui.AFunOb
import com.zxhyis.toolss.ui.AFunOb.APP_MANAGER
import com.zxhyis.toolss.ui.AFunOb.Antivirus
import com.zxhyis.toolss.ui.AFunOb.BATTERY_INFO
import com.zxhyis.toolss.ui.AFunOb.EMPTY_FILE_CLEANER
import com.zxhyis.toolss.ui.AFunOb.JUNK_CLEANER
import com.zxhyis.toolss.ui.AFunOb.LARGE_FILE_CLEANER
import com.zxhyis.toolss.ui.AFunOb.NETWORK_TRAFFIC
import com.zxhyis.toolss.ui.AFunOb.NOTIFICATION_CLEANER
import com.zxhyis.toolss.ui.AFunOb.PHOTO_COMPRESS
import com.zxhyis.toolss.ui.AFunOb.RECENT_APP_USAGE
import com.zxhyis.toolss.ui.AFunOb.SIMILAR_PHOTOS
import com.zxhyis.toolss.ui.AFunOb.BATTERY_OPTIMIZER
import com.zxhyis.toolss.ui.page.BatteryInfoActivity
import com.zxhyis.toolss.ui.page.EmptyFileActivity
import com.zxhyis.toolss.ui.page.NetWorkActivity
import com.zxhyis.toolss.ui.page.PrepareScanActivity
import com.zxhyis.toolss.ui.page.RepeaterdPhotoActivity
import com.zxhyis.toolss.ui.page.VirusActivity
import com.zxhyis.toolss.ui.page.appmanager.AppManager2Activity
import com.zxhyis.toolss.ui.page.bigfileclean.BigFileCleanActivity
import com.zxhyis.toolss.ui.page.notifaction.NotificationCleanActivity
import com.zxhyis.toolss.ui.page.notifaction.NotificationGuestActivity
import com.zxhyis.toolss.ui.page.photocompress.photo.StartCompressionPhotoActivity
import com.zxhyis.toolss.ui.page.recentusage.RecentAppActivity

class ResultActivity : TopActivity<ActivityResultBinding>() {

    private lateinit var adapter: AppFunctionAdapter

    override fun getViewBinding1(): ActivityResultBinding {
        return ActivityResultBinding.inflate(layoutInflater)
    }

    @SuppressLint("SetTextI18n")
    override fun init() {
        BarUtils.setStatusBarColor(this, ContextCompat.getColor(this, R.color.color_4772ff))
        vb.root.updatePadding(top = BarUtils.getStatusBarHeight())
        adapter = AppFunctionAdapter {
            when (it) {
                JUNK_CLEANER -> {
                    startActivity(Intent(this, PrepareScanActivity::class.java))
                }

                Antivirus -> {
                    startActivity(Intent(this, VirusActivity::class.java))
                }

                RECENT_APP_USAGE -> {
                    startActivity(Intent(this, RecentAppActivity::class.java))
                }

                LARGE_FILE_CLEANER -> {
                    startActivity(Intent(this, BigFileCleanActivity::class.java))
                }

                NOTIFICATION_CLEANER -> {
                    if (SPUtils.getInstance().getBoolean("notification_guest", false)) {
                        startActivity(
                            Intent(
                                this,
                                NotificationCleanActivity::class.java
                            )
                        )
                    } else {
                        startActivity(
                            Intent(
                                this,
                                NotificationGuestActivity::class.java
                            )
                        )
                    }
                }

                NETWORK_TRAFFIC -> {
                    startActivity(Intent(this, NetWorkActivity::class.java))
                }

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

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

                SIMILAR_PHOTOS -> {
                    startActivity(Intent(this, RepeaterdPhotoActivity::class.java))
                }

                EMPTY_FILE_CLEANER -> {
                    startActivity(Intent(this, EmptyFileActivity::class.java))
                }

                PHOTO_COMPRESS -> {
                    startActivity(Intent(this, StartCompressionPhotoActivity::class.java))
                }


            }

            finish()
        }
        vb.rvFun.adapter = adapter
        adapter.updateListPostion()
        adapter.notifyDataSetChanged()
        val wenBen = intent.getStringExtra("wenBen")
        vb.tvInfo.text = wenBen

        val from = intent.getStringExtra("from")
        when (from) {
            JUNK_CLEANER -> {
                if (intent.getLongExtra("clean_size", 0L) > 0) {
                    vb.tvInfo.text =
                        "Cleaned up ${
                            intent.getLongExtra("clean_size", 0L).toFormatSize(1)
                        }"
                } else {
                    vb.tvInfo.text = "No junk files found."
                }
                SPUtils.getInstance().put("last_use_junk_cleaner", System.currentTimeMillis())

            }

            Antivirus -> {
                vb.tvInfo.text = "Virus threats have been processed."
                SPUtils.getInstance().put("last_use_antivirus", System.currentTimeMillis())
            }

            RECENT_APP_USAGE -> {
                vb.tvInfo.text = ""
            }

            LARGE_FILE_CLEANER -> {
                vb.tvInfo.text = ""
            }

            NOTIFICATION_CLEANER -> {
                vb.tvInfo.text = ""
            }

            NETWORK_TRAFFIC -> {
                vb.tvInfo.text = ""
            }

            APP_MANAGER -> {
                vb.tvInfo.text = ""
            }

            BATTERY_INFO -> {
                vb.tvInfo.text = "Battery scan completed."
                SPUtils.getInstance().put("last_use_battery_info", System.currentTimeMillis())
            }

            EMPTY_FILE_CLEANER -> {
                vb.tvInfo.text = ""
            }

            PHOTO_COMPRESS -> {
                vb.tvInfo.text =
                    "Compress ${intent.getIntExtra("num", 0)} photo, ${
                        intent.getLongExtra(
                            "size",
                            0L
                        ).toFormatSize(1)
                    } space freed"
            }
            BATTERY_OPTIMIZER->{
                vb.tvInfo.text = "Optimization completed"
            }
            AFunOb.APPP_SPEED -> {
                vb.tvInfo.text = "Optimization completed"
            }


            else -> {}
        }
        from?.let {
            vb.tvTitle.text = it
            adapter.removeItem(it)
        }

        //load gif
//        Glide.with(this)
//            .asGif()
//            .load(R.drawable.success)
//            .into(object : ImageViewTarget<GifDrawable>(vb.icSuccess) {
//                override fun onResourceReady(
//                    resource: GifDrawable,
//                    transition: Transition<in GifDrawable>?
//                ) {
//                    super.onResourceReady(resource, transition)
//                    resource.setLoopCount(1)
//                    resource.start()
//                }
//
//                override fun setResource(resource: GifDrawable?) {
//                    view.setImageDrawable(resource)
//                }
//
//            })


        onBackPressedDispatcher.addCallback(object : OnBackPressedCallback(true) {
            override fun handleOnBackPressed() {
                finishToMain()
//                AdmobUtils.showInterstitialAd(this@ResultActivity) {
//
//                }
            }

        })
        vb.ivBack.setOnClick(javaClass.simpleName) {
            finishToMain()
        }

    }

    override fun onResume() {
        super.onResume()
    }

    override fun onDestroy() {
        super.onDestroy()
    }

}
