package com.sybotan.android.demo.tools;

import android.app.Activity;
import android.view.Display;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.sybotan.android.demo.DemoApp;
import com.sybotan.android.demo.R;

/**
 * Toast工具类
 * Created by mengxy on 2017/8/17.
 */

public class ToastUtils {
    private static Toast myToast;

    /**
     * 显示自定义的Toast
     * showToast
     *
     * @Title: showToast
     * @Description: TODO
     */
    public static void showMyToast(String msg) {
        if (!isFastDoubleToast()) {
//        MyErrorToast toast = new MyErrorToast(context);
//        toast.showError(msg);
            View toast_view = LayoutInflater.from(DemoApp.Companion.getContext()).inflate(R.layout.layout_toast, null);
            TextView tv_load = (TextView) toast_view.findViewById(R.id.tv_tips);
            tv_load.setText(msg);
            //避免Toast连续显示
            if (myToast == null) {
//            myToast = new Toast(MyApplication.getContext());
                myToast = Toast.makeText(DemoApp.Companion.getContext(), "", Toast.LENGTH_LONG);
            } else {
                //设置toast显示时间
                myToast.setDuration(Toast.LENGTH_LONG);
            }
            //设置toast显示位置
            myToast.setGravity(Gravity.CENTER, 0, 0);
            //设置toast显示的view
            myToast.setView(toast_view);
            myToast.show();
        }
    }

    //显示操作状态Toast
    public static void showToast(String msg, int type) {
        if (!isFastDoubleToast()) {
            View toast_view = LayoutInflater.from(DemoApp.Companion.getContext()).inflate(R.layout.layout_toast, null);
            TextView tv_load = (TextView) toast_view.findViewById(R.id.tv_tips);
            tv_load.setText(msg);
            //避免Toast连续显示
            if (myToast == null) {
//            myToast = new Toast(MyApplication.getContext());
                myToast = Toast.makeText(DemoApp.Companion.getContext(), msg, Toast.LENGTH_LONG);
            } else {
                //设置toast显示时间
                myToast.setDuration(Toast.LENGTH_LONG);
            }
            //设置toast显示位置
            myToast.setGravity(Gravity.CENTER, 0, 0);
            //设置toast显示的view
            myToast.setView(toast_view);
            myToast.show();
        }
    }

    /**
     * 提示
     */
    public static void showNotice(Activity activity, String msg, int type) {
        //if (!isFastDoubleToast()) {
            View toast_view = LayoutInflater.from(DemoApp.Companion.getContext()).inflate(R.layout.layout_notice, null);
            TextView notice_tv = toast_view.findViewById(R.id.notice_tv);
            notice_tv.setText(msg);
            //避免Toast连续显示
            if (myToast == null) {
//            myToast = new Toast(MyApplication.getContext());
                myToast = Toast.makeText(DemoApp.Companion.getContext(), msg, Toast.LENGTH_LONG);
            } else {
                //设置toast显示时间
                myToast.cancel();
                myToast = Toast.makeText(DemoApp.Companion.getContext(), msg, Toast.LENGTH_LONG);
                //myToast.setDuration(Toast.LENGTH_LONG);
            }
            LinearLayout linearLayout = toast_view.findViewById(R.id.notice_linear);
            switch (type) {
                //Success
                case 0:
                    linearLayout.setBackgroundColor(activity.getResources().getColor(R.color.green_D1F4F6));
                    notice_tv.setTextColor(activity.getResources().getColor(R.color.green_36A4AA));
                    break;
                //Faild
                case 1:
                    linearLayout.setBackgroundColor(activity.getResources().getColor(R.color.yellow_FEE8D8));
                    notice_tv.setTextColor(activity.getResources().getColor(R.color.yellow_E88946));
                    break;
                //Error
                case 2:
                    linearLayout.setBackgroundColor(activity.getResources().getColor(R.color.red_FFDCD9));
                    notice_tv.setTextColor(activity.getResources().getColor(R.color.red_D44D44));
                    break;
            }
            WindowManager windowManager = activity.getWindowManager();
            Display display = windowManager.getDefaultDisplay();
            linearLayout.getLayoutParams().width = display.getWidth();
            //设置toast显示位置
//        myToast.setGravity(Gravity.TOP, 0, DensityUtils.dp2px(activity, 44));
            myToast.setGravity(Gravity.TOP, 0, 0);
            //设置toast显示的view
            myToast.setView(toast_view);
            myToast.show();
        //}
    }

    private static long lastClickTime;

    private static boolean isFastDoubleToast() {
        long time = System.currentTimeMillis();
        long timeD = time - lastClickTime;
        if (0 < timeD && timeD < 4000) {
            return true;
        }
        lastClickTime = time;
        return false;
    }

    public static void showNoticeSuccess(Activity activity, String msg) {
        showNotice(activity, msg, 0);
    }

    public static void showNoticeFaild(Activity activity, String msg) {
        showNotice(activity, msg, 1);
    }

    public static void showNoticeError(Activity activity, String msg) {
        showNotice(activity, msg, 2);
    }

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

}
