package com.mytatvarnsdk.myView.dialog;

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

import cgmblelib.Enum.enumError;
import cgmblelib.database.entity.PocGlucose;
import cgmblelib.utils.Tool;


/***
 * 报警弹框的提示
 *
 * @author minyuchun
 */
public class AlarmDialogManagement {

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

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


    /**
     * 显示报警弹框
     * @param activity 上下文
     * @param glucose  血糖数据
     */
    public void showAlarmDialog(Activity activity, PocGlucose glucose) {
        if (activity != null && !activity.isFinishing()) {
            if (!Tool.isPullData(glucose) && glucose != null) {
                if (glucose.getBGICount() == 0 && glucose.getErrorCode() != enumError.NONE) {
                    if (glucose.getErrorCode() == enumError.ERROR_CURRENT_SMALL) {
                        showCurrentError(activity, glucose);
                    }
                } else {
                    switch (glucose.getGlucoseStatus()) {
                        case HYPERGLYCEMIA:
                            showHyperglycemia(activity);
                            break;
                        case HYPOGLYCEMIA:
                            showHypoglycemia(activity);
                            break;
                        default:
                            break;
                    }
                }
            } else {
                WarnDialogUtils.getInstance().dismissWarnDialog();
            }
        } else {
            WarnDialogUtils.getInstance().dismissWarnDialog();
        }
    }

    /**
     * 显示高血糖
     * @param activity 上下文
     */
    private void showHyperglycemia(Activity activity) {
        WarnDialogUtils.getInstance().showWarnDialog(activity, DialogType.TYPE_HYPERGLYCEMIA, new WarnDialog.OnSelectListener() {
            @Override
            public void confirm(DialogInterface dialog) {
            }

            @Override
            public void cancel(DialogInterface dialog) {
                WarnDialogUtils.getInstance().dismissWarnDialog();
            }
        });
    }

    /**
     * 显示低血糖
     * @param activity 上下文
     */
    private void showHypoglycemia(Activity activity) {
        WarnDialogUtils.getInstance().showWarnDialog(activity, DialogType.TYPE_HYPOGLYCEMIA, new WarnDialog.OnSelectListener() {
            @Override
            public void confirm(DialogInterface dialog) {
            }

            @Override
            public void cancel(DialogInterface dialog) {
                WarnDialogUtils.getInstance().dismissWarnDialog();
            }
        });
    }

    /**
     * 显示电流异常
     * @param activity 上下文
     * @param glucose  血糖值
     */
    private void showCurrentError(Activity activity, PocGlucose glucose) {
        if (glucose.getBGICount() > 0) {
            WarnDialogUtils.getInstance().showWarnDialog(activity, DialogType.TYPE_ANOMALY_CURRENT, new WarnDialog.OnSelectListener() {
                @Override
                public void confirm(DialogInterface dialog) {
                }

                @Override
                public void cancel(DialogInterface dialog) {
                    WarnDialogUtils.getInstance().dismissWarnDialog();
                }
            });
        } else {
            WarnDialogUtils.getInstance().showWarnDialog(activity, DialogType.TYPE_ANOMALY_CURRENT_INIT);
        }
    }

}
