package org.springblade.third.butt.iotdevice.service.impl;

import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ctg.ag.sdk.biz.AepDeviceManagementClient;
import com.ctg.ag.sdk.biz.aep_device_management.*;
import org.springblade.core.tool.api.R;
import org.springblade.smartapplication.smartsmoke.entity.SmartDevice;
import org.springblade.third.butt.iotdevice.constant.AepConstant;
import org.springblade.third.butt.iotdevice.dto.AepIotDeviceQueryDto;
import org.springblade.third.butt.iotdevice.entity.AepIotDeviceEntity;
import org.springblade.third.butt.iotdevice.service.IAepDeviceManageService;
import org.springblade.third.iotaepproduct.entity.ThirdIotAepProduct;
import org.springblade.third.iotaepproduct.service.IThirdIotAepProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created By lidexi in 2021/7/14
 **/
@Service
public class AepDeviceManageService implements IAepDeviceManageService {
	@Autowired
	private IThirdIotAepProductService thirdIotAepProductService;

	@Override
	public R registerDevice(SmartDevice iotDevice) {
		AepDeviceManagementClient client = AepDeviceManagementClient.newClient()
			.appKey(AepConstant.APPKEY).appSecret(AepConstant.APPSECRET)
			.build();

		CreateDeviceRequest request = new CreateDeviceRequest();
		ThirdIotAepProduct iotAepProduct = thirdIotAepProductService.getBaseMapper().selectOne(new QueryWrapper<>(new ThirdIotAepProduct())
			.lambda().eq(ThirdIotAepProduct::getProductId, iotDevice.getProductId()));
		request.setParamMasterKey(iotAepProduct.getApiKey());

		Map other = new HashMap();
		other.put("autoObserver", 0);
		Map body = new HashMap<>();
		body.put("deviceName", iotDevice.getDeviceName());
		body.put("imei", iotDevice.getImei());
		body.put("productId", iotDevice.getProductId());
		body.put("other", other);
		body.put("operator", "ddd");
		request.setBody(JSONUtil.toJsonStr(body).getBytes());
		JSONObject jsonObject = null;
		Map aepResultBean = null;
		try {
			CreateDeviceResponse createDeviceResponse = client.CreateDevice(request);

			jsonObject = JSONUtil.parseObj(createDeviceResponse.toString().split("body=")[1]);
			System.out.println(createDeviceResponse.toString());
			aepResultBean = JSONUtil.toBean(jsonObject, Map.class);
		} catch (Exception e) {
			e.printStackTrace();
		}

// more requests

		client.shutdown();
		return R.data(aepResultBean);
	}

	@Override
	public R deleteDevice(SmartDevice iotDevice) {
		AepDeviceManagementClient client = AepDeviceManagementClient.newClient()
			.appKey(AepConstant.APPKEY).appSecret(AepConstant.APPSECRET)
			.build();

		DeleteDeviceRequest request = new DeleteDeviceRequest();
		request.setParamMasterKey(iotDevice.getMasterKey());
		request.setParamProductId(iotDevice.getProductId());    // single value
		request.setParamDeviceIds(iotDevice.getDeviceId());    // single value
		try {
			System.out.println(client.DeleteDevice(request));
		} catch (Exception e) {
			e.printStackTrace();
		}

		client.shutdown();
		return null;
	}

	@Override
	public R queryDevice(AepIotDeviceQueryDto aepIotDeviceQueryDto) {
		return null;
	}

	@Override
	public R queryDeviceList(AepIotDeviceQueryDto aepIotDeviceQueryDto) {
		AepDeviceManagementClient client = AepDeviceManagementClient.newClient()
			.appKey(AepConstant.APPKEY).appSecret(AepConstant.APPSECRET)
			.build();

		QueryDeviceListRequest request = new QueryDeviceListRequest();
		request.setParamMasterKey(aepIotDeviceQueryDto.getMasterKey());
		request.setParamProductId(aepIotDeviceQueryDto.getProductId());
		request.setParamPageNow(aepIotDeviceQueryDto.getPageNow());
		request.setParamPageSize(aepIotDeviceQueryDto.getPageSize());

		List<AepIotDeviceEntity> aepIotDeviceEntities = null;
		try {
			QueryDeviceListResponse queryDeviceListResponse = client.QueryDeviceList(request);
			JSONObject body = JSONUtil.parseObj(new String(queryDeviceListResponse.getBody()));
			JSONObject result = JSONUtil.parseObj(body.get("result"));
			JSONArray list = JSONUtil.parseArray(result.get("list"));
			aepIotDeviceEntities = com.alibaba.fastjson.JSONObject.parseArray(list.toString(), AepIotDeviceEntity.class);

			System.out.println();
		} catch (Exception e) {
			e.printStackTrace();
		}

// more requests

		client.shutdown();
		return R.data(aepIotDeviceEntities);
	}

	@Override
	public R updateDevice() {
		return null;
	}
}
