package com.duben.microtribe.utils;


import android.content.Context;
import android.os.Handler;
import android.os.Looper;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import com.duben.microtribe.R;

/**
 * 描述：Toast
 * 作者：孟崔广
 * 时间：2017/10/10 17:51
 * 邮箱：mengcuiguang@cashbang.cn
 */
public class ToastUtil {

    private static Handler mHandler = new Handler(Looper.getMainLooper());
    private static Toast mToast = null;

    private static Object mSynObj = new Object();

    public static void show(Context context, String msg) {
        showToast(context, msg, Toast.LENGTH_SHORT, Gravity.CENTER);
    }

    public static void showLong(Context context, String msg) {
        showToast(context, msg, Toast.LENGTH_LONG, Gravity.CENTER);
    }

    public static void show(Context context, String msg, boolean isShowToast) {
        if (isShowToast)
            showToast(context, msg, Toast.LENGTH_SHORT, Gravity.CENTER);
    }

    public static void show(Context context, int msg) {
        showToast(context, msg, Toast.LENGTH_SHORT, Gravity.CENTER);
    }

    public static void showCenter(Context context, String msg) {
        showToast(context, msg, Toast.LENGTH_SHORT, Gravity.CENTER);
    }

    public static void showCenter(Context context, int msg) {
        showToast(context, msg, Toast.LENGTH_SHORT, Gravity.CENTER);
    }

    public static void dismiss() {
        if (mToast != null) {
            if (Integer.valueOf(android.os.Build.VERSION.SDK) < 14)
                mToast.cancel();
        }
    }

    private static void showToast(final Context context, final int msg, final int len,
                                  final int gravity) {
        showToast(context, context.getResources().getString(msg), len, gravity);
    }

    private static void showToast(final Context context, final String msg, final int len,
                                  final int gravity) {
        if (msg == null|| TextUtils.isEmpty(msg)) return;

        new Thread(new Runnable() {
            public void run() {
                mHandler.post(new Runnable() {
                    @Override
                    public void run() {
                        synchronized (mSynObj) {
                            if (msg == null|| TextUtils.isEmpty(msg)) return;

                            if (mToast != null) {
                                if (Integer.valueOf(android.os.Build.VERSION.SDK) < 12)
                                    mToast.cancel();

                                View view = mToast.getView();
                                if (view != null) {
                                    if (view instanceof TextView) {
                                        TextView toastView = (TextView) view;
                                        toastView.setText(msg);
                                    }
                                }
                            } else {
                                mToast = new Toast(context);

                                TextView toastView = (TextView) LayoutInflater.from(context).inflate(R.layout.toast_view, null);
                                toastView.setText(msg);
                                mToast.setView(toastView);
                            }
                            mToast.setDuration(len);
                            mToast.setGravity(gravity, 0, 80);
                            mToast.show();
                        }
                    }
                });
            }
        }).start();
    }
}
