package com.test.basd.smartjunkcleaner.activity

import android.content.Intent
import android.graphics.Color
import androidx.activity.OnBackPressedCallback
import androidx.core.view.updatePadding
import com.blankj.utilcode.util.BarUtils
import com.blankj.utilcode.util.SPUtils
import com.test.basd.smartjunkcleaner.activity.photocompress.photo.StartCompressionPhotoActivity
import com.test.basd.smartjunkcleaner.adapter.AppFunctionAdapter
import com.test.basd.smartjunkcleaner.databinding.ActivityLayoutResultBinding
import com.test.basd.smartjunkcleaner.helps.BaseActivity
import com.test.basd.smartjunkcleaner.helps.KotlinExt.toFormatSize
import com.test.basd.smartjunkcleaner.notificationclean.NotificationGuestActivity
import com.test.basd.smartjunkcleaner.view.AFunOb.APP_MANAGER
import com.test.basd.smartjunkcleaner.view.AFunOb.BATTERY_INFO
import com.test.basd.smartjunkcleaner.view.AFunOb.EMPTY_FILE_CLEANER
import com.test.basd.smartjunkcleaner.view.AFunOb.JUNK_CLEANER
import com.test.basd.smartjunkcleaner.view.AFunOb.LARGE_FILE_CLEANER
import com.test.basd.smartjunkcleaner.view.AFunOb.NETWORK_TRAFFIC
import com.test.basd.smartjunkcleaner.view.AFunOb.NOTIFICATION_CLEANER
import com.test.basd.smartjunkcleaner.view.AFunOb.PHOTO_COMPRESS
import com.test.basd.smartjunkcleaner.view.AFunOb.RECENT_APP_USAGE
import com.test.basd.smartjunkcleaner.view.AFunOb.SIMILAR_PHOTOS

class ResultActivity : BaseActivity<ActivityLayoutResultBinding>() {
    override val binding: ActivityLayoutResultBinding by lazy {
        ActivityLayoutResultBinding.inflate(layoutInflater)
    }
    private lateinit var adapter: AppFunctionAdapter

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

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

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

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

                NOTIFICATION_CLEANER -> {
                    startActivity(Intent(this, NotificationGuestActivity::class.java))
                }

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

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

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

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

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


            }

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

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

            }


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

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

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

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

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

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

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

            PHOTO_COMPRESS -> {
                binding.tvInfo.text =
                    "Compress ${intent.getIntExtra("num", 0)} photo, ${
                        intent.getLongExtra(
                            "size",
                            0L
                        ).toFormatSize(1)
                    } space freed"
            }



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

        //load gif
//        Glide.with(this)
//            .asGif()
//            .load(R.mipmap.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) {
//
//                }
            }

        })
        binding.ivBack.setOnClickListener {
            finishToMain()
        }

    }

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

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