package com.test.basd.supercleanermax.activity

import android.annotation.SuppressLint
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.supercleanermax.activity.photocompress.photo.StartCompressionPhotoActivity
import com.test.basd.supercleanermax.adapter.AppFunctionAdapter
import com.test.basd.supercleanermax.databinding.ActivityLayoutResultBinding
import com.test.basd.supercleanermax.helps.BaseActivity
import com.test.basd.supercleanermax.helps.KotlinExt.toFormatSize
import com.test.basd.supercleanermax.notificationclean.NotificationCleanActivity
import com.test.basd.supercleanermax.notificationclean.NotificationGuestActivity
import com.test.basd.supercleanermax.view.AFunOb.APP_MANAGER
import com.test.basd.supercleanermax.view.AFunOb.APP_SPEED
import com.test.basd.supercleanermax.view.AFunOb.BATTERY_INFO
import com.test.basd.supercleanermax.view.AFunOb.EMPTY_FILE_CLEANER
import com.test.basd.supercleanermax.view.AFunOb.JUNK_CLEANER
import com.test.basd.supercleanermax.view.AFunOb.LARGE_FILE_CLEANER
import com.test.basd.supercleanermax.view.AFunOb.NETWORK_TRAFFIC
import com.test.basd.supercleanermax.view.AFunOb.NOTIFICATION_CLEANER
import com.test.basd.supercleanermax.view.AFunOb.PHOTO_COMPRESS
import com.test.basd.supercleanermax.view.AFunOb.RECENT_APP_USAGE
import com.test.basd.supercleanermax.view.AFunOb.SIMILAR_PHOTOS

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


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



            APP_SPEED -> {
                binding.tvInfo.text = "Optimization completed"
            }


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


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