package com.common.openapi;

import com.common.openapi.entity.CloudRecordsData;
import com.common.openapi.entity.ControlMovePTZData;
import com.common.openapi.entity.DeviceAlarmStatusData;
import com.common.openapi.entity.DeviceChannelInfoData;
import com.common.openapi.entity.DeviceDetailListData;
import com.common.openapi.entity.DeviceListData;
import com.common.openapi.entity.DeviceModifyNameData;
import com.common.openapi.entity.DeviceUnBindData;
import com.common.openapi.entity.DeviceVersionListData;
import com.common.openapi.entity.LocalRecordsData;
import com.common.openapi.entity.SubAccountDeviceData;
import com.google.gson.JsonObject;
import com.mm.android.deviceaddmodule.LCDeviceEngine;
import com.mm.android.deviceaddmodule.mobilecommon.AppConsume.BusinessException;
import com.mm.android.deviceaddmodule.openapi.HttpSend;

import java.util.HashMap;

public class DeviceInfoOpenApiManager {

    private static int TIME_OUT = 10 * 1000;
    private static int DMS_TIME_OUT = 45 * 1000;

    /**
     * ��蕁笈キ��箙�罘�app羞糸������篋���莅上������堺��拭��
     *
     * @param deviceListData
     * @return
     * @throws BusinessException
     */
    public static DeviceListData.Response deviceBaseList(DeviceListData deviceListData) throws BusinessException {
        HashMap<String, Object> paramsMap = new HashMap<String, Object>();
        paramsMap.put("token", LCDeviceEngine.newInstance().subAccessToken);
        paramsMap.put("bindId", deviceListData.data.baseBindId);
        paramsMap.put("limit", deviceListData.data.limit);
        paramsMap.put("type", deviceListData.data.type);
        paramsMap.put("needApInfo", deviceListData.data.needApInfo);
        JsonObject json = HttpSend.execute(paramsMap, MethodConst.METHOD_DEVICE_BASE_LIST,TIME_OUT);
        DeviceListData.Response response = new DeviceListData.Response();
        response.parseData(json);
        return response;
    }

    /**
     * ��蕁笈キ��綣��上抗�井兄��莅上��������堺��拭��
     *
     * @param deviceListData
     * @return
     * @throws BusinessException
     */
    public static DeviceListData.Response deviceOpenList(DeviceListData deviceListData) throws BusinessException {
        HashMap<String, Object> paramsMap = new HashMap<String, Object>();
        paramsMap.put("token", LCDeviceEngine.newInstance().subAccessToken);
        paramsMap.put("bindId", deviceListData.data.openBindId);
        paramsMap.put("limit", deviceListData.data.limit);
        paramsMap.put("type", deviceListData.data.type);
        paramsMap.put("needApInfo", deviceListData.data.needApInfo);
        JsonObject json = HttpSend.execute(paramsMap, MethodConst.METHOD_DEVICE_OPEN_LIST,TIME_OUT);
        DeviceListData.Response response = new DeviceListData.Response();
        response.parseData(json);
        return response;
    }

    /**
     * �拷��婭���紊�綺���垩�������桁��茵�����篁九垸��茵���桁�莅上���莚��篆≧��
     *
     * @param deviceDetailListData
     * @return
     * @throws BusinessException
     */
    public static DeviceDetailListData.Response deviceOpenDetailList(DeviceDetailListData deviceDetailListData) throws BusinessException {
        HashMap<String, Object> paramsMap = new HashMap<String, Object>();
        paramsMap.put("token", LCDeviceEngine.newInstance().subAccessToken);
        paramsMap.put("deviceList", deviceDetailListData.data.deviceList);
        JsonObject json = HttpSend.execute(paramsMap, MethodConst.METHOD_DEVICE_OPEN_DETAIL_LIST,TIME_OUT);
        DeviceDetailListData.Response response = new DeviceDetailListData.Response();
        response.parseData(json);
        return response;
    }

