package com.common.lib_base.utils.ui;

import android.content.Context;
import android.graphics.Point;
import android.os.Handler;
import android.view.Gravity;
import android.view.WindowManager;
import android.widget.Toast;

import com.common.lib_base.utils.ui.UIUtils;

import java.util.Timer;
import java.util.TimerTask;

/**
 * Date: 2022/5/17 0017 11:20
 * Description:
 */
public class ToastUtils {

    // 在类前面声明吐司，确保在这个页面只有一个吐司
    private static Toast toast;


    public static void showShortToast(Context context, String msg) {

        if (toast == null) {
            toast = Toast.makeText(context, msg, Toast.LENGTH_SHORT);
        } else {
            // 关闭吐司显示
            toast.cancel();
            toast = Toast.makeText(context, msg, Toast.LENGTH_SHORT);
        }

        // 居中
        toast.setGravity(Gravity.CENTER, 0, 0);
        // 重新显示吐司
        toast.show();
    }

    public static void showLongToast(Context context, String msg) {

        if (toast == null) {
            toast = Toast.makeText(context, msg, Toast.LENGTH_LONG);
        } else {
            // 关闭吐司显示
            toast.cancel();
            toast = Toast.makeText(context, msg, Toast.LENGTH_LONG);
        }

        // 居中
//        toast.setGravity(Gravity.CENTER, 0, 0);
        toast.setGravity(Gravity.BOTTOM, 0, 0);
        // 重新显示吐司
        toast.show();
    }

    public static void showShortToast(String msg) {

        if (toast == null) {
            toast = Toast.makeText(UIUtils.getContext(), msg, Toast.LENGTH_SHORT);
        } else {
            // 关闭吐司显示
            toast.cancel();
            toast = Toast.makeText(UIUtils.getContext(), msg, Toast.LENGTH_SHORT);
        }

        // 居中
        toast.setGravity(Gravity.CENTER, 0, 0);
        // 重新显示吐司
        toast.show();
    }

    public static void showLongToast(String msg) {

        if (toast == null) {
            toast = Toast.makeText(UIUtils.getContext(), msg, Toast.LENGTH_LONG);
        } else {
            // 关闭吐司显示
            toast.cancel();
            toast = Toast.makeText(UIUtils.getContext(), msg, Toast.LENGTH_LONG);
        }

        // 居中
        toast.setGravity(Gravity.CENTER, 0, 0);
        // 重新显示吐司
        toast.show();
    }






    /**
     * 自定义时长
     *
     * @param msg
     * @param duration
     */
    public static void showLongToast(String msg, int duration) {

        if (toast == null) {
            toast = Toast.makeText(UIUtils.getContext(), msg, Toast.LENGTH_LONG);
        } else {
            // 关闭吐司显示
            toast.cancel();
            toast = Toast.makeText(UIUtils.getContext(), msg, Toast.LENGTH_LONG);
        }

        // 居中
        toast.setGravity(Gravity.CENTER, 0, 0);
        // 重新显示吐司
        toast.show();


        new Handler().postDelayed(new Runnable() {
            public void run() {
                toast.cancel();
            }
        }, duration * 1000);


    }

}
