package com.zxhyis.toolss.ui.page


import android.animation.Animator
import android.animation.Animator.AnimatorListener
import android.annotation.SuppressLint
import android.content.Intent
import android.net.Uri
import android.view.View
import androidx.lifecycle.lifecycleScope
import com.bumptech.glide.Glide
import com.zxhyis.toolss.R
import com.zxhyis.toolss.base.TopActivity
import com.zxhyis.toolss.databinding.ActivityPictureDetailBinding
import com.zxhyis.toolss.logic.setOnClick
import com.zxhyis.toolss.logic.setStatusBarTextDark
import com.zxhyis.toolss.logic.shareImage
import com.zxhyis.toolss.logic.startActivity
import com.zxhyis.toolss.ui.page.result.ResultActivity
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(javaClass.simpleName) {
            finish()
        }

        vb.tvDelete.setOnClick(javaClass.simpleName) {
            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(javaClass.simpleName) {
            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()
        val intent = Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE)
        val uri = Uri.fromFile(file)
        intent.data = uri
        sendBroadcast(intent)
    }

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

}