package com.qy.agv.util;


import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

public class ToastUtils {
	
    /** 上下文. */
    private static Context mContext = null;

    /** 显示Toast. */
    public static final int SHOW_TOAST = 0;

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

    public static void showToastByThread(Context context, String msg){
        showToastByThread(context, msg, Toast.LENGTH_SHORT);
    }

    /**
     * 主要Handler类，在线程中可用
     * what：0.提示文本信息
     */
    private static Handler baseHandler = new Handler() {

        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case SHOW_TOAST:
                    showToast(mContext,msg.getData().getString("TEXT"));
                    break;
                default:
                    break;
            }
        }
    };

    /**
     * 描述：Toast提示文本.
     * @param text  文本
     */
    public static void showToast(Context context,String text) {
        mContext = context;
        try {
            if (!StringUtils.isEmpty(text)) {
                Toast mToast = Toast.makeText(context, "", Toast.LENGTH_SHORT);
                mToast.setText(text);
//                LinearLayout linearLayout = (LinearLayout) mToast.getView();
//                TextView messageTextView = (TextView) linearLayout.getChildAt(0);
//                messageTextView.setTextSize(20);
                mToast.show();
            }
        } catch (Exception e) {
            throw e;
        }
    }

    /**
     * 描述：Toast提示文本.
     * @param text  文本
     */
    public static void showToastL(Context context,String text) {
        mContext = context;
        if(!StringUtils.isEmpty(text)){
            Toast mToast = Toast.makeText(context, "", Toast.LENGTH_LONG);
            mToast.setText(text);
            LinearLayout linearLayout = (LinearLayout) mToast.getView();
            TextView messageTextView = (TextView) linearLayout.getChildAt(0);
            messageTextView.setTextSize(20);
            mToast.show();
        }
    }
    /**
     * 描述：Toast提示文本.
     * @param text  文本
     */
    public static void showToastK(Context context,String text) {
        mContext = context;
        if(!StringUtils.isEmpty(text)){
            Toast mToast = Toast.makeText(context, "", Toast.LENGTH_SHORT);
            mToast.setText(text);
            LinearLayout linearLayout = (LinearLayout) mToast.getView();
            TextView messageTextView = (TextView) linearLayout.getChildAt(0);
            messageTextView.setTextSize(25);
            mToast.show();
        }
    }

    /**
     * 描述：Toast提示文本.
     * @param resId  文本的资源ID
     */
    public static void showToast(Context context,int resId) {
        mContext = context;
        Toast.makeText(context,""+context.getResources().getText(resId), Toast.LENGTH_SHORT).show();
    }

    /**
     * 描述：在线程中提示文本信息.
     * @param resId 要提示的字符串资源ID，消息what值为0,
     */
    public static void showToastInThread(Context context,int resId) {
        mContext = context;
        Message msg = baseHandler.obtainMessage(SHOW_TOAST);
        Bundle bundle = new Bundle();
        bundle.putString("TEXT", context.getResources().getString(resId));
        msg.setData(bundle);
        baseHandler.sendMessage(msg);
    }
    

    /**
     * 描述：在线程中提示文本信息.
     * @param text 消息what值为0
     */
    public static void showToastInThread(Context context,String text) {
        mContext = context;
        Message msg = baseHandler.obtainMessage(SHOW_TOAST);
        Bundle bundle = new Bundle();
        bundle.putString("TEXT", text);
        msg.setData(bundle);
        baseHandler.sendMessage(msg);
    }

    public static void showToastByThread(final Context context, final CharSequence msg, final int length){
        new Thread(()->{
            mHandler.post(()->{
                synchronized (synObject) {
                    if (mToast != null){
                        mToast.setText(msg);
                        mToast.setDuration(length);
                    }else{
                        mToast = Toast.makeText(context, "", length);
                        LinearLayout linearLayout = (LinearLayout) mToast.getView();
                        TextView messageTextView = (TextView) linearLayout.getChildAt(0);
                        messageTextView.setTextSize(25);
                        mToast.setText(msg);
                    }
                    mToast.show();
                }
            });
        }).start();
    }
}
