package com.audio.record.screen.test.activity

import android.animation.Animator
import android.animation.AnimatorSet
import android.animation.ObjectAnimator
import android.content.Context
import android.content.Intent
import android.graphics.Bitmap
import android.media.projection.MediaProjectionManager
import android.os.Bundle
import android.os.Handler
import android.os.Looper
import android.os.PersistableBundle
import android.view.animation.DecelerateInterpolator
import android.widget.ImageView
import android.widget.TextView
import androidx.activity.result.ActivityResultLauncher
import androidx.activity.result.contract.ActivityResultContracts
import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.isVisible
import com.audio.record.screen.test.R
import com.audio.record.screen.test.base.BaseActivity
import com.audio.record.screen.test.databinding.ActivityPlayBinding
import com.audio.record.screen.test.service.FloatingCallback
import com.audio.record.screen.test.service.FloatingWindowBridge
import com.audio.record.screen.test.service.ScreenshotAnimExecutor
import com.audio.record.screen.test.tool.Common
import com.audio.record.screen.test.tool.ScreenCaptureHelper


/**
 * Service悬浮窗中请求权限的透明Activity
 */
class ScreenshotAnimActivity : AppCompatActivity(), FloatingCallback {


    private lateinit var imageView: ImageView
    private lateinit var tv: TextView
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        Common.setStatusBarTextColor(this, true)
        setContentView(R.layout.floating_screenshot_anim)
        Common.showLog("---ScreenshotAnimActivity--onCreate")
        FloatingWindowBridge.registerCallback(this)
        imageView = findViewById<ImageView>(R.id.image)
        tv = findViewById<TextView>(R.id.tv_content)
    }


    override fun onRefreshBitmap(bitmap: Bitmap) {
        super.onRefreshBitmap(bitmap)
        Common.showLog("---ScreenshotAnimActivity--onRefreshBitmap")
        tv.isVisible = false
        imageView.setImageBitmap(bitmap)

        val fullScreenSize = Common.getFullScreenSize(this)
        val screenWidth = fullScreenSize.first
        val screenHeight = fullScreenSize.second

        val scale = 0.25f
        val targetX = (screenWidth - screenWidth * scale - 12).toFloat()
        val targetY = (screenHeight - screenHeight * scale - 14).toFloat()

     // 设置初始位置在中心
        imageView.translationX = 0f
        imageView.translationY = 0f
        imageView.scaleX = 1f
        imageView.scaleY = 1f

     // 缩放动画
        val scaleX = ObjectAnimator.ofFloat(imageView, "scaleX", 1f, scale)
        val scaleY = ObjectAnimator.ofFloat(imageView, "scaleY", 1f, scale)

        // 位移动画（目标点是右下角）
        val translateX = ObjectAnimator.ofFloat(imageView, "translationX", 0f, targetX - screenWidth / 2f)
        val translateY = ObjectAnimator.ofFloat(imageView, "translationY", 0f, targetY - screenHeight / 2f)

        val animatorSet = AnimatorSet().apply {
            playTogether(scaleX, scaleY, translateX, translateY)
            duration = 600L
            interpolator = DecelerateInterpolator()
            addListener(object : Animator.AnimatorListener {
                override fun onAnimationStart(animation: Animator) {
                    Common.showLog("-----动画开始")
                }

                override fun onAnimationEnd(animation: Animator) {
                    Common.showLog("-----动画结束")
                    Handler(Looper.getMainLooper()).postDelayed({
                        finish()
                    }, 2000)
                }

                override fun onAnimationCancel(animation: Animator) {}
                override fun onAnimationRepeat(animation: Animator) {}
            })
        }
        animatorSet.start()



    }

    override fun onResume() {
        super.onResume()
        Common.showLog("--ScreenshotAnimActivity---onResume")
    }


}