package com.atmob.keyboard_android.component.base.animator.impl

import android.animation.Animator
import android.animation.AnimatorListenerAdapter
import android.animation.ValueAnimator
import android.view.View
import android.view.animation.LinearInterpolator
import com.atmob.keyboard_android.component.base.animator.ComponentAnimator

/**
 * 渐变效果的动画执行器
 */
class AlphaAnimator : ComponentAnimator() {
    private var mShowAnimator: ValueAnimator? = null
    private var mHideAnimator: ValueAnimator? = null

    /**
     * 是否显示中
     */
    private var isShow = false

    override fun show(onStart: (() -> Unit)?, onFinish: (() -> Unit)?) {
        if (isShow) {
            return
        }
        if (mShowAnimator != null && mShowAnimator!!.isRunning) {
            return
        }
        val uiComponent = getUIComponent()
        uiComponent?.asView()?.let { rootView ->
            mShowAnimator = ValueAnimator.ofFloat(0f, 1f).apply {
                duration = 300
                interpolator = LinearInterpolator()
                addUpdateListener { animation ->
                    val alpha = animation.animatedValue as Float
                    rootView.alpha = alpha
                }
                addListener(object : AnimatorListenerAdapter() {
                    override fun onAnimationStart(animation: Animator) {
                        super.onAnimationStart(animation)
                        rootView.visibility = View.VISIBLE
                        onStart?.invoke()
                    }

                    override fun onAnimationEnd(animation: Animator) {
                        super.onAnimationEnd(animation)
                        isShow = true
                        onFinish?.invoke()
                    }
                })
            }
            mShowAnimator!!.start()
        }
    }

    override fun hide(onStart: (() -> Unit)?, onFinish: (() -> Unit)?) {
        if (!isShow) {
            return
        }
        if (mHideAnimator != null && mHideAnimator!!.isRunning) {
            return
        }
        val uiComponent = getUIComponent()
        uiComponent?.asView()?.let { rootView ->
            mHideAnimator = ValueAnimator.ofFloat(1f, 0f).apply {
                duration = 200
                interpolator = LinearInterpolator()
                addUpdateListener { animation ->
                    val alpha = animation.animatedValue as Float
                    rootView.alpha = alpha
                }
                addListener(object : AnimatorListenerAdapter() {
                    override fun onAnimationStart(animation: Animator) {
                        super.onAnimationStart(animation)
                        onStart?.invoke()
                    }

                    override fun onAnimationEnd(animation: Animator) {
                        super.onAnimationEnd(animation)
                        rootView.visibility = View.GONE
                        isShow = false
                        onFinish?.invoke()
                    }
                })
            }
            mHideAnimator!!.start()
        }
    }
}