    /**
     * �拷��婭���紊�綺���垩�������桁��茵�����篁九垸��茵���桁�箙�罘�app羞糸������篋���莅上���莚��篆≧��
     *
     * @param deviceDetailListData
     * @return
     * @throws BusinessException
     */
    public static DeviceDetailListData.Response deviceBaseDetailList(DeviceDetailListData deviceDetailListData) throws BusinessException {
        HashMap<String, Object> paramsMap = new HashMap<String, Object>();
        paramsMap.put("token", LCDeviceEngine.newInstance().subAccessToken);
        paramsMap.put("deviceList", deviceDetailListData.data.deviceList);
        JsonObject json = HttpSend.execute(paramsMap, MethodConst.METHOD_DEVICE_BASE_DETAIL_LIST,TIME_OUT);
        DeviceDetailListData.Response response = new DeviceDetailListData.Response();
        response.parseData(json);
        return response;
    }

    /**
     * 茹g�莅上�
     *
     * @param deviceUnBindData
     * @return
     * @throws BusinessException
     */
    public static boolean unBindDevice(DeviceUnBindData deviceUnBindData) throws BusinessException {
        // 茹g�莅上�
        HashMap<String, Object> paramsMap = new HashMap<String, Object>();
        paramsMap.put("token", LCDeviceEngine.newInstance().accessToken);
        paramsMap.put("deviceId", deviceUnBindData.data.deviceId);
        HttpSend.execute(paramsMap, MethodConst.METHOD_DEVICE_UN_BIND,TIME_OUT);
        return true;
    }

    public static boolean deletePermission(String deviceId,String channelId) throws BusinessException{
        HashMap<String, Object> paramsMap = new HashMap<String, Object>();
        paramsMap.put("openid", LCDeviceEngine.newInstance().openid);
        paramsMap.put("channelId", channelId);
        paramsMap.put("token", LCDeviceEngine.newInstance().accessToken);
        paramsMap.put("deviceId", deviceId);
        HttpSend.execute(paramsMap, MethodConst.DELETE_DEVICE_PERMISSION,TIME_OUT);
        return true;
    }

    public static boolean recoverSDCard(String deviceId) throws BusinessException{
        HashMap<String, Object> paramsMap = new HashMap<String, Object>();
        paramsMap.put("token", LCDeviceEngine.newInstance().accessToken);
        paramsMap.put("deviceId", deviceId);
        HttpSend.execute(paramsMap, MethodConst.RECOVER_SDCARD,TIME_OUT);
        return true;
    }

    /**
     * �桁�莅上�����������膾т拭��
     *
     * @param deviceVersionListData
     * @return
     * @throws BusinessException
     */
    public static DeviceVersionListData.Response deviceVersionList(DeviceVersionListData deviceVersionListData) throws BusinessException {
        HashMap<String, Object> paramsMap = new HashMap<String, Object>();
        paramsMap.put("token", LCDeviceEngine.newInstance().subAccessToken);
        paramsMap.put("deviceIds", deviceVersionListData.data.deviceIds);
        JsonObject json = HttpSend.execute(paramsMap, MethodConst.METHOD_DEVICE_VERSION_LIST,TIME_OUT);
        DeviceVersionListData.Response response = new DeviceVersionListData.Response();
        response.parseData(json);
        return response;
    }

    /**
     * 篆��壕�紊���������腱�
     *
     * @param deviceModifyNameData
     * @return
     * @throws BusinessException
     */
    public static boolean modifyDeviceName(DeviceModifyNameData deviceModifyNameData) throws BusinessException {
        // 茹g�莅上�
        HashMap<String, Object> paramsMap = new HashMap<String, Object>();
        paramsMap.put("token", LCDeviceEngine.newInstance().subAccessToken);
        paramsMap.put("deviceId", deviceModifyNameData.data.deviceId);
        paramsMap.put("channelId", deviceModifyNameData.data.channelId);
        paramsMap.put("name", deviceModifyNameData.data.name);
        HttpSend.execute(paramsMap, MethodConst.METHOD_DEVICE_MODIFY_NAME,TIME_OUT);
        return true;
    }

