package org.springblade.third.iotdevice.feign;

import com.alibaba.fastjson.JSONObject;
import lombok.AllArgsConstructor;
import org.springblade.core.tool.api.R;
import org.springblade.device.entity.IotDevice;
import org.springblade.third.iotdevice.dto.AepIotDeviceQueryDto;
import org.springblade.third.iotdevice.service.impl.IotAepDeviceServiceImpl;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @author lidexi
 * @date 2021/5/29 16:35
 */

@RestController
@RequestMapping("/feign/third/aep")
@AllArgsConstructor
public class AepDeviceManageClient implements IAepDeviceManageClient {

	private final IotAepDeviceServiceImpl iotAepDeviceService;

	@Override
	@PostMapping("/registerDevice")
	public R<JSONObject> registerDevice(@RequestBody IotDevice iotDevice) {
//		JSONObject data = new JSONObject();
//		data.put("deviceId", "cf04785948d23095032");
//		return R.data(data);
		return iotAepDeviceService.registerDevice(iotDevice);
	}

	@Override
	@PostMapping("/deleteDevice")
	public R deleteDevice(@RequestBody IotDevice iotDevice) {
		return iotAepDeviceService.deleteDevice(iotDevice);
	}

	@Override
	@PostMapping("/queryDevice")
	public R queryDevice(@RequestBody IotDevice iotDevice) {
		return iotAepDeviceService.queryDevice(iotDevice);
	}

	@Override
	@PostMapping("/queryDeviceList")
	public R queryDeviceList(@RequestBody 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);
		return R.status(true);
	}

	@Override
	@PostMapping("/updateDevice")
	public R updateDevice(@RequestBody IotDevice iotDevice) {
//		return R.status(true);
		return iotAepDeviceService.updateDevice(iotDevice);
	}
}
