package com.cooleshow.base.utils;

import android.content.Context;
import android.graphics.Color;
import android.os.Handler;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.TableLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.cooleshow.base.R;

public class ToastUtil {

    public static ToastUtil instance;
    private Toast toast;

    public static ToastUtil getInstance() {
        if (null == instance) {
            instance = new ToastUtil();
        }
        return instance;
    }

    /**
     * 创建自定义Toasts ：
     */
    public void show(Context context, String toastStr) {
        if (init(toastStr, Toast.LENGTH_SHORT) == null) {
            toast = new Toast(Utils.getApp());
            toast.setText(toastStr);
            toast.setDuration(Toast.LENGTH_SHORT);
            toast.show();
        }
    }

    /**
     * 创建自定义Toasts ：
     */
    public void showShort(String toastStr) {
        if (init(toastStr, Toast.LENGTH_SHORT) == null) {
            toast = new Toast(Utils.getApp());
            toast.setText(toastStr);
            toast.setDuration(Toast.LENGTH_SHORT);
            toast.show();
        }
    }
    private Toast init(String message, int duration) {
        try {
            if (toast != null) {
                TextView text = (TextView) toast.getView();
                if (text != null) {
                    text.setText(message);
                }
            } else {
                int dp26 = SizeUtils.dp2px(26);
                int dp10 = SizeUtils.dp2px(10);
                TextView contentView = new TextView(Utils.getApp());
                contentView.setGravity(Gravity.CENTER);
                contentView.setBackgroundResource(R.drawable.shape_toast);
                contentView.setTextSize(15);
                contentView.setTextColor(Utils.getApp().getResources().getColor(R.color.white));
                contentView.setPadding(dp26, dp10, dp26, dp10);
                contentView.setText(message);
                toast = new Toast(Utils.getApp());
                toast.setGravity(Gravity.CENTER_VERTICAL, 0, 0);
                toast.setView(contentView);
            }
            toast.setDuration(duration);
            toast.show();
        } catch (Exception e) {
            Toast.makeText(Utils.getApp(), message, duration).show();
            e.printStackTrace();
        }
        return toast;
    }
}
