package com.zxhl.cms.widget;

import android.app.Activity;
import android.content.Context;
import android.graphics.drawable.Drawable;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.AccelerateInterpolator;
import android.view.animation.Animation;
import android.view.animation.AnimationSet;
import android.view.animation.LinearInterpolator;
import android.view.animation.TranslateAnimation;
import android.widget.FrameLayout;
import android.widget.ImageView;

import com.luck.picture.lib.tools.ScreenUtils;
import com.tencent.qcloud.tuicore.util.ScreenUtil;
import com.zxhl.cms.R;
import com.zxhl.cms.localmodle.EventBusUpdateXinDongZhiBean;

import org.greenrobot.eventbus.EventBus;

/**
 * @Description: java类作用描述
 * @Author: lidandan
 * @CreateDate: 2021/6/9 4:57 下午
 */
public class ChatSendGiftView {
    /**
     * 发送礼物动画
     *
     * @param context
     * @param startView
     */
    public static void sendGiftAnimation(Activity context, View startView, Drawable giftDrawable) {

        //得到商品图片的坐标（用于计算动画开始的坐标）（此图片控件添加到根布局，居中）
        int startLoc[] = new int[2];
        startLoc[0] = ScreenUtil.getScreenWidth(context) / 2 - ScreenUtil.dip2px(45);
        startLoc[1] = ScreenUtil.getScreenHeight(context) / 2 - ScreenUtil.dip2px(50);

        //结束控件的坐标(用于计算动画结束后的坐标)
        ImageView animProductView = new ImageView(context);
//        Drawable drawable = productView.getDrawable();
        if (giftDrawable != null) {
            animProductView.setImageDrawable(giftDrawable);
        }
        animProductView.setLayoutParams(startView.getLayoutParams());
        //创建动画层的布局
        FrameLayout animationLayout = createAnimLayout(context);

        final View view = addViewToAnimLayout(context, animationLayout, animProductView, startLoc);

        float startX = startLoc[0];
        float startY = startLoc[1];

        //商品掉落后的终点坐标：购物车起始点-父布局起始点+购物车图片的1/5
        float toX = ScreenUtil.getScreenWidth(context) / 2 - ScreenUtil.dip2px(45);
       // float toY = ChatView.titleBarLayoutLoc[1] + ScreenUtil.dip2px(30) + ScreenUtils.getStatusBarHeight(context);
        float toY =  ScreenUtil.dip2px(30) + ScreenUtils.getStatusBarHeight(context);

        //平移动画X轴 计算X轴要平移的距离，设置动画的偏移时间由透明度和缩放动画持续时间决定
        TranslateAnimation translateAnimationX = new TranslateAnimation(0, toX - startX, 0, 0);
        translateAnimationX.setStartOffset(300);
        translateAnimationX.setDuration(700);
        //设置线性插值器
        translateAnimationX.setInterpolator(new LinearInterpolator());

        //平移动画Y轴 同X轴
        TranslateAnimation translateAnimationY = new TranslateAnimation(0, 0, 0, toY - startY);
        translateAnimationY.setStartOffset(300);
        translateAnimationY.setDuration(700);
        //设置加速插值器
        translateAnimationY.setInterpolator(new AccelerateInterpolator());

        //动画集合
        final AnimationSet set = new AnimationSet(false);
        set.addAnimation(translateAnimationX);
        set.addAnimation(translateAnimationY);
        set.setAnimationListener(new Animation.AnimationListener() {
            @Override
            public void onAnimationStart(Animation animation) {
                startView.setVisibility(View.VISIBLE);
            }

            @Override
            public void onAnimationEnd(Animation animation) {
                //动画执行完成
                animationLayout.post(() -> {
                    context.runOnUiThread(() -> {
                        animationLayout.removeAllViews();
                        removeAnimLayout(context, animationLayout);
                        view.setVisibility(View.GONE);
                        animationLayout.setVisibility(View.GONE);
                        view.clearAnimation();
                        set.cancel();
                        animation.cancel();
                    });
                });
                //更新心动值
                EventBus.getDefault().post(new EventBusUpdateXinDongZhiBean());
            }

            @Override
            public void onAnimationRepeat(Animation animation) {

            }
        });
        //设置动画播放完以后消失，终止填充
        set.setFillAfter(false);
        view.startAnimation(set);
    }

    /**
     * @param
     * @return void
     * @throws
     * @Description: 创建动画层
     */
    private static FrameLayout createAnimLayout(Activity activity) {
        ViewGroup rootView = (ViewGroup) activity.getWindow().getDecorView();
        FrameLayout animLayout = new FrameLayout(activity);
        FrameLayout.LayoutParams lp = new FrameLayout.LayoutParams(FrameLayout.LayoutParams.MATCH_PARENT, FrameLayout.LayoutParams.MATCH_PARENT);
        animLayout.setLayoutParams(lp);
        animLayout.setBackgroundResource(R.color.transparent);
        rootView.addView(animLayout);
        return animLayout;
    }

    /**
     * 动画执行完成后，移除之前创建的view
     *
     * @param activity
     * @param animLayout
     */
    private static void removeAnimLayout(Activity activity, FrameLayout animLayout) {
        ViewGroup rootView = (ViewGroup) activity.getWindow().getDecorView();
        rootView.removeView(animLayout);
    }

    /**
     * @param vg       动画运行的层 这里是frameLayout
     * @param view     要运行动画的View
     * @param location 动画的起始位置
     * @return
     * @deprecated 将要执行动画的view 添加到动画层
     */
    private static View addViewToAnimLayout(Context context, ViewGroup vg, View view, int[] location) {
        int x = location[0];
        int y = location[1];
        int screenWidth = ScreenUtil.getScreenWidth(context);
        vg.addView(view);
        FrameLayout.LayoutParams lp = new FrameLayout.LayoutParams(
                view.getLayoutParams().width, view.getLayoutParams().height);
        lp.leftMargin = x;
        lp.topMargin = y;
        view.setPadding(0, 0, x < screenWidth / 2 ? ScreenUtil.dip2px(8) : 0, 0);
        view.setLayoutParams(lp);

        return view;
    }


}
