package org.mtransit.android.commons;

import org.mtransit.android.commons.api.SupportFactory;

import android.content.Context;
import android.graphics.Color;
import android.support.annotation.DrawableRes;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.annotation.StringRes;
import android.view.Gravity;
import android.view.View;
import android.view.WindowManager;
import android.widget.PopupWindow;
import android.widget.TextView;
import android.widget.Toast;

public final class ToastUtils implements MTLog.Loggable {

	private static final String LOG_TAG = ToastUtils.class.getSimpleName();

	@Override
	public String getLogTag() {
		return LOG_TAG;
	}

	private ToastUtils() {
	}

	public static void makeTextAndShowCentered(@Nullable Context context, @StringRes int resId) {
		makeTextAndShowCentered(context, resId, Toast.LENGTH_SHORT);
	}

	public static void makeTextAndShowCentered(@Nullable Context context, @StringRes int resId, int duration) {
		if (context == null) {
			return;
		}
		Toast toast = Toast.makeText(context, resId, duration);
		toast.setGravity(Gravity.CENTER, 0, 0);
		toast.show();
	}

	public static void makeTextAndShowCentered(@Nullable Context context, CharSequence text) {
		makeTextAndShowCentered(context, text, Toast.LENGTH_SHORT);
	}

	public static void makeTextAndShowCentered(@Nullable Context context, CharSequence text, int duration) {
		if (context == null) {
			return;
		}
		Toast toast = Toast.makeText(context, text, duration);
		toast.setGravity(Gravity.CENTER, 0, 0);
		toast.show();
	}

	public static void makeTextAndShow(@Nullable Context context, @StringRes int resId) {
		makeTextAndShow(context, resId, Toast.LENGTH_SHORT);
	}

	public static void makeTextAndShow(@Nullable Context context, @StringRes int resId, int duration) {
		if (context == null) {
			return;
		}
		Toast toast = Toast.makeText(context, resId, duration);
		toast.show();
	}

	public static void makeTextAndShow(@NonNull Context context, CharSequence text) {
		makeTextAndShow(context, text, Toast.LENGTH_SHORT);
	}

	public static void makeTextAndShow(@NonNull Context context, CharSequence text, int duration) {
		Toast toast = Toast.makeText(context, text, duration);
		toast.show();
	}

	public static boolean showTouchableToast(@Nullable Context context, @Nullable PopupWindow touchableToast, @Nullable View parent) {
		if (context == null || touchableToast == null || parent == null) {
			return false;
		}
		int bottomPaddingInPx = (int) ResourceUtils.convertSPtoPX(context, 110);
		int leftMarginInPx = (int) ResourceUtils.convertSPtoPX(context, 10);
		touchableToast.showAtLocation(parent, Gravity.LEFT | Gravity.BOTTOM, leftMarginInPx, bottomPaddingInPx);
		return true;
	}

	@Nullable
	public static PopupWindow getNewTouchableToast(@Nullable Context context, @StringRes int textResId) {
		return getNewTouchableToast(context, android.R.drawable.toast_frame, textResId);
	}

	@Nullable
	public static PopupWindow getNewTouchableToast(@Nullable Context context, @DrawableRes int toastResId, @StringRes int textResId) {
		if (context == null) {
			return null;
		}
		try {
			TextView contentView = new TextView(context);
			contentView.setText(textResId);
			contentView.setTextColor(Color.WHITE);
			PopupWindow newTouchableToast = new PopupWindow(WindowManager.LayoutParams.WRAP_CONTENT, WindowManager.LayoutParams.WRAP_CONTENT);
			newTouchableToast.setContentView(contentView);
			newTouchableToast.setTouchable(true);
			newTouchableToast.setBackgroundDrawable(SupportFactory.get().getResourcesDrawable(context.getResources(), toastResId, null));
			return newTouchableToast;
		} catch (Exception e) {
			MTLog.w(LOG_TAG, e, "Error while creating touchable toast!");
			return null;
		}
	}
}

@@ -651,16 +651,122 @@
 ame();%0A%0A
+%09public static final int TOAST_MARGIN_IN_DP = 10;%0A%09public static final int NAVIGATION_HEIGHT_IN_DP = 48;%0A%0A
 %09@Overri
@@ -2509,24 +2509,715 @@
 w parent) %7B%0A
+%09%09int additionalBottomMarginInDp = 90; // smart ad banner max height%0A%09%09return showTouchableToast(context, touchableToast, parent, additionalBottomMarginInDp);%0A%09%7D%0A%0A%09public static boolean showTouchableToast(@Nullable Context context, @Nullable PopupWindow touchableToast, @Nullable View parent, int additionalBottomMarginInDp) %7B%0A%09%09return showTouchableToast(context, touchableToast, parent,%0A%09%09%09%09NAVIGATION_HEIGHT_IN_DP + additionalBottomMarginInDp + TOAST_MARGIN_IN_DP, // bottom%0A%09%09%09%09TOAST_MARGIN_IN_DP // left%0A%09%09);%0A%09%7D%0A%0A%09public static boolean showTouchableToast(@Nullable Context context, @Nullable PopupWindow touchableToast, @Nullable View parent, int bottomMarginInDp, int leftMarginInDp) %7B%0A
 %09%09if (contex
@@ -3302,31 +3302,30 @@
 %09%09int bottom
-Padd
+Marg
 in
-g
 InPx = (int)
@@ -3366,11 +3366,24 @@
 xt, 
-110
+bottomMarginInDp
 );%0A%09
@@ -3447,18 +3447,30 @@
 ontext, 
-10
+leftMarginInDp
 );%0A%09%09tou
@@ -3561,15 +3561,14 @@
 ttom
-Padd
+Marg
 in
-g
 InPx

Allow custom additional margin for touchable toast