package com.zxhyis.tools.ui.`fun`


import android.annotation.SuppressLint
import android.text.format.Formatter
import android.view.View
import androidx.activity.OnBackPressedCallback
import com.bumptech.glide.Glide
import com.bumptech.glide.load.resource.gif.GifDrawable
import com.bumptech.glide.request.target.ImageViewTarget
import com.bumptech.glide.request.transition.Transition
import com.zxhyis.tools.R
import com.zxhyis.tools.base.TopActivity
import com.zxhyis.tools.databinding.ActivityResultBinding
import com.zxhyis.tools.logic.setOnClick
import com.zxhyis.tools.logic.setStatusBarTextDark
import com.zxhyis.tools.logic.startActivity
import com.zxhyis.tools.ui.home.MainActivity

class ResultActivity : TopActivity<ActivityResultBinding>() {

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

    @SuppressLint("SetTextI18n")
    override fun init() {
        when (intent.getStringExtra("from")) {
            "clean" -> {
                vb.tvInfo.text =
                    "Successfully clean up the garbage ${
                        Formatter.formatFileSize(
                            this,
                            intent.getLongExtra("clean_size", 0L)
                        )
                    }"

                vb.llCenter.visibility = View.GONE
                vb.clClean.visibility = View.GONE
            }

            "apk" -> {
                vb.tvInfo.text = "Delete successful"
                vb.llCenter.visibility = View.VISIBLE
            }

            "audio" -> {
                vb.tvInfo.text = "Delete successful"
                vb.llCenter.visibility = View.VISIBLE
            }

            "zip" -> {
                vb.tvInfo.text = "Delete successful"
                vb.llCenter.visibility = View.VISIBLE
            }

            "empty_file" -> {
                vb.tvInfo.text = "Delete successful"
                vb.llCenter.visibility = View.VISIBLE
            }

            "large_file" -> {
                vb.tvInfo.text = "Delete successful"
                vb.llCenter.visibility = View.VISIBLE
            }

            "picture" -> {
                vb.tvInfo.text = "Delete successful"
                vb.llCenter.visibility = View.VISIBLE
            }

            "doc" -> {
                vb.tvInfo.text = "Delete successful"
            }

            "power" -> {
                vb.tvInfo.text = "Successfully detected battery information once"
            }


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

            })

        vb.inResult.ivBack.setOnClick {
            startActivity<MainActivity>()
            finish()
        }

        vb.tvComplete.setOnClick {
            vb.tvComplete.setBackgroundResource(R.drawable.bg_shape_set_click)
            startActivity<MainActivity>()
            finish()
        }

        vb.tvContinue.setOnClick {
            vb.tvContinue.setBackgroundResource(R.drawable.bg_shape_complete_click)
        }

        vb.clBattery.setOnClick {

        }

        vb.clBin.setOnClick {

        }

        vb.clLargeFile.setOnClick {

        }

        onBackPressedDispatcher.addCallback(object : OnBackPressedCallback(true) {
            override fun handleOnBackPressed() {
                startActivity<MainActivity>()
                finish()
            }

        })


    }

    override fun onResume() {
        super.onResume()
        setStatusBarTextDark(this)
        vb.tvComplete.setBackgroundResource(R.drawable.bg_shape_set)
        vb.tvContinue.setBackgroundResource(R.drawable.bg_shape_complete)
    }

}