package com.miya.fastcashier.utils;

import android.content.Context;
import android.view.Gravity;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.CheckResult;
import androidx.annotation.NonNull;

import es.dmoral.toasty.Toasty;


/**
 * 居中的Toasty弹窗 将图片和文字的左右排布，改为了 上下排布
 * Created by fengyu on 2018/10/8.
 */
public class CenterToasty {

    @CheckResult
    public static Toast info(@NonNull Context context, @NonNull CharSequence message, int duration, boolean withIcon) {
        Toast toast =  Toasty.info(context, message, duration, withIcon);
        transformToast(toast);
        return toast;

    }

    @CheckResult
    public static Toast info(@NonNull Context context, @NonNull CharSequence message, int duration) {
        Toast toast = Toasty.info(context, message, duration);
        transformToast(toast);
        return toast;
    }

    @CheckResult
    public static Toast info(@NonNull Context context, @NonNull CharSequence message) {
        Toast toast = Toasty.info(context, message);
        transformToast(toast);
        return toast;

    }

    @CheckResult
    public static Toast infox(@NonNull Context context, @NonNull CharSequence message, @NonNull int xOffset) {
        Toast toast = Toasty.info(context, message);
        transformToast(toast, xOffset, 0);
        return toast;
    }

    @CheckResult
    public static Toast normal(@NonNull Context context, @NonNull CharSequence message, int duration) {
        Toast toast = Toasty.normal(context, message, duration);
        transformToast(toast);
        return toast;
    }

    @CheckResult
    public static Toast warning(@NonNull Context context, @NonNull CharSequence message, int duration) {
        Toast toast = Toasty.warning(context, message, duration);
        transformToast(toast);
        return toast;
    }

    @CheckResult
    public static Toast warning(@NonNull Context context, @NonNull CharSequence message, int duration, boolean withIcon) {
        Toast toast = Toasty.warning(context, message, duration, withIcon);
        transformToast(toast);
        return toast;
    }


    @CheckResult
    public static Toast success(@NonNull Context context, @NonNull CharSequence message, int duration, boolean withIcon) {
        Toast toast = Toasty.success(context, message, duration, withIcon);
        transformToast(toast);
        return toast;
    }


    @CheckResult
    public static Toast success(@NonNull Context context, @NonNull CharSequence message, int duration) {
        Toast toast = Toasty.success(context, message, duration);
        transformToast(toast);
        return toast;
    }

    @CheckResult
    public static Toast error(@NonNull Context context, @NonNull CharSequence message, int duration, boolean withIcon) {
        Toast toast = Toasty.error(context, message, duration, withIcon);
        transformToast(toast);
        return toast;

    }

    @CheckResult
    public static Toast error(@NonNull Context context, @NonNull CharSequence message) {
        Toast toast = Toasty.error(context, message);
        transformToast(toast);
        return toast;

    }

    @CheckResult
    public static Toast error(@NonNull Context context, @NonNull CharSequence message, int duration) {
        Toast toast = Toasty.error(context, message, duration);
        transformToast(toast);
        return toast;

    }

    private static void transformToast(Toast toast) {
        LinearLayout linearLayout = (LinearLayout) toast.getView();
        linearLayout.setOrientation(LinearLayout.VERTICAL);
        linearLayout.setPadding(20, 20, 20, 20);
        ImageView icon = (ImageView) linearLayout.getChildAt(0);
        icon.setLayoutParams(new LinearLayout.LayoutParams(50, 50));
        TextView messageTextView = (TextView) linearLayout.getChildAt(1);
        messageTextView.setTextSize(28);
        messageTextView.setPadding(0, 30, 0, 30);
        messageTextView.setGravity(Gravity.CENTER);
        toast.setGravity(Gravity.CENTER, 0, 0);
    }


    private static void transformToast(Toast toast, int xOffset, int yOffset) {
        LinearLayout linearLayout = (LinearLayout) toast.getView();
        linearLayout.setOrientation(LinearLayout.VERTICAL);
        linearLayout.setPadding(20, 20, 20, 20);
        ImageView icon = (ImageView) linearLayout.getChildAt(0);
        icon.setLayoutParams(new LinearLayout.LayoutParams(50, 50));
        TextView messageTextView = (TextView) linearLayout.getChildAt(1);
        messageTextView.setTextSize(28);
        messageTextView.setPadding(0, 30, 0, 30);
        messageTextView.setGravity(Gravity.CENTER);
        toast.setGravity(Gravity.CENTER, xOffset, yOffset);
    }

}
