package com.qimai.android.widgetlib.toast;

import android.graphics.PorterDuff;
import android.graphics.PorterDuffColorFilter;
import android.graphics.drawable.ColorDrawable;
import android.graphics.drawable.Drawable;
import android.os.Build;
import android.view.Gravity;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.MainThread;

/**
 * created by wangwei   ON 2019-11-04  email:wangwei_5521@163.com
 *
 * @version 1.1.1
 * @Description
 **/
public class ToastUtils {

    private static volatile ToastUtils instance;
    private static final int COLOR_DEFAULT = 0xFEFFFFFF;

    private static volatile Toast mToast;
    private static volatile Toast mCustomToast;

    private int sMsgTextSize = -1;
    private int sMsgColor = COLOR_DEFAULT;
    private int sGravity = -1;
    private int sBgColor = COLOR_DEFAULT;
    private int sBgResource = -1;

    public static ToastUtils getInstance() {
        if (instance == null) {
            synchronized (ToastUtils.class) {
                if (instance == null) {
                    instance = new ToastUtils();
                }
            }
        }
        return instance;
    }

    @MainThread
    public void showCenter(String msg) {
        getToast().setText(msg);
        mToast.setGravity(Gravity.CENTER, 0, 0);
        mToast.show();
    }

    @MainThread
    public void showToast(CharSequence msg) {
        getToast();
        View toastView = mToast.getView();
        TextView message = toastView.findViewById(android.R.id.message);
        message.setText(msg);
        if (sMsgTextSize != -1) {
            message.setTextSize(sMsgTextSize);
        }
        if (sMsgColor != COLOR_DEFAULT) {
            message.setTextColor(sMsgColor);
        }
        if (sGravity != -1) {
            mToast.setGravity(sGravity, 0, 0);
        }
        if (sBgResource != -1) {
            toastView.setBackgroundResource(sBgResource);

        } else if (sBgColor != COLOR_DEFAULT) {
            Drawable background = toastView.getBackground();
            if (background != null) {
                background.setColorFilter(
                        new PorterDuffColorFilter(sBgColor, PorterDuff.Mode.SRC_IN)
                );
            } else {
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
                    toastView.setBackground(new ColorDrawable(sBgColor));
                } else {
                    toastView.setBackgroundDrawable(new ColorDrawable(sBgColor));
                }
            }
        }
        mToast.show();
    }

    public ToastUtils setsMsgTextSize(int msgTextSize) {
        sMsgTextSize = msgTextSize;
        return this;
    }

    public ToastUtils setMsgColor(int color) {
        this.sMsgColor = color;
        return this;
    }

    public ToastUtils setGravity(int gravity) {
        this.sGravity = gravity;
        return this;
    }

    public ToastUtils setBgColor(int color) {
        this.sBgColor = color;
        return this;
    }

    public ToastUtils setBgResouce(int bgResouce) {
        this.sBgColor = bgResouce;
        return this;
    }

    private static Toast getToast() {
        if (mToast == null) {
            synchronized (ToastUtils.class) {
                if (mToast == null) {
                    mToast = Toast.makeText(HolderContext.getContext(), "", Toast.LENGTH_LONG);
                }
            }
        }
        return mToast;
    }
}
