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


import android.animation.Animator
import android.animation.Animator.AnimatorListener
import android.annotation.SuppressLint
import android.graphics.BitmapFactory
import android.net.Uri
import android.view.View
import androidx.lifecycle.lifecycleScope
import com.bumptech.glide.Glide
import com.zxhyis.tools.R
import com.zxhyis.tools.base.TopActivity
import com.zxhyis.tools.databinding.ActivityPictureDetailBinding
import com.zxhyis.tools.logic.setOnClick
import com.zxhyis.tools.logic.setStatusBarTextDark
import com.zxhyis.tools.logic.shareImage
import com.zxhyis.tools.logic.startActivity
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch
import java.io.File

class PictureDetailActivity : TopActivity<ActivityPictureDetailBinding>() {

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

    @SuppressLint("Recycle")
    override fun init() {
        val imagePath=intent.getStringExtra("image_path")
        Glide.with(this)
            .load(imagePath)
            .into(vb.imageDetail)
        vb.inImageDetail.ivBack.setOnClick {
            finish()
        }

        vb.tvDelete.setOnClick {
            lifecycleScope.launch {
                delay(500)
                delete()
                vb.apply {
                    inImageDetail.clOnly.visibility=View.GONE
                    imageDetail.visibility=View.GONE
                    llBottom.visibility=View.GONE
                    imageLoad.playAnimation()
                    imageLoad.visibility=View.VISIBLE
                    tvLoad.visibility=View.VISIBLE
                }
            }
            vb.tvDelete.setBackgroundResource(R.drawable.bg_shape_set_click)
        }

        vb.imageLoad.addAnimatorListener(object :AnimatorListener{
            override fun onAnimationStart(animation: Animator) {

            }

            override fun onAnimationEnd(animation: Animator) {
                startActivity<ResultActivity>("from" to "picture_detail")
                finish()
            }

            override fun onAnimationCancel(animation: Animator) {
            }

            override fun onAnimationRepeat(animation: Animator) {
            }

        })

        vb.tvShare.setOnClick {
            vb.tvShare.setBackgroundResource(R.drawable.bg_shape_recovery_click)
            shareImage(Uri.parse(imagePath),this)
        }


    }

    private fun delete(){
        val file=File(intent.getStringExtra("image_path").toString())
        file.delete()
    }

    override fun onResume() {
        super.onResume()
        setStatusBarTextDark(this)
        vb.tvDelete.setBackgroundResource(R.drawable.bg_shape_set)
        vb.tvShare.setBackgroundResource(R.drawable.bg_shape_recovery)
    }

}