package com.secspace.lib.common.utils;

import android.content.Context;
import android.os.Handler;
import android.os.Looper;
import android.widget.Toast;

public class ToastUtil {

    private ToastUtil() {
        throw new UnsupportedOperationException("禁止实例化");
    }

    private static boolean isMainThread() {
        return Looper.getMainLooper() == Looper.myLooper();
    }

    public static void showShort(final Context context, final CharSequence text) {
        if (isMainThread()) {
            Toast.makeText(context, text, Toast.LENGTH_SHORT).show();
        } else {
            new Handler(Looper.getMainLooper()).post(new Runnable() {
                @Override
                public void run() {
                    Toast.makeText(context, text, Toast.LENGTH_SHORT).show();
                }
            });
        }
    }

    public static void showShort(final Context context, final int resId) {
        if (isMainThread()) {
            Toast.makeText(context, resId, Toast.LENGTH_SHORT).show();
        } else {
            new Handler(Looper.getMainLooper()).post(new Runnable() {
                @Override
                public void run() {
                    Toast.makeText(context, resId, Toast.LENGTH_SHORT).show();
                }
            });
        }
    }

    public static void showLong(final Context context, final CharSequence text) {
        if (isMainThread()) {
            Toast.makeText(context, text, Toast.LENGTH_LONG).show();
        } else {
            new Handler(Looper.getMainLooper()).post(new Runnable() {
                @Override
                public void run() {
                    Toast.makeText(context, text, Toast.LENGTH_LONG).show();
                }
            });
        }
    }

    public static void showLong(final Context context, final int resId) {
        if (isMainThread()) {
            Toast.makeText(context, resId, Toast.LENGTH_LONG).show();
        } else {
            new Handler(Looper.getMainLooper()).post(new Runnable() {
                @Override
                public void run() {
                    Toast.makeText(context, resId, Toast.LENGTH_LONG).show();
                }
            });
        }
    }
}
