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.AccelerateInterpolator
import com.atmob.keyboard_android.component.base.IUIComponent
import com.atmob.keyboard_android.component.base.animator.ComponentAnimator
import com.atmob.keyboard_android.ext.measureView

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

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

    /**
     * 组件宽高
     */
    private var mComponentSize: Pair<Int, Int>? = null

    override fun onAttachUIComponent(component: IUIComponent) {
        super.onAttachUIComponent(component)
        component.asView().let { rootView ->
            rootView.post {
                ensureInitComponentSize(rootView)
            }
        }
    }

    override fun show(onStart: (() -> Unit)?, onFinish: (() -> Unit)?) {
        if (isShow) {
            return
        }
        if (mShowAnimator != null && mShowAnimator!!.isRunning) {
            return
        }
        val uiComponent = getUIComponent()
        uiComponent?.asView()?.let { rootView ->
            // 先测量view后，才能获取到尺寸
            ensureInitComponentSize(rootView)
            mShowAnimator = ValueAnimator.ofFloat(rootView.measuredWidth.toFloat(), 0f).apply {
                duration = 300
                interpolator = AccelerateInterpolator()
                addUpdateListener { animation ->
                    val translationX = animation.animatedValue as Float
                    rootView.translationX = translationX
                }
                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 ->
            // 先测量view后，才能获取到尺寸
            ensureInitComponentSize(rootView)
            mHideAnimator = ValueAnimator.ofFloat(0f, rootView.measuredWidth.toFloat()).apply {
                duration = 200
                interpolator = AccelerateInterpolator()
                addUpdateListener { animation ->
                    val translationX = animation.animatedValue as Float
                    rootView.translationX = translationX
                }
                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()
        }
    }

    /**
     * 确保测量了组件的大小
     */
    private fun ensureInitComponentSize(rootView: View) {
        if (mComponentSize == null) {
            mComponentSize = measureView(rootView)
        }
    }
}