package com.renrenliao.im.utils;

import android.content.Context;
import android.content.res.Resources;
import android.os.Looper;
import android.view.Gravity;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.renrenliao.im.R;
import com.renrenliao.im.provider.ClarityPotion;


/**
 * author : HJQ
 * github : https://github.com/getActivity/ToastUtils
 * time   : 2018/09/01
 * desc   : Toast工具类
 * modify : ljy
 */
public class RingToast {

    // private static IToastStyle mToastStyle;
    private static Toast mToast;
    //  private static Context mContext;
    private static ImageView mImgType;
    private static TextView message;


    //成功
    public static int NONE = -1;
    public static int OK = 0;
    public static int ERROR = 1;
    public static int WARN = 2;


    public static void init() {

        Context context = ClarityPotion.healingSalve;

        View layout = View.inflate(context, R.layout.layout_ringtoast, null);
        mImgType = layout.findViewById(R.id.mImgType);
        message = layout.findViewById(R.id.message);
        mToast = Toast.makeText(context, "", Toast.LENGTH_SHORT);
        mToast.setGravity(Gravity.CENTER, 0, 0);
        mToast.setView(layout);
    }

    /**
     * 显示一个对象的吐司
     *
     * @param object 对象
     */
    public static void show(Object object, int toastType) {
        show(object != null ? object.toString() : "null", toastType);
    }

    public static void show(Object object) {
        show(object != null ? object.toString() : "null", NONE);
    }


    /**
     * 显示一个吐司
     *
     * @param id 如果传入的是正确的string id就显示对应字符串
     *           如果不是则显示一个整数的string
     */
    public static void show(int id) {

        //吐司工具类还没有被初始化，必须要先调用init方法进行初始化
        if (mToast == null) {
            throw new IllegalStateException("ToastUtils has not been initialized");
        }

        try {
            //如果这是一个资源id
            show(mToast.getView().getContext().getResources().getText(id), NONE);
        } catch (Resources.NotFoundException ignored) {
            //如果这是一个int类型
            show(String.valueOf(id), NONE);
        }
    }

    /**
     * 显示一个吐司
     *
     * @param text 需要显示的文本
     */
    public static void show(CharSequence text, int toastType) {//默认警告

        //吐司工具类还没有被初始化，必须要先调用init方法进行初始化
        if (mToast == null) {
            // throw new IllegalStateException("ToastUtils has not been initialized");
            try {
                //判断是否在主线程中执行
                if (Looper.myLooper() == Looper.getMainLooper()) {
                    init();
                } else {
                    //在子线程中显示处理
                    Looper.prepare();
                    init();
                    Looper.loop();
                }
            } catch (Exception ex) {
                ex.printStackTrace();
            }
        }

        if (text == null || "".contentEquals(text)) {
            return;
        }

        //如果显示的文字超过了10个就显示长吐司，否则显示短吐司
        if (text.length() > 20) {
            mToast.setDuration(Toast.LENGTH_LONG);
        } else {
            mToast.setDuration(Toast.LENGTH_SHORT);
        }

        if (toastType == NONE) {//默认
            mImgType.setImageResource(R.drawable.ic_info);
        } else if (toastType == ERROR) {//失败
            mImgType.setImageResource(R.drawable.ic_erro);
        } else if (toastType == OK) {//成功
            mImgType.setImageResource(R.drawable.ic_sucess);
        } else {//警告
            mImgType.setImageResource(R.drawable.ic_sucess);
        }
        try {
            //判断是否在主线程中执行
            if (Looper.myLooper() == Looper.getMainLooper()) {
                message.setText(text);
                mToast.show();
            } else {
                //在子线程中显示处理
                Looper.prepare();
                message.setText(text);
                mToast.show();
                Looper.loop();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


}
