package com.app.baselibrary.commonUtil;

import android.os.Looper;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import com.app.baselibrary.R;
import com.app.baselibrary.base.app.BaseApplication;


/**
 * Desc: ToastUtil
 * Author: pl
 * Date: 2017-03-15 16:19
 */

public class ToastUtil {

    private static Toast sToast = null;

    public static void showMessage(final String msg) {
        showMessage(msg, Toast.LENGTH_SHORT);
    }

    public static void showLongMessage(final String msg) {
        showMessage(msg, Toast.LENGTH_LONG);
    }

    public static void showMessage(final int msg) {
        showMessage(msg, Toast.LENGTH_SHORT);
    }
    //显示在屏幕中间
    public static void showMessageCenter(final String msg) {
        showMessageCenter(msg, Toast.LENGTH_SHORT);
    }
    //显示在屏幕中间
    private static void showMessageCenter(final String msg, final int lengthShort) {
        if (Thread.currentThread() != Looper.getMainLooper().getThread()) {
            BaseApplication.getUiHandler().post(new Runnable() {
                @Override
                public void run() {
                    showMessageCenter(msg, lengthShort);
                }
            });
            return;
        }
        if (android.text.TextUtils.isEmpty(msg)) {
            return;
        }
        if (sToast != null) {
            sToast.cancel();
        }
        View toastView = LayoutInflater.from(BaseApplication.getAppContext()).inflate(R.layout.toast_message_center, null);
        TextView message = (TextView) toastView.findViewById(R.id.message);
        message.setText(msg);
        sToast = new Toast(BaseApplication.getAppContext());
        sToast.setGravity(Gravity.CENTER, 0, 0);
        sToast.setDuration(lengthShort);
        sToast.setView(toastView);
        sToast.show();
    }

    public static void showMessage(final String msg,
                                   final int len) {
        if (Thread.currentThread() != Looper.getMainLooper().getThread()) {
            BaseApplication.getUiHandler().post(new Runnable() {
                @Override
                public void run() {
                    showMessage(msg, len);
                }
            });
            return;
        }
        if (android.text.TextUtils.isEmpty(msg)) {
            return;
        }
        if (sToast != null) {
            sToast.cancel();
        }
        View toastView = LayoutInflater.from(BaseApplication.getAppContext()).inflate(R.layout.toast_message_bottom, null);
        TextView message = (TextView) toastView.findViewById(R.id.message);
        message.setText(msg);
        sToast = new Toast(BaseApplication.getAppContext());
        sToast.setGravity(Gravity.BOTTOM, 0, 20);
        sToast.setDuration(len);
        sToast.setView(toastView);
        sToast.show();
    }

    public static void showMessage(final int msg,
                                   final int len) {
        showMessage(BaseApplication.getAppContext().getString(msg), len);
    }

    public static void cancelToast() {
        if (sToast != null) {
            sToast.cancel();
            sToast = null;
        }
    }
}
