package com.assimilate.alltrans.common;

import android.app.Activity;
import android.graphics.Color;
import android.view.Gravity;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.ScaleAnimation;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.appcompat.content.res.AppCompatResources;

import com.assimilate.alltrans.MyApp;
import com.assimilate.alltrans.R;
import com.google.android.material.snackbar.Snackbar;

public class Widget {
    private static volatile Toast toast;

    public static void makeToast(@NonNull final Activity mActivity, @NonNull final String msg) {
        if (null != toast) {
            toast.cancel();
        }
        toast = Toast.makeText(mActivity, msg, Toast.LENGTH_SHORT);
        toast.show();
    }

    public static void makeSnackbar(@NonNull final Activity mActivity, @NonNull final String msg) {
        Snackbar snackbar = Snackbar.make(mActivity.findViewById(android.R.id.content), msg, Snackbar.LENGTH_SHORT);
        View view = snackbar.getView();
       view.setBackground(AppCompatResources.getDrawable(mActivity,R.drawable.button_r20_black_bg));

        FrameLayout.LayoutParams params = (FrameLayout.LayoutParams) view.getLayoutParams();
        params.gravity = Gravity.CENTER;
        view.setLayoutParams(params);
        // 获取Snackbar的TextView并设置文字居中
        TextView textView = view.findViewById(com.google.android.material.R.id.snackbar_text);
        if (textView != null) {
            textView.setTextAlignment(View.TEXT_ALIGNMENT_CENTER);
        }

        snackbar.show();
    }
    // 封装放大动画的方法
    public static void startScaleAnimation(final ImageView imageView) {
        final ScaleAnimation scaleAnimation = new ScaleAnimation(
                1.0f, 1.2f,  // 从1倍放大到1.2倍
                1.0f, 1.2f,  // 从1倍放大到1.2倍
                Animation.RELATIVE_TO_SELF, 0.5f,  // 以自身中心X轴为缩放中心
                Animation.RELATIVE_TO_SELF, 0.5f   // 以自身中心Y轴为缩放中心
        );
        scaleAnimation.setDuration(900);  // 动画持续时间，单位为毫秒
        scaleAnimation.setFillAfter(true);  // 动画结束后保持放大状态

        scaleAnimation.setAnimationListener(new Animation.AnimationListener() {
            private int repeatCount = 2;

            @Override
            public void onAnimationStart(Animation animation) {}

            @Override
            public void onAnimationEnd(Animation animation) {
                if (repeatCount > 1) {
                    repeatCount--;
                    imageView.startAnimation(scaleAnimation);
                }
            }

            @Override
            public void onAnimationRepeat(Animation animation) {}
        });

        // 开始动画
        imageView.startAnimation(scaleAnimation);
    }
}
