package com.kdanmobile.reader.utils

import android.animation.Animator
import android.animation.AnimatorListenerAdapter
import android.animation.AnimatorSet
import android.animation.ObjectAnimator
import android.animation.PropertyValuesHolder
import android.animation.ValueAnimator
import android.os.Handler
import android.os.Looper
import android.view.View
import android.view.animation.AccelerateInterpolator
import android.view.animation.Animation
import android.view.animation.LinearInterpolator

/**
 * @项目名称:PDFReader_android_for_as
 * @包名称:com.kdanmobile.pdf.tools
 * @文件名称:AnimationUtil
 * @创建时间:15/9/12 - 上午11:29 - 11
 * @创建人:luozhipeng
 * @修改记录:
 * @修改人: --------------------------------------
 * @Copyright (c)-2015kdanmobile
 */
class AnimationUtil private constructor() {

    init {
        /* cannot be instantiated */
        throw UnsupportedOperationException("cannot be instantiated")
    }

    companion object {
        private var showTopToBottom: ObjectAnimator? = null
        private var showBottomToTop: ObjectAnimator? = null
        private var showRightToLeft: ObjectAnimator? = null
        private var showLeftToRight: ObjectAnimator? = null

        fun ViewScaleAnimation(view: View?, toScale: Float, duration: Long = 16000) {
            if (view != null && view.animation != null) {
                view.animation.start()
                return
            }
            val anim1 = ObjectAnimator.ofFloat(view, "scaleX", 1.0f, toScale, 1.0f)
            anim1.duration = duration
            anim1.repeatMode = ValueAnimator.RESTART
            anim1.repeatCount = Animation.INFINITE
            val anim2 = ObjectAnimator.ofFloat(view, "scaleY", 1.0f, toScale, 1.0f)
            anim2.duration = duration
            anim2.repeatMode = ValueAnimator.RESTART
            anim2.repeatCount = Animation.INFINITE
            //        ObjectAnimator anim3 = ObjectAnimator.ofFloat(view, "alpha",
            //                1.0f, 0.9f, 1.0f);
            //        anim3.setDuration(16000);
            //        anim3.setRepeatMode(Animation.RESTART);
            //        anim3.setRepeatCount(Animation.INFINITE);
            val animSet = AnimatorSet()
            animSet.interpolator = LinearInterpolator()
            animSet.playTogether(anim1, anim2)
            animSet.start()
        }

        private fun ImageViewAnimation(view: View, duration: Long = 16000) {
            val pvhX = PropertyValuesHolder.ofFloat("alpha", 1f, 0.4f, 1f)
            val pvhY = PropertyValuesHolder.ofFloat("scaleX", 1f, 1.2f, 1f)
            val pvhZ = PropertyValuesHolder.ofFloat("scaleY", 1f, 1.2f, 1f)
            val anim = ObjectAnimator.ofPropertyValuesHolder(view, pvhX, pvhY, pvhZ)
            anim.repeatCount = Animation.INFINITE//1->2->1的循环模式
            anim.repeatMode = ValueAnimator.RESTART//无限模式
            anim.interpolator = AccelerateInterpolator()
            anim.setDuration(duration).start()
        }

        /**
         * 显示buttons控件,从控件底部往控件顶部扩展显示
         *
         * @param view
         */
        fun showViewFromBottomToTop(view: View?, duration: Long = 300) {
            if (showBottomToTop != null && showBottomToTop!!.isRunning) {
                if (showBottomToTop?.target == view) {
                    showBottomToTop?.cancel()
                    showBottomToTop = null
                } else {
                    return
                }
            }

            if (view != null) {
                val pvhA = PropertyValuesHolder.ofFloat("alpha", view.alpha, 1.0f)
                val pvhY = PropertyValuesHolder.ofFloat("Y", Math.max(view.y, view.top.toFloat() + view.height), view.top.toFloat())
                showBottomToTop = ObjectAnimator.ofPropertyValuesHolder(view, pvhA, pvhY)
                showBottomToTop?.apply {
                    interpolator = AccelerateInterpolator()
                    addListener(object : AnimatorListenerAdapter() {
                        override fun onAnimationStart(animation: Animator) {
                            super.onAnimationStart(animation)
                            view.visibility = View.VISIBLE
                        }

                        override fun onAnimationCancel(animation: Animator?) {
                            super.onAnimationCancel(animation)
                            animation?.removeAllListeners()
                        }
                    })
                    setDuration(duration).start()
                }
            }
        }

        /**
         * 隐藏buttons控件,从控件顶部往底部回收隐藏
         *
         * @param view
         */
        fun hideViewFromTopToBottom(view: View?, duration: Long = 300) {
            if (showBottomToTop != null && showBottomToTop!!.isRunning) {
                if (showBottomToTop?.target == view) {
                    showBottomToTop?.cancel()
                    showBottomToTop = null
                } else {
                    return
                }
            }

            if (view != null) {
                val pvhA = PropertyValuesHolder.ofFloat("alpha", view.alpha, 0f)
                val pvhY = PropertyValuesHolder.ofFloat("Y", Math.min(view.y, view.top.toFloat()), view.top.toFloat() + view.height)
                showBottomToTop = ObjectAnimator.ofPropertyValuesHolder(view, pvhA, pvhY)
                showBottomToTop = ObjectAnimator.ofPropertyValuesHolder(view, pvhA, pvhY)
                showBottomToTop?.apply {
                    interpolator = AccelerateInterpolator()
                    addListener(object : AnimatorListenerAdapter() {
                        override fun onAnimationEnd(animation: Animator) {
                            super.onAnimationEnd(animation)
                            view.visibility = View.INVISIBLE
                        }

                        override fun onAnimationCancel(animation: Animator?) {
                            super.onAnimationCancel(animation)
                            animation?.removeAllListeners()
                        }
                    })
                    setDuration(duration).start()
                }
            }
        }

        fun showViewFromRightToLeft(view: View?, duration: Long = 300) {
            if (showRightToLeft != null && showRightToLeft!!.isRunning) {
                if (showRightToLeft?.target == view) {
                    showRightToLeft?.cancel()
                    showRightToLeft = null
                } else {
                    return
                }
            }

            if (view != null) {
                val pvhA = PropertyValuesHolder.ofFloat("alpha", view.alpha, 1.0f)
                val pvhX = PropertyValuesHolder.ofFloat("X", Math.min(view.x, view.left.toFloat() + view.width), view.left.toFloat())
                showRightToLeft = ObjectAnimator.ofPropertyValuesHolder(view, pvhA, pvhX)
                showRightToLeft?.apply {
                    interpolator = AccelerateInterpolator()
                    addListener(object : AnimatorListenerAdapter() {
                        override fun onAnimationStart(animation: Animator) {
                            super.onAnimationStart(animation)
                            view.visibility = View.VISIBLE
                        }

                        override fun onAnimationCancel(animation: Animator?) {
                            super.onAnimationCancel(animation)
                            animation?.removeAllListeners()
                        }
                    })
                    setDuration(duration).start()
                }
            }
        }

        fun hideViewFromLeftToRight(view: View?, duration: Long = 300) {
            if (showRightToLeft != null && showRightToLeft!!.isRunning) {
                if (showRightToLeft?.target == view) {
                    showRightToLeft?.cancel()
                    showRightToLeft = null
                } else {
                    return
                }
            }

            if (view != null) {
                val pvhA = PropertyValuesHolder.ofFloat("alpha", view.alpha, 0f)
                val pvhX = PropertyValuesHolder.ofFloat("X", Math.max(view.x, view.left.toFloat()), view.left.toFloat() + view.width)
                showRightToLeft = ObjectAnimator.ofPropertyValuesHolder(view, pvhA, pvhX)
                showRightToLeft?.apply {
                    interpolator = AccelerateInterpolator()
                    addListener(object : AnimatorListenerAdapter() {
                        override fun onAnimationEnd(animation: Animator) {
                            super.onAnimationEnd(animation)
                            view.visibility = View.INVISIBLE
                        }

                        override fun onAnimationCancel(animation: Animator?) {
                            super.onAnimationCancel(animation)
                            animation?.removeAllListeners()
                        }
                    })
                    setDuration(duration).start()
                }
            }
        }

        /**
         * 显示buttons控件,从控件顶部往控件底部扩展显示
         *
         * @param view
         */
        fun showViewFromTopToBottom(view: View?, duration: Long = 300) {
            if (showTopToBottom != null && showTopToBottom!!.isRunning) {
                if (showTopToBottom?.target == view) {
                    showTopToBottom?.cancel()
                    showTopToBottom = null
                } else {
                    return
                }
            }

            if (view != null) {
                val pvhA = PropertyValuesHolder.ofFloat("alpha", view.alpha, 1.0f)
                val pvhY = PropertyValuesHolder.ofFloat("Y", Math.min(view.y, view.top.toFloat() + view.height), view.top.toFloat())
                showTopToBottom = ObjectAnimator.ofPropertyValuesHolder(view, pvhA, pvhY)
                showTopToBottom?.apply {
                    interpolator = AccelerateInterpolator()
                    addListener(object : AnimatorListenerAdapter() {
                        override fun onAnimationStart(animation: Animator) {
                            super.onAnimationStart(animation)
                            view.visibility = View.VISIBLE
                        }

                        override fun onAnimationCancel(animation: Animator?) {
                            super.onAnimationCancel(animation)
                            animation?.removeAllListeners()
                        }
                    })
                    setDuration(duration).start()
                }
            }
        }

        /**
         * 隐藏buttons控件，从控件底部往控件顶部收缩隐藏
         *
         * @param view
         */
        fun hideViewFromBottomToTop(view: View?, duration: Long = 300) {
            if (showTopToBottom != null && showTopToBottom!!.isRunning) {
                if (showTopToBottom?.target == view) {
                    showTopToBottom?.cancel()
                    showTopToBottom = null
                } else {
                    return
                }
            }

            if (view != null) {
                val pvhA = PropertyValuesHolder.ofFloat("alpha", view.alpha, 0f)
                val pvhY = PropertyValuesHolder.ofFloat("Y", Math.max(view.y, view.top.toFloat()), view.top.toFloat() - view.height)
                showTopToBottom = ObjectAnimator.ofPropertyValuesHolder(view, pvhA, pvhY)
                showTopToBottom?.apply {
                    interpolator = AccelerateInterpolator()
                    addListener(object : AnimatorListenerAdapter() {
                        override fun onAnimationEnd(animation: Animator) {
                            super.onAnimationEnd(animation)
                            view.visibility = View.INVISIBLE
                        }

                        override fun onAnimationCancel(animation: Animator?) {
                            super.onAnimationCancel(animation)
                            animation?.removeAllListeners()
                        }
                    })
                    setDuration(duration).start()
                }
            }
        }

        fun showViewFromLeftToRight(view: View?, duration: Long = 300) {
            if (showLeftToRight != null && showLeftToRight!!.isRunning) {
                if (showLeftToRight?.target == view) {
                    showLeftToRight?.cancel()
                    showLeftToRight = null
                } else {
                    return
                }
            }

            if (view != null) {
                val pvhA = PropertyValuesHolder.ofFloat("alpha", view.alpha, 1.0f)
                val pvhX = PropertyValuesHolder.ofFloat("X", Math.max(view.x, view.left.toFloat() - view.width), view.left.toFloat())
                showLeftToRight = ObjectAnimator.ofPropertyValuesHolder(view, pvhA, pvhX)
                showLeftToRight?.apply {
                    interpolator = AccelerateInterpolator()
                    addListener(object : AnimatorListenerAdapter() {
                        override fun onAnimationStart(animation: Animator) {
                            super.onAnimationStart(animation)
                            view.visibility = View.VISIBLE
                        }

                        override fun onAnimationCancel(animation: Animator?) {
                            super.onAnimationCancel(animation)
                            animation?.removeAllListeners()
                        }
                    })
                    setDuration(duration).start()
                }
            }
        }

        fun hideViewFromRightToLeft(view: View?, duration: Long = 300) {
            if (showLeftToRight != null && showLeftToRight!!.isRunning) {
                if (showLeftToRight?.target == view) {
                    showLeftToRight?.cancel()
                    showLeftToRight = null
                } else {
                    return
                }
            }

            if (view != null) {
                val pvhA = PropertyValuesHolder.ofFloat("alpha", view.alpha, 0f)
                val pvhX = PropertyValuesHolder.ofFloat("X", Math.min(view.x, view.left.toFloat()), view.left.toFloat() - view.width)
                showLeftToRight = ObjectAnimator.ofPropertyValuesHolder(view, pvhA, pvhX)
                showLeftToRight?.apply {
                    interpolator = AccelerateInterpolator()
                    addListener(object : AnimatorListenerAdapter() {
                        override fun onAnimationEnd(animation: Animator) {
                            super.onAnimationEnd(animation)
                            view.visibility = View.INVISIBLE
                        }

                        override fun onAnimationCancel(animation: Animator?) {
                            super.onAnimationCancel(animation)
                            animation?.removeAllListeners()
                        }
                    })
                    setDuration(duration).start()
                }
            }
        }

        fun showViewAlpha(view: View?, duration: Long = 300) {
            if (view != null && view.visibility != View.VISIBLE) {
                val pvhA = PropertyValuesHolder.ofFloat("alpha", 0f, 1.0f)
                val animAlpha = ObjectAnimator.ofPropertyValuesHolder(view, pvhA)
                animAlpha.interpolator = AccelerateInterpolator()
                animAlpha.addListener(object : AnimatorListenerAdapter() {
                    override fun onAnimationStart(animation: Animator) {
                        super.onAnimationStart(animation)
                        view.visibility = View.VISIBLE
                    }
                })
                animAlpha.setDuration(duration).start()
            }
        }

        fun hideViewAlpha(view: View?, duration: Long = 300) {
            if (view != null && view.visibility == View.VISIBLE) {
                val pvhA = PropertyValuesHolder.ofFloat("alpha", 1.0f, 0f)
                val animAlpha = ObjectAnimator.ofPropertyValuesHolder(view, pvhA)
                animAlpha.interpolator = AccelerateInterpolator()
                animAlpha.addListener(object : AnimatorListenerAdapter() {
                    override fun onAnimationEnd(animation: Animator) {
                        super.onAnimationEnd(animation)
                        Handler(Looper.getMainLooper()).post { view.visibility = View.GONE }
                    }
                })
                animAlpha.setDuration(duration).start()
            }
        }

        /**
         * 结束该动画
         */
        fun cancelAnimations() {
            showTopToBottom?.cancel()
            showBottomToTop?.cancel()
            showLeftToRight?.cancel()
            showRightToLeft?.cancel()
            showTopToBottom = null
            showBottomToTop = null
            showRightToLeft = null
            showLeftToRight = null
        }
    }
}
