package com.mytatvarnsdk.myView.dialog;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.DialogInterface;

import com.mytatvarnsdk.R;

/**
 * 警告弹框控制类
 *
 * @author minyuchun
 */
public class WarnDialogUtils {
    /**
     * 警告窗口
     */
    private WarnDialog warnDialog;

    private static class SingletonInstance {
        private static final WarnDialogUtils INSTANCE = new WarnDialogUtils();
    }

    public static WarnDialogUtils getInstance() {
        return SingletonInstance.INSTANCE;
    }

    /**
     * 默认的异常类型
     */
    private int TYPE_NOW = DialogType.TYPE_DEFAULT;

    /**
     * 显示警告弹框
     *
     * @param activity       依赖的页面
     * @param warnDialogType 警告类型
     */
    public void showWarnDialog(Activity activity, @DialogType int warnDialogType) {
        showWarnDialog(activity, warnDialogType, null, null, null, null);
    }

    /**
     * 显示警告弹框
     *
     * @param activity       依赖的页面
     * @param warnDialogType 警告类型
     */
    public void showWarnDialog(Activity activity, @DialogType int warnDialogType, DialogInterface.OnClickListener listener) {
        showWarnDialog(activity, warnDialogType, null, listener, null, null);
    }

    /**
     * 显示警告弹框
     *
     * @param activity       依赖的页面
     * @param warnDialogType 警告类型
     */
    public void showWarnDialog(Activity activity, @DialogType int warnDialogType, WarnDialog.OnSelectListener listener) {
        showWarnDialog(activity, warnDialogType, null, null, listener, null);
    }

    /**
     * 显示告警提示框
     *
     * @param activity       依赖的窗口
     * @param warnDialogType 警告框类型
     * @param listener       回调监听
     */
    @SuppressLint("StringFormatInvalid")
    public synchronized void showWarnDialog(Activity activity, int warnDialogType, CharSequence content, DialogInterface.OnClickListener listener, WarnDialog.OnSelectListener onSelectListener, WarnDialog.OnChooseListener onChooseListener) {
        if (activity == null || activity.isFinishing()) {
            return;
        }
        if (isWarnDialogShowing() && TYPE_NOW == warnDialogType) {
            return;
        }
        dismissWarnDialog();//先隐藏以前的弹框
        TYPE_NOW = warnDialogType;
        warnDialog = new WarnDialog(activity);
        switch (warnDialogType) {
            case DialogType.TYPE_LOW_POWER:
                warnDialog.setContent(activity.getResources().getString(R.string.dialog_sensor_low_power))
                        .setTitle(null);
                break;
            case DialogType.TYPE_MONITORING_END:
                warnDialog.setContent(activity.getResources().getString(R.string.dialog_cycle_end))
                        .setTitle(null);
                break;
            case DialogType.TYPE_ANOMALY_CURRENT:
            case DialogType.TYPE_ANOMALY_CURRENT_INIT:
                warnDialog.setContent(activity.getResources().getString(R.string.dialog_warn_current_error))
                        .setTitle(null);
                break;
            case DialogType.TYPE_HYPERGLYCEMIA:
            case DialogType.TYPE_HYPERGLYCEMIA_NONE:
                warnDialog.setContent(activity.getResources().getString(R.string.dialog_warn_sugar_warn_high))
                        .setTitle(null);
                break;
            case DialogType.TYPE_HYPOGLYCEMIA:
            case DialogType.TYPE_HYPOGLYCEMIA_NONE:
                warnDialog.setContent(activity.getResources().getString(R.string.dialog_warn_sugar_warn_low))
                        .setTitle(null);
                break;
            case DialogType.TYPE_URGENT_LOW_NONE:
                warnDialog.setContent(activity.getResources().getString(R.string.dialog_warn_sugar_warn_too_low))
                        .setTitle(null);
                break;
            case DialogType.POP_BT_OPEN:
                warnDialog.setContent(content)
                        .setImageShow(false)
                        .setTitle(null);
                break;
            case DialogType.TYPE_UNBIND:
                warnDialog.setContent(activity.getResources().getString(R.string.dialog_sensor_unexpected_power_off))
                        .setTitle(null);
                break;
            case DialogType.TYPE_GPS_SERVICE:
                warnDialog.setContent(activity.getResources().getString(R.string.fail_gps))
                        .setTitle(null);
                break;

            case DialogType.TYPE_CAMERA_PERMISSION_FAIL:
                warnDialog.setContent(activity.getResources().getString(R.string.fail_camara_permission))
                        .setTitle(null);
                break;
            case DialogType.TYPE_ANOMALY_CURRENT_CONTINUE:
                warnDialog.setContent(activity.getResources().getString(R.string.error_current_continue))
                        .setTitle(null);
                break;
            case DialogType.TYPE_SELECT_CONNECT:
                warnDialog.setContent(content)
                        .setTitle(activity.getResources().getString(R.string.data_device));
                break;
            case DialogType.TYPE_TRANSMITTER_UNBIND:
                warnDialog.setContent(content)
                        .setImageShow(false)
                        .setTitle(null);
                break;
            case DialogType.TYPE_QR_RESULT:
                warnDialog.setContent(content)
                        .setSuccessImg()
                        .setTitle(null);
                break;
            case DialogType.TYPE_BLUETOOTH_OFF:
            case DialogType.TYPE_PERMISSION_REASON:
            case DialogType.TYPE_SCAN_CODE:
                warnDialog.setContent(content)
                        .setTitle(null);
                break;
            case DialogType.TYPE_QR_TIP:
                warnDialog.setContent(activity.getResources().getString(R.string.qr_request))
                        .setTitle(null);
                break;
            case DialogType.TYPE_STD_DETAIL:
                warnDialog.setTitle(content.toString())
                        .setContent(activity.getString(R.string.sd)).
                        setBottomText(activity.getString(R.string.i_see), null).setImageShow(false).setTitleBold(true);
                break;
            case DialogType.TYPE_FOLLOW_INVITE:
                warnDialog.setContent(activity.getString(R.string.follow_add)).
                        setBottomText(activity.getString(R.string.yes), null).setImageShow(false);
                break;
            default:
                break;
        }
        warnDialog.setConfirmButtonClickListener(listener)
                .setSelectListener(onSelectListener)
                .setChooseListener(onChooseListener)
                .show();
    }

    /**
     * 将警告弹框关闭
     */
    public void dismissWarnDialog() {
        try {
            if (isWarnDialogShowing()) {
                warnDialog.dismiss();
                warnDialog = null;
            } else if (warnDialog != null) {
                warnDialog = null;
            }
            TYPE_NOW = DialogType.TYPE_DEFAULT;
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 是否警告弹框正在显示
     *
     * @return 是否正在显示
     */
    private boolean isWarnDialogShowing() {
        return warnDialog != null && warnDialog.isShowing();
    }

    public int getTYPE_NOW() {
        return TYPE_NOW;
    }
}