    /**
     * ��筝��紊�������莚��篆≧��キ��
     *
     * @param deviceChannelInfoData
     * @return
     * @throws BusinessException
     */
    public static DeviceChannelInfoData.Response bindDeviceChannelInfo(DeviceChannelInfoData deviceChannelInfoData) throws BusinessException {
        // ��筝��紊�������莚��篆≧��キ��
        HashMap<String, Object> paramsMap = new HashMap<String, Object>();
        paramsMap.put("token", LCDeviceEngine.newInstance().subAccessToken);
        paramsMap.put("deviceId", deviceChannelInfoData.data.deviceId);
        paramsMap.put("channelId", deviceChannelInfoData.data.channelId);
        JsonObject json = HttpSend.execute(paramsMap, MethodConst.METHOD_DEVICE_CHANNEL_INFO, DMS_TIME_OUT);
        DeviceChannelInfoData.Response response = new DeviceChannelInfoData.Response();
        response.parseData(json);
        return response;
    }

    /**
     * 莅丞舟���綣���
     *
     * @param deviceAlarmStatusData
     * @return
     * @throws BusinessException
     */
    public static boolean modifyDeviceAlarmStatus(DeviceAlarmStatusData deviceAlarmStatusData) throws BusinessException {
        // 茹g�莅上�
        HashMap<String, Object> paramsMap = new HashMap<String, Object>();
        paramsMap.put("token", LCDeviceEngine.newInstance().subAccessToken);
        paramsMap.put("deviceId", deviceAlarmStatusData.data.deviceId);
        paramsMap.put("channelId", deviceAlarmStatusData.data.channelId);
        paramsMap.put("enable", deviceAlarmStatusData.data.enable);
        HttpSend.execute(paramsMap, MethodConst.METHOD_DEVICE_MODIFY_ALARM_STATUS, DMS_TIME_OUT);
        return true;
    }

    /**
     * 莅上���膾�
     *
     * @param deviceId
     * @return
     * @throws BusinessException
     */
    public static boolean upgradeDevice(String deviceId) throws BusinessException {
        // 茹g�莅上�
        HashMap<String, Object> paramsMap = new HashMap<String, Object>();
        paramsMap.put("token", LCDeviceEngine.newInstance().subAccessToken);
        paramsMap.put("deviceId", deviceId);
        try{
            JsonObject execute = HttpSend.execute(paramsMap, MethodConst.METHOD_DEVICE_UPDATE, DMS_TIME_OUT);
            if(execute!=null){
                return true;
            }else{
                return false;
            }
        }catch (Exception e){
            return false;
        }


    }

    /**
     * ��綺���莅上�篋�綵����閬�
     *
     * @param cloudRecordsData
     * @return
     * @throws BusinessException
     */
    public static CloudRecordsData.Response getCloudRecords(CloudRecordsData cloudRecordsData) throws BusinessException {
        // ��綺���莅上�篋�綵����閬�
        HashMap<String, Object> paramsMap = new HashMap<String, Object>();
        paramsMap.put("token", LCDeviceEngine.newInstance().subAccessToken);
        paramsMap.put("deviceId", cloudRecordsData.data.deviceId);
        paramsMap.put("channelId", cloudRecordsData.data.channelId);
        paramsMap.put("beginTime", cloudRecordsData.data.beginTime);
        paramsMap.put("endTime", cloudRecordsData.data.endTime);
//        paramsMap.put("nextRecordId", cloudRecordsData.data.nextRecordId);
        paramsMap.put("count", cloudRecordsData.data.count);
        JsonObject json = HttpSend.execute(paramsMap, MethodConst.METHOD_GET_CLOUND_RECORDS,TIME_OUT);
        CloudRecordsData.Response response = new CloudRecordsData.Response();
        response.parseData(json);
        return response;
    }

    /**
     * �ヨ�莅上�莅上�綵����閬�
     *
     * @param localRecordsData
     * @return
     * @throws BusinessException
     */
    public static LocalRecordsData.Response queryLocalRecords(LocalRecordsData localRecordsData) throws BusinessException {
        // �ヨ�莅上�莅上�綵����閬�
        HashMap<String, Object> paramsMap = new HashMap<String, Object>();
        paramsMap.put("token", LCDeviceEngine.newInstance().subAccessToken);
        paramsMap.put("deviceId", localRecordsData.data.deviceId);
        paramsMap.put("channelId", localRecordsData.data.channelId);
        paramsMap.put("beginTime", localRecordsData.data.beginTime);
        paramsMap.put("endTime", localRecordsData.data.endTime);
        paramsMap.put("type", localRecordsData.data.type);
        paramsMap.put("queryRange", localRecordsData.data.queryRange);
        JsonObject json = HttpSend.execute(paramsMap, MethodConst.METHOD_QUERY_LOCAL_RECORD, DMS_TIME_OUT);
        LocalRecordsData.Response response = new LocalRecordsData.Response();
        response.parseData(json);
        return response;
    }

