package com.cooleshow.base.utils;

import android.content.Context;
import android.graphics.Color;
import android.os.Handler;
import android.text.TextUtils;
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 com.cooleshow.base.R;

public class ToastUtil {

    public static ToastUtil toast;
    private String toastStr;
    Toast toast1 = null;
    private TextView tv;
    private Handler handler;

    public static ToastUtil getInstance() {
        if (null == toast) {
            toast = new ToastUtil();
        }
        return toast;
    }


    /**
     * 创建自定义Toasts ：
     */
    public void show(Context context, String toastStr) {

        if (null == context || TextUtils.isEmpty(toastStr)) {
            return;
        }
        if (!TextUtils.isEmpty(this.toastStr) && toastStr.equals(this.toastStr)) {
            if (handler == null) {
                handler = new Handler();
                handler.postDelayed(() -> {
                    ToastUtil.this.toastStr = null;
                    handler = null;
                }, 2000);
            }
            return;
        }
        this.toastStr = toastStr;
        toast1 = null;
        toast1 = new Toast(context);
        View toastRoot = LayoutInflater.from(context).inflate(R.layout.my_toast, null);
        toast1.setView(toastRoot);
        toast1.setGravity(Gravity.CENTER, 0, 0);
        tv = toastRoot.findViewById(R.id.TextViewInfo);

        if (toastStr.length() > 15) {
            toast1.setDuration(Toast.LENGTH_LONG);
        } else {
            toast1.setDuration(Toast.LENGTH_SHORT);
        }
        if (tv != null) {
            tv.setText(toastStr);
        }
        toast1.show();
    }

    public void cancel(){
        if(toast1 != null){
            toast1.cancel();
        }
    }

    /**
     * 创建自定义Toasts ：
     */
    public void showSpecialScore(Context context, int score) {

//        if(toast1 != null){
//            toast1.cancel();
//        }
//        toast1 = null;
//        toast1 = new Toast(context);
//        View toastRoot = LayoutInflater.from(context).inflate(R.layout.layout_toast_score, null);
//        toast1.setView(toastRoot);
//        toast1.setGravity(Gravity.CENTER, 0, 0);
//        TextView tv_layout_toast_score = toastRoot.findViewById(R.id.tv_layout_toast_score);
//        toast1.setDuration(Toast.LENGTH_SHORT);
//        ImageView iv_layout_toast_score = toastRoot.findViewById(R.id.iv_layout_toast_score);
//
//        if (score >= 80) {
//            iv_layout_toast_score.setImageResource(R.mipmap.img_score_perfect);
//            tv_layout_toast_score.setTextColor(Color.parseColor("#516AFF"));
//        } else if (score >= 60) {
//            iv_layout_toast_score.setImageResource(R.mipmap.img_score_great);
//            tv_layout_toast_score.setTextColor(Color.parseColor("#FF8E5A"));
//        } else if (score >= 40) {
//            iv_layout_toast_score.setImageResource(R.mipmap.img_score_good);
//            tv_layout_toast_score.setTextColor(Color.parseColor("#FF958B"));
//        } else {
//            iv_layout_toast_score.setImageResource(R.mipmap.img_score_bad);
//            tv_layout_toast_score.setTextColor(Color.parseColor("#EE4C6A"));
//        }
//        if (tv_layout_toast_score != null) {
//            tv_layout_toast_score.setText(score + "");
//        }
//        toast1.show();
    }

    /**
     * 创建自定义Toasts ：
     */
    public void showSpecialScore(Context context, int score, int x, int y) {
//
//        if(toast1 != null){
//            toast1.cancel();
//        }
//        toast1 = null;
//        toast1 = new Toast(context);
//        View toastRoot = LayoutInflater.from(context).inflate(R.layout.layout_toast_score, null);
//        toast1.setView(toastRoot);
//        toast1.setGravity(Gravity.TOP | Gravity.LEFT, x, y);
//        TextView tv_layout_toast_score = toastRoot.findViewById(R.id.tv_layout_toast_score);
//        toast1.setDuration(Toast.LENGTH_SHORT);
//        ImageView iv_layout_toast_score = toastRoot.findViewById(R.id.iv_layout_toast_score);
//
//        if (score >= 80) {
//            iv_layout_toast_score.setImageResource(R.mipmap.img_score_perfect);
//            tv_layout_toast_score.setTextColor(Color.parseColor("#516AFF"));
//        } else if (score >= 60) {
//            iv_layout_toast_score.setImageResource(R.mipmap.img_score_great);
//            tv_layout_toast_score.setTextColor(Color.parseColor("#FF8E5A"));
//        } else if (score >= 40) {
//            iv_layout_toast_score.setImageResource(R.mipmap.img_score_good);
//            tv_layout_toast_score.setTextColor(Color.parseColor("#FF958B"));
//        } else {
//            iv_layout_toast_score.setImageResource(R.mipmap.img_score_bad);
//            tv_layout_toast_score.setTextColor(Color.parseColor("#EE4C6A"));
//        }
//        if (tv_layout_toast_score != null) {
//            tv_layout_toast_score.setText(score + "");
//        }
//        toast1.show();
    }
}
