package com.sheep.jiuyan.samllsheep.utils;

import android.content.Context;
import android.view.Gravity;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;

import com.sheep.gamegroup.model.entity.BaseMessage;
import com.sheep.jiuyan.samllsheep.BuildConfig;
import com.sheep.jiuyan.samllsheep.SheepApp;


/**
 * Created by Dlg on 2015/6/17.
 */
public class G {

    private static Toast mToast;//全局的Toast
    public static int HEIGHT = 1280;//屏幕高
    public static int WIDTH = 720;//屏幕宽
    public static float DENSITY = 2;//屏幕的Density

    /**
     * @param str
     * @param defaultstr
     * @return 如果str为null或者长度为零，返回defaultstr，否则返回str
     */
    public static final String getString(String str, String defaultstr) {
        return str == null || str.length() < 1 ? defaultstr : str;
    }


    /**
     * 返回像素值
     *
     * @param dp 将转换的dp值
     * @return 返回像素植
     */
    public static int getRealPix(int dp) {
        return (int) (dp * G.DENSITY);
    }


    /**
     * 全局的Toast
     *
     * @param context
     * @param msg      显示的msg，如果为null或者空表示取消显示
     * @param duration 显示的时长
     */
    public static void showToast(Context context, String msg, int duration) {
        if (mToast == null) {
            mToast = Toast.makeText(context, msg, duration);
            mToast.setGravity(Gravity.CENTER, 0, getRealPix(200));//居中靠下
            if (mToast.getView() instanceof ViewGroup
                    && ((ViewGroup) mToast.getView()).getChildAt(0) instanceof TextView) {
                ((TextView) ((ViewGroup) mToast.getView()).getChildAt(0)).setSingleLine(false);
            }
        } else if (msg == null || msg.equals(""))
            mToast.cancel();
        else {
            mToast.setText(msg);
        }
        mToast.show();
    }

    public static void showToast(String msg) {
        showToast(SheepApp.mSheepApp, msg, Toast.LENGTH_SHORT);
    }
    public static void showToast(int resId) {
        showToast(SheepApp.mSheepApp, SheepApp.mSheepApp.getString(resId), Toast.LENGTH_SHORT);
    }
    public static void showToast(BaseMessage baseMessage) {
        G.showToast(baseMessage.getMsg() + (BuildConfig.DEBUG ? "[" + baseMessage.getCode() + "]" : ""));
    }
}
