package com.magnify.utils.ui.living.animation;

import android.animation.Animator;
import android.animation.AnimatorListenerAdapter;
import android.animation.ObjectAnimator;
import android.animation.PropertyValuesHolder;
import android.support.v4.util.SimpleArrayMap;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.AccelerateDecelerateInterpolator;
import android.view.animation.DecelerateInterpolator;

import java.util.concurrent.LinkedBlockingQueue;

import utils.utils.UIUtils;

/**
 * 配合BubbleShowViewManager,做动画的复用管理
 */
public class BubbleChild {
    /**
     * 默认出现的位置
     */
    private static int pointY;
    private static int margin;
    private static SimpleArrayMap<String, LinkedBlockingQueue<View>> mCacheViewMaps = new SimpleArrayMap<>();

    static {
        pointY = UIUtils.getScreenHeight() - UIUtils.dip2Px(280) - UIUtils.getStatusHeight();
        margin = UIUtils.dip2Px(8);
    }

    /**
     * 需要做动画的View
     */
    private View mView;
    /**
     * X轴方向移动动画
     */
    private ObjectAnimator mTranslateXAnimator;
    /**
     * 上滑隐藏动画
     */
    private ObjectAnimator mPlayFinishAnimator;
    /**
     * 弹幕所在列的角标
     */
    private int index;
    private int[] isColumnUse;

    public BubbleChild(View mView, int index, int[] isColumnUse) {
        this.mView = mView;
        this.index = index;
        this.isColumnUse = isColumnUse;
    }

    /**
     * 返回对应的缓存起来的View
     */
    public static <E extends View> E getCacheView(Class<E> mClass) {
        LinkedBlockingQueue<View> mViews = mCacheViewMaps.get(mClass.getName());
        if (mViews == null || mViews.isEmpty()) {
            return null;
        }
        return (E) mViews.poll();
    }

    /**
     * 开启弹幕动画
     */
    public void startAnimation(ViewGroup mViewGroup) {
        ViewGroup.LayoutParams layoutParams = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        mView.setLayoutParams(layoutParams);
        mViewGroup.addView(mView);
        if (mView.getMeasuredHeight() == 0 || mView.getMeasuredWidth() == 0) {
            UIUtils.mearsureView(mView);
        }
        mView.setY(pointY - index * mView.getMeasuredHeight() - index * margin);
        mTranslateXAnimator = ObjectAnimator.ofFloat(mView, "translationX", -mView.getMeasuredWidth(), UIUtils.dip2Px(16));
        mTranslateXAnimator.setInterpolator(new AccelerateDecelerateInterpolator());
        mTranslateXAnimator.setDuration(250);
        mTranslateXAnimator.start();

        PropertyValuesHolder mAlphaHolder = PropertyValuesHolder.ofFloat("alpha", 1f, 0);
        PropertyValuesHolder mTranslationY = PropertyValuesHolder.ofFloat("translationY",
                pointY - index * mView.getMeasuredHeight() - index * margin, pointY - index * margin - (mView.getMeasuredHeight() * (index + 2)));

        mTranslateXAnimator.addListener(new AnimatorListenerAdapter() {
            @Override
            public void onAnimationEnd(Animator animation) {
                mPlayFinishAnimator.start();
            }
        });
        mPlayFinishAnimator = ObjectAnimator.ofPropertyValuesHolder(mView, mAlphaHolder, mTranslationY);
        mPlayFinishAnimator.setDuration(250);
        mPlayFinishAnimator.setInterpolator(new DecelerateInterpolator());
        mPlayFinishAnimator.setStartDelay(5000);
        mPlayFinishAnimator.addListener(new AnimatorListenerAdapter() {
            @Override
            public void onAnimationEnd(Animator animation) {
                super.onAnimationEnd(animation);
                recylerView();
                mPlayFinishAnimator = null;
                mTranslateXAnimator = null;
            }
        });
    }

    /**
     * 进行view的回收
     */
    private void recylerView() {
        isColumnUse[index] = 0;
        UIUtils.removeParent(mView);
        mView.setAlpha(1.0f);
        String mClassName = mView.getClass().getName();
        LinkedBlockingQueue<View> mViews = mCacheViewMaps.get(mClassName);
        if (mViews == null) {
            mViews = new LinkedBlockingQueue<>();
        }
        mViews.add(mView);
        mCacheViewMaps.put(mClassName, mViews);
    }

}