package com.gh.gamecenter.common.view

import android.animation.*
import android.content.Context
import android.util.AttributeSet
import android.view.MotionEvent
import android.view.View
import android.view.animation.LinearInterpolator
import android.widget.ImageView
import android.widget.RelativeLayout
import androidx.core.content.ContextCompat
import com.gh.gamecenter.common.R
import java.util.*

class LikeView : RelativeLayout {

    private val num = floatArrayOf(-35f, -25f, -15f, 0f, 15f, 25f, 35f)

    constructor(context: Context) : super(context)
    constructor(context: Context, attrs: AttributeSet) : super(context, attrs)
    constructor(context: Context, attrs: AttributeSet, defStyleAttr: Int) : super(context, attrs, defStyleAttr)

    fun runLikeAnimation(event: MotionEvent?) {
        if (event == null) return
        val iv = ImageView(context)
        val lp = LayoutParams(168, 150)
        val widthPixels = context.resources.displayMetrics.widthPixels
        val leftMargin = if (event.rawX.toInt() + 84 > widthPixels) {
            widthPixels - 168
        } else {
            event.rawX.toInt() - 84
        }
        lp.leftMargin = leftMargin
        lp.topMargin = event.rawY.toInt() - 220
        iv.setImageDrawable(ContextCompat.getDrawable(context, R.drawable.ic_double_click_like))
        iv.layoutParams = lp

        addView(iv)

        val animatorSet = AnimatorSet()
        animatorSet.play(
            scaleAni(iv, "scaleX", 1f, 2f, 50, 0)
        )
            .with(scaleAni(iv, "scaleY", 1f, 2f, 50, 0))
            //缩放动画，X轴2倍缩小至0.9倍
            .with(scaleAni(iv, "scaleX", 2f, 0.9f, 100, 50))
            //缩放动画，Y轴2倍缩放至0.9倍
            .with(scaleAni(iv, "scaleY", 2f, 0.9f, 100, 50))
            //旋转动画，随机旋转角
            .with(rotation(iv, 0, 0, num[Random().nextInt(6)]))
            //渐变透明动画，透明度从0-1
            .with(alphaAni(iv, 0F, 1F, 100, 0))
            //缩放动画，X轴0.9倍缩小至
            .with(scaleAni(iv, "scaleX", 0.9f, 1F, 50, 150))
            //缩放动画，Y轴0.9倍缩放至
            .with(scaleAni(iv, "scaleY", 0.9f, 1F, 50, 150))
            //位移动画，Y轴从0上移至600
            .with(translationY(iv, 0F, -800F, 1200, 400))
            //透明动画，从1-0
            .with(alphaAni(iv, 1F, 0F, 500, 400))
            //缩放动画，X轴1至3倍
            .with(scaleAni(iv, "scaleX", 1F, 3f, 800, 400))
            //缩放动画，Y轴1至3倍
            .with(scaleAni(iv, "scaleY", 1F, 3f, 800, 400))


        animatorSet.start()
        animatorSet.addListener(object : AnimatorListenerAdapter() {
            override fun onAnimationEnd(animation: Animator) {
                super.onAnimationEnd(animation)
                removeViewInLayout(iv)
            }
        })
    }


    private fun scaleAni(
        view: View,
        propertyName: String,
        from: Float,
        to: Float,
        time: Long,
        delayTime: Long
    ): ObjectAnimator {
        val ani: ObjectAnimator = ObjectAnimator.ofFloat(view, propertyName, from, to)
        ani.interpolator = LinearInterpolator()
        ani.startDelay = delayTime
        ani.duration = time
        return ani
    }

    private fun translationX(view: View, from: Float, to: Float, time: Long, delayTime: Long): ObjectAnimator {
        val ani: ObjectAnimator = ObjectAnimator.ofFloat(view, "translationX", from, to)
        ani.interpolator = LinearInterpolator()
        ani.startDelay = delayTime
        ani.duration = time
        return ani
    }

    private fun translationY(view: View, from: Float, to: Float, time: Long, delayTime: Long): ObjectAnimator {
        val ani: ObjectAnimator = ObjectAnimator.ofFloat(view, "translationY", from, to)
        ani.interpolator = LinearInterpolator()
        ani.startDelay = delayTime
        ani.duration = time
        return ani
    }

    private fun alphaAni(view: View, from: Float, to: Float, time: Long, delayTime: Long): ObjectAnimator {
        val ani = ObjectAnimator.ofFloat(view, "alpha", from, to)
        ani.interpolator = LinearInterpolator()
        ani.startDelay = delayTime
        ani.duration = time
        return ani
    }

    private fun rotation(view: View, time: Long, delayTime: Long, vararg values: Float): ObjectAnimator {
        val ani = ObjectAnimator.ofFloat(view, "rotation", *values)
        ani.duration = time
        ani.startDelay = delayTime
        ani.interpolator = TimeInterpolator { input -> input }
        return ani
    }
}