package com.ym.game.view

import android.animation.Animator
import android.animation.AnimatorSet
import android.animation.ObjectAnimator
import android.util.Log
import android.view.animation.AccelerateDecelerateInterpolator
import android.widget.ImageView
import com.ym.game.listener.CustomAnimatiorListener
import com.ym.library.utils.LogUtils

/**
 *Craeted by ${junqi.li}
 *Craeted by 2020/9/10
 */
abstract class TurntableUtils {
    private var start = 0f
    private var end = 1440f
    private var single = 1111
    private var double = 2222
    private var status = double
    private var isCancel = false
    private var objectAnimator1: ObjectAnimator? = null
    private var animatorSet: AnimatorSet? = null
    private val TAG = "TurntableUtils"


    fun initAnim() {
        animatorSet = AnimatorSet()
        animatorSet?.interpolator = AccelerateDecelerateInterpolator()
        animatorSet?.addListener(object : CustomAnimatiorListener() {
            override fun onAnimStart(animation: Animator?) {

            }

            override fun onAnimationEnd(animation: Animator?) {
                super.onAnimationEnd(animation)
                Log.d(TAG, "onAnimationEnd")
                animEnd()
                if (!isCancel) {
//                    showDialog()
                }
                isCancel = false
            }

            override fun onAnimationCancel(animation: Animator?) {
                super.onAnimationCancel(animation)
                isCancel = true
                Log.d(TAG, "onAnimationCancel")
            }
        })
    }

    fun startAnim(type: Int, img: ImageView) {
        LogUtils.e(TAG,"type="+type)
        var num = type

        if (type == 1) {
            num = 0
        } else {
            if (start >= 45f) {

            } else {
                num -= 1
            }
        }

//        if (type == 1) {
//            if (start >= 60f) {
//                num = 0
//            } else {
//                num = 1
//            }
//        } else {
//            if (start >= 60f) {
//                num = 1
//            } else {
//                num = 0
//            }
//
//        }
        LogUtils.e(TAG,"num="+num)
        end = (4 * 360f) - (num * 45f)
        LogUtils.e(TAG,"end="+end)
        objectAnimator1 = ObjectAnimator.ofFloat(img, "rotation", start, end).setDuration(3000)
        objectAnimator1?.addUpdateListener {
            val value = it.getAnimatedValue().toString().toFloat()
            val a = (value / 360f).toInt()
            val x = value - (a * 360f)

            val n = (x /120f).toInt()
            val z = x - (n * 120)
//            Log.d(
//                TAG,
//                "value:" + value + "  a:" + a + "  x:" + x + " n:" + n + "  z:" + z
//            )
      //      start = z

        }
//        Log.d(TAG, " start:" + start + " end:" + end)
        animatorSet?.play(objectAnimator1)
        animatorSet?.start()
//        start = (num * 60f)
        if (type == 1) {
            status = single
        } else {
            status = double
        }
    }

    //    abstract fun awardVideoClose()
//
//    abstract fun showDialog()
//
    abstract fun animEnd()
}