    public static int queryCloudUse(String deviceId,String channelId) throws BusinessException{
        HashMap<String, Object> paramsMap = new HashMap<String, Object>();
        paramsMap.put("token", LCDeviceEngine.newInstance().subAccessToken);
        paramsMap.put("deviceId", deviceId);
        paramsMap.put("channelId", channelId);
        JsonObject json = HttpSend.execute(paramsMap, MethodConst.GET_DEVICE_CLOUD, DMS_TIME_OUT);
        Integer state = json.get("strategyStatus").getAsInt();
        return state;
    }

    public static String  querySDState(String deviceId) throws BusinessException{
        HashMap<String, Object> paramsMap = new HashMap<String, Object>();
        paramsMap.put("token", LCDeviceEngine.newInstance().subAccessToken);
        paramsMap.put("deviceId", deviceId);
        JsonObject json = HttpSend.execute(paramsMap, MethodConst.SD_STATUE_QUERY, DMS_TIME_OUT);
        String state = json.get("status").getAsString();
        return state;
    }

    /**
     * 篋��亥Щ��ァ�倶・�o�V2�����
     *
     * @param controlMovePTZData
     * @return
     * @throws BusinessException
     */
    public static void controlMovePTZ(ControlMovePTZData controlMovePTZData) throws BusinessException {
        // 篋��亥Щ��ァ�倶・�o�V2�����
        HashMap<String, Object> paramsMap = new HashMap<String, Object>();
        paramsMap.put("token", LCDeviceEngine.newInstance().subAccessToken);
        paramsMap.put("deviceId", controlMovePTZData.data.deviceId);
        paramsMap.put("channelId", controlMovePTZData.data.channelId);
        paramsMap.put("operation", controlMovePTZData.data.operation);
        paramsMap.put("duration", controlMovePTZData.data.duration);
        HttpSend.execute(paramsMap, MethodConst.METHOD_CONTROL_MOVE_PTZ, DMS_TIME_OUT);
    }

    /**
     * ���よ�紊�篋�綵����閬�
     *
     * @param recordRegionId
     * @return
     * @throws BusinessException
     */
    public static boolean deleteCloudRecords(String recordRegionId) throws BusinessException {
        // ���よ�紊�篋�綵����閬�
        HashMap<String, Object> paramsMap = new HashMap<String, Object>();
        paramsMap.put("token", LCDeviceEngine.newInstance().subAccessToken);
        paramsMap.put("recordRegionId", recordRegionId);
        HttpSend.execute(paramsMap, MethodConst.METHOD_DELETE_CLOUND_RECORDS,TIME_OUT);
        return true;
    }

    //V7.12�医�
    public static SubAccountDeviceData.Response getSubAccountDeviceListLight(int pageNo, int pageSize, String openid)throws BusinessException{
        HashMap<String, Object> paramsMap = new HashMap<String, Object>();
        paramsMap.put("token", LCDeviceEngine.newInstance().accessToken);
        paramsMap.put("pageNo", pageNo);
        paramsMap.put("pageSize",pageSize);
        paramsMap.put("openid",openid);
        JsonObject json = HttpSend.execute(paramsMap, MethodConst.LIST_SUB_ACCOUNT_DEVICE,TIME_OUT);
        SubAccountDeviceData.Response response = new SubAccountDeviceData.Response();
        response.parseData(json);
        return  response;
    }

    public static DeviceDetailListData.Response getSubAccountDeviceList(int pageNo, int pageSize)throws BusinessException{
        HashMap<String, Object> paramsMap = new HashMap<String, Object>();
        paramsMap.put("token", LCDeviceEngine.newInstance().subAccessToken);
        paramsMap.put("page", pageNo);
        paramsMap.put("pageSize",pageSize);
        JsonObject json = HttpSend.execute(paramsMap, MethodConst.SUB_ACCOUNT_DEVICE_LIST,TIME_OUT);
        DeviceDetailListData.Response response = new DeviceDetailListData.Response();
        response.parseData(json);
        return  response;
    }

}