package com.tofly.ghrq_route.module.incident;

import android.content.Context;

import com.mgq.base.utils.toast.ToastUtil;
import com.tofly.ghrq_route.base.BaseApp;
import com.tofly.ghrq_route.module.incident.controller.MonitorContact;
import com.tofly.ghrq_route.module.incident.controller.MonitorController;
import com.tofly.ghrq_route.module.incident.domain.CarUploadBean;
import com.tofly.ghrq_route.module.incident.domain.DeviceUploadBean;
import com.tofly.ghrq_route.module.service.GetDeviceUploadService;

import java.util.List;

public class MonitorTool {
    private static final String TAG = MonitorTool.class.getName();

    private static MonitorTool monitorTool = null;

    private MonitorController monitorController;

    private MonitorTool() {

    }

    public static synchronized MonitorTool getInstance() {
        if (monitorTool == null) {
            monitorTool = new MonitorTool();
        }
        return monitorTool;
    }

    /**
     * 从服务器获取设备状态信息
     * */
    public void getDeviceUploadListOnline(Context context, String departId, GetDeviceUploadService.GetDeviceUpload onGetDeviceUpload){
        getDeviceUploadList(context, departId, onGetDeviceUpload);
    }

    /**
     * 从服务器/本地获取设备状态信息
     * */
    private void getDeviceUploadList(final Context context, String departId, GetDeviceUploadService.GetDeviceUpload onGetDeviceUpload) {
        if (monitorController == null)
            monitorController = new MonitorController();
        monitorController.getDeviceUploadList(context, departId, new MonitorContact.GetDeviceUploadListCallback() {
            @Override
            public void getDataList(List<DeviceUploadBean> dataList, int total) {
                if (onGetDeviceUpload != null) {
                    onGetDeviceUpload.onGetDevices(dataList);
                }
            }

            @Override
            public void onError(int errCode, String errMsg) {
                errMsg = BaseApp.formatErrorMessage(errCode, errMsg);
                ToastUtil.showToast(context, errMsg);
            }
        });
    }

    /**
     * 从服务器获取车辆状态信息
     * */
    public void getCarUploadListOnline(Context context, GetDeviceUploadService.GetCarUpload onGetCarUpload){
        getCarUploadList(context, onGetCarUpload);
    }

    /**
     * 从服务器/本地获取车辆状态信息
     * */
    private void getCarUploadList(final Context context, GetDeviceUploadService.GetCarUpload onGetCarUpload) {
        if (monitorController == null)
            monitorController = new MonitorController();
        monitorController.getCarUploadList(context, new MonitorContact.GetCarUploadListCallback() {
            @Override
            public void getDataList(List<CarUploadBean> dataList, int total) {
                if (onGetCarUpload != null) {
                    onGetCarUpload.onGetCars(dataList);
                }
            }

            @Override
            public void onError(int errCode, String errMsg) {
                errMsg = BaseApp.formatErrorMessage(errCode, errMsg);
                ToastUtil.showToast(context, errMsg);
            }
        });
    }
}
