package com.tofly.ghrq_route.module.troubleType;

import android.content.Context;

import com.mgq.base.utils.toast.ToastUtil;
import com.tofly.ghrq_route.R;
import com.tofly.ghrq_route.base.BaseApp;
import com.tofly.ghrq_route.base.C;
import com.tofly.ghrq_route.module.troubleType.controller.TroubleTypeCallBack;
import com.tofly.ghrq_route.module.troubleType.controller.TroubleTypeController;
import com.tofly.ghrq_route.module.troubleType.domain.PipeTypeBean;
import com.tofly.ghrq_route.module.troubleType.domain.SysDeviceTypeBean;
import com.tofly.ghrq_route.module.troubleType.domain.TroubleRiskLevelBean;
import com.tofly.ghrq_route.module.troubleType.domain.TroubleTypeBean;
import com.tofly.ghrq_route.module.user.domain.UserBean;

import java.util.List;

/**
 * Created by Amu on 2017/4/26.
 */

public class TroubleTypeTool {

    private static final String TAG = TroubleTypeTool.class.getName();

    private static TroubleTypeTool troubleTypeTool = null;

    private TroubleTypeController troubleTypeController;

    private TroubleTypeDaoHelper dbDaoHelper;

    private TroubleTypeTool(){
        dbDaoHelper = TroubleTypeDaoHelper.getInstance();
    }

    public static synchronized TroubleTypeTool getInstance(){
        if (troubleTypeTool == null){
            troubleTypeTool = new TroubleTypeTool();
        }
        return troubleTypeTool;
    }


    public void getSysDeviceTypeList(final Context context, final TroubleTypeCallBack troubleTypeCallBack) {
        if (C.is_passed_notify)
            return;

        UserBean userBean = BaseApp.getInstance().getLoginUser();
        if (userBean != null) {
            if (troubleTypeController == null)
                troubleTypeController = new TroubleTypeController(context);
            troubleTypeController.getSysDeviceTypeList(userBean.getToken(), new TroubleTypeCallBack(){
                @Override
                public void onGetSysDeviceTypeList(boolean isSuccess, int errorCode, String errMsg, List<SysDeviceTypeBean> equipments) {
                    super.onGetSysDeviceTypeList(isSuccess, errorCode, errMsg, equipments);
                    if (isSuccess){
                    }else {
                        List<SysDeviceTypeBean> deviceTypeBeans = dbDaoHelper.getSysDeviceTypeList();
                        if (deviceTypeBeans != null && deviceTypeBeans.size() > 0){
                            equipments = deviceTypeBeans;
                        }else {
                            errMsg = BaseApp.formatErrorMessage(errorCode, errMsg);
                            String strWarn = context.getString(R.string.dangerreport_get_trotype_err) + errMsg;
                            ToastUtil.showToast(context, strWarn);
                        }
                    }

                    if (troubleTypeCallBack != null)
                        troubleTypeCallBack.onGetSysDeviceTypeList(isSuccess, errorCode, errMsg, equipments);
                }
            });
        }
    }

    public void getTroubleTypeList(final Context context, final TroubleTypeCallBack troubleTypeCallBack){
        UserBean userBean = BaseApp.getInstance().getLoginUser();
        if (userBean != null) {
            if (troubleTypeController == null)
                troubleTypeController = new TroubleTypeController(context);
            int pageindex = 1;
            int pagesize = C.DEFAULT_MAX_LOAD_PAGESIZE;
            troubleTypeController.getTroubleTypeList(context, pageindex, pagesize, new TroubleTypeCallBack() {
                @Override
                public void onGetTroubleTypeList(boolean isSuccess, int errorCode, String errMsg, List<TroubleTypeBean> troubleTypes) {
                    super.onGetTroubleTypeList(isSuccess, errorCode, errMsg, troubleTypes);
                    if (isSuccess){
                    }else {
                        List<TroubleTypeBean> troubleTypeBeans = dbDaoHelper.getSysTroubleTypeList();
                        if (troubleTypeBeans != null && troubleTypeBeans.size() > 0){
                            troubleTypes = troubleTypeBeans;
                        }else {
                            errMsg = BaseApp.formatErrorMessage(errorCode, errMsg);
                            String strWarn = context.getString(R.string.dangerreport_get_trotype_err) + errMsg;
                            ToastUtil.showToast(context, strWarn);
                        }
                    }

                    if (troubleTypeCallBack != null)
                        troubleTypeCallBack.onGetTroubleTypeList(isSuccess, errorCode, errMsg, troubleTypes);
                }
            });
        }
    }

    public void getTroubleLevelList(final Context context, final TroubleTypeCallBack troubleTypeCallBack){
        UserBean userBean = BaseApp.getInstance().getLoginUser();
        if (userBean != null) {
            if (troubleTypeController == null)
                troubleTypeController = new TroubleTypeController(context);
            int pageindex = 1;
            int pagesize = C.DEFAULT_MAX_LOAD_PAGESIZE;
            troubleTypeController.getTroubleLevelList(context, pageindex, pagesize, new TroubleTypeCallBack() {
                @Override
                public void onGetTroubleRiskLevelList(boolean isSuccess, int errorCode, String errMsg, List<TroubleRiskLevelBean> levelBeans) {
                    super.onGetTroubleRiskLevelList(isSuccess, errorCode, errMsg, levelBeans);
                    if (isSuccess){
                    }else {
                        List<TroubleRiskLevelBean> troubleLevelList = dbDaoHelper.getSysTroubleLevelList();
                        if (troubleLevelList != null && troubleLevelList.size() > 0){
                            levelBeans = troubleLevelList;
                        }else {
                            errMsg = BaseApp.formatErrorMessage(errorCode, errMsg);
                            String strWarn = context.getString(R.string.dangerreport_get_trolevel_err) + errMsg;
                            ToastUtil.showToast(context, strWarn);
                        }
                    }

                    if (troubleTypeCallBack != null)
                        troubleTypeCallBack.onGetTroubleRiskLevelList(isSuccess, errorCode, errMsg, levelBeans);
                }
            });
        }
    }

    public void getPipeTypeList(final Context context, final TroubleTypeCallBack troubleTypeCallBack){
        UserBean userBean = BaseApp.getInstance().getLoginUser();
        if (userBean != null) {
            if (troubleTypeController == null)
                troubleTypeController = new TroubleTypeController(context);
            int pageindex = 1;
            int pagesize = C.DEFAULT_MAX_LOAD_PAGESIZE;
            troubleTypeController.getPipeTypeList(context, pageindex, pagesize, new TroubleTypeCallBack() {
                @Override
                public void onGetPipeTypeList(boolean isSuccess, int errorCode, String errMsg, List<PipeTypeBean> pipeTypes) {
                    super.onGetPipeTypeList(isSuccess, errorCode, errMsg, pipeTypes);
                    if (isSuccess){
                    }else {
                        List<PipeTypeBean> pipeTypeList = dbDaoHelper.getPipeTypeList();
                        if (pipeTypeList != null && pipeTypeList.size() > 0){
                            pipeTypes = pipeTypeList;
                        }else {
                            errMsg = BaseApp.formatErrorMessage(errorCode, errMsg);
                            String strWarn = context.getString(R.string.dangerreport_get_pipeType_err) + errMsg;
                            ToastUtil.showToast(context, strWarn);
                        }
                    }

                    if (troubleTypeCallBack != null)
                        troubleTypeCallBack.onGetPipeTypeList(isSuccess, errorCode, errMsg, pipeTypes);
                }
            });
        }
    }
}
