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.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.MainThread;

import com.qimai.android.widgetlib.R;

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

    private static volatile CustomToastUtils 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 CustomToastUtils getInstance() {
        if (instance == null) {
            synchronized (CustomToastUtils.class) {
                if (instance == null) {
                    instance = new CustomToastUtils();
                }
            }
        }
        return instance;
    }

    public void success() {
        View view = getToast().getView();
        ImageView icon = view.findViewById(R.id.toastIcon);
        TextView title = view.findViewById(R.id.toastMsg);
        title.setText("支付成功");
        icon.setImageResource(R.drawable.toast_success);
        mToast.setGravity(Gravity.CENTER, 0, 0);
        mToast.show();
    }

    public void fail() {
        View view = getToast().getView();
        ImageView icon = view.findViewById(R.id.toastIcon);
        TextView title = view.findViewById(R.id.toastMsg);
        title.setText("支付失败");
        icon.setImageResource(R.drawable.toast_fail);
        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 CustomToastUtils setsMsgTextSize(int msgTextSize) {
        sMsgTextSize = msgTextSize;
        return this;
    }

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

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

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

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

    private static Toast getToast() {
        if (mToast == null) {
            synchronized (CustomToastUtils.class) {
                if (mToast == null) {
                    mToast = Toast.makeText(HolderContext.getContext(), "", Toast.LENGTH_LONG);
                    mToast.setView(LayoutInflater.from(HolderContext.getContext()).inflate(R.layout.custometoast, null));
                }
            }
        }
        return mToast;
    }
}
