package com.cooleshow.student.widgets.helper;

import android.animation.Animator;
import android.animation.AnimatorListenerAdapter;
import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.LinearInterpolator;
import android.widget.ImageView;


import com.cooleshow.student.R;

import java.util.ArrayList;
import java.util.Random;

import androidx.constraintlayout.widget.ConstraintLayout;

/**
 * Author by pq, Date on 2022/4/7.
 */
public class LiveRoomAnimatorHelper {
    private volatile static LiveRoomAnimatorHelper mHelper;

    private static final int[] addLikeIcons = new int[]{R.drawable.icon_live_room_add_like_1,
            R.drawable.icon_live_room_add_like_2, R.drawable.icon_live_room_add_like_3,
            R.drawable.icon_live_room_add_like_4, R.drawable.icon_live_room_add_like_5,
            R.drawable.icon_live_room_add_like_6, R.drawable.icon_live_room_add_like_7,
            R.drawable.icon_live_room_add_like_8};
    private ArrayList<View> addLikeAnimaViews = new ArrayList<>();
    private ArrayList<AnimatorSet> addLikeAnimas = new ArrayList<>();

    private LiveRoomAnimatorHelper(){

    }

    public static  LiveRoomAnimatorHelper getInstance(){
        if(mHelper == null){
            synchronized (LiveRoomAnimatorHelper.class){
                if(mHelper == null){
                    mHelper = new LiveRoomAnimatorHelper();
                }
            }
        }
        return mHelper;
    }

    public void startAddLikeAnimation(Context context, ViewGroup viewGroup) {
        ImageView imageView = new ImageView(context);
        Random random = new Random();
        int i = random.nextInt(8);
        imageView.setImageResource(addLikeIcons[i]);
        imageView.setId(View.generateViewId());
        ConstraintLayout.LayoutParams layoutParams = new ConstraintLayout.LayoutParams(ConstraintLayout.LayoutParams.WRAP_CONTENT, ConstraintLayout.LayoutParams.WRAP_CONTENT);
        View targetView = viewGroup.findViewById(R.id.icon_add_like);
        layoutParams.leftToLeft = targetView.getId();
        layoutParams.rightToRight = targetView.getId();
        layoutParams.bottomToTop = targetView.getId();
        viewGroup.addView(imageView, layoutParams);
        addLikeAnimaViews.add(imageView);
        ObjectAnimator objectAnimator = ObjectAnimator.ofFloat(imageView, "translationX", 0f, 20f, 0f, -20f, 0f, 20f, 0f, -20f);
        ObjectAnimator objectAnimator2 = ObjectAnimator.ofFloat(imageView, "translationY", 0f, -600f);
        ObjectAnimator objectAnimator3 = ObjectAnimator.ofFloat(imageView, "alpha", 1.0f, 0.2f);
        AnimatorSet animatorSet = new AnimatorSet();
        LinearInterpolator linearInterpolator = new LinearInterpolator();
        animatorSet.setInterpolator(linearInterpolator);
        animatorSet.play(objectAnimator).with(objectAnimator2).with(objectAnimator3);
        animatorSet.setDuration(2000);
        animatorSet.start();
        addLikeAnimas.add(animatorSet);
        animatorSet.addListener(new AnimatorListenerAdapter() {
            @Override
            public void onAnimationEnd(Animator animation) {
                super.onAnimationEnd(animation);
                if (viewGroup != null) {
                    viewGroup.removeView(imageView);
                }
                if (addLikeAnimaViews != null) {
                    addLikeAnimaViews.remove(imageView);
                }
                if (addLikeAnimas != null) {
                    addLikeAnimas.remove(animatorSet);
                }
            }
        });
    }



    public void startBarrageViewAnimation(Context context, View targetView) {
        targetView.setVisibility(View.VISIBLE);
        ObjectAnimator objectAnimator = ObjectAnimator.ofFloat(targetView, "translationX", -300, 0f);
        ObjectAnimator objectAnimator2 = ObjectAnimator.ofFloat(targetView, "alpha", 0.2f, 1f);
        AnimatorSet animatorSet = new AnimatorSet();
        LinearInterpolator linearInterpolator = new LinearInterpolator();
        animatorSet.setInterpolator(linearInterpolator);
        animatorSet.play(objectAnimator).with(objectAnimator2);
        animatorSet.setDuration(1000);
        animatorSet.start();
        addLikeAnimaViews.add(targetView);
        addLikeAnimas.add(animatorSet);
        animatorSet.addListener(new AnimatorListenerAdapter() {
            @Override
            public void onAnimationEnd(Animator animation) {
                super.onAnimationEnd(animation);
                if (addLikeAnimaViews != null) {
                    addLikeAnimaViews.remove(targetView);
                }
                if (addLikeAnimas != null) {
                    addLikeAnimas.remove(animatorSet);
                }
            }
        });
    }

    public void releaseAnimator(){
        if (addLikeAnimaViews != null) {
            for (int i = 0; i < addLikeAnimaViews.size(); i++) {
                View view = addLikeAnimaViews.get(i);
                view.clearAnimation();
            }
        }
        if (addLikeAnimas != null) {
            for (int i = 0; i < addLikeAnimas.size(); i++) {
                AnimatorSet animatorSet = addLikeAnimas.get(i);
                animatorSet.cancel();
                animatorSet = null;
            }
        }
    }


}
