package org.springblade.third.elevator.feign;

import cn.hutool.core.lang.Assert;
import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springblade.core.tool.api.R;
import org.springblade.device.entity.ElevatorDevice;
import org.springblade.third.elevator.bean.ElevatorApiResultBean;
import org.springblade.third.elevator.bean.ElevatorInfoResultBean;
import org.springblade.third.elevator.util.ElevatorUtil;
import org.springframework.web.bind.annotation.*;

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

@RestController
@RequestMapping("/feign/third/elevator")
@AllArgsConstructor
public class ThirdElevatorDeviceClient implements IThirdElevatorDeviceClient {

	private final ElevatorUtil elevatorUtil;

	/**
	 * 同步云梯信息
	 */
	@Override
	@PostMapping("/sync")
	public List<ElevatorDevice> sync(){
		List<String> registerCodeList = new ArrayList<>();
		ElevatorApiResultBean liftRegCodes = elevatorUtil.getLiftRegCodes();
		Object data = liftRegCodes.getData();
		if (data != null){
			JSONArray jsonArray = JSONUtil.parseArray(data);
			jsonArray.forEach(item -> {
				JSONObject jsonObject = JSONUtil.parseObj(item);
				Object registerCode = jsonObject.get("registerCode");
				if (registerCode != null){
					registerCodeList.add(registerCode.toString());
				}
			});
		}
		Assert.isTrue(registerCodeList.size() > 0, "暂无同步信息！");
		ElevatorInfoResultBean liftInfo = elevatorUtil.getLiftInfo(registerCodeList);
		List<ElevatorDevice> list = liftInfo.getData();
		return list;
	}

	/**
	 * 同步云梯信息
	 */
	@Override
	@GetMapping("/getRealvideo")
	public R getRealvideo(@RequestParam(value = "registerCode") String registerCode){
		Map<String, String> params = new HashMap<>();
		params.put("registerCode", registerCode);
		return R.data(elevatorUtil.getRealvideoAuthorization(params));
	}

	/**
	 * 获取电梯历史告警信息(云梯接口)
	 */
	@Override
	@GetMapping("/getLiftHisAlarm")
	public R getLiftHisAlarm(@RequestParam(value = "registerCode")String registerCodes){
		Map<String, Object> params = new HashMap<>();
		params.put("registerCodes", registerCodes.split(","));

		ElevatorApiResultBean resultBean = elevatorUtil.getLiftHisAlarm(params);

		if (resultBean.getCode() != null && resultBean.getCode().equals(0)){
			return R.data(resultBean.getData());
		}else{
			return R.fail(resultBean.getMessage());
		}
	}

	/**
	 * 获取电梯实时监控信息
	 */
	@Override
	@GetMapping("/getAlarmVideo")
	public R getAlarmVideo(@RequestParam(value = "registerCode")String alarmCode){
		Map<String, String> params = new HashMap<>();
		params.put("alarmCode", alarmCode);
		return R.data(elevatorUtil.getAlarmVideoAuthorization(params));
	}

	/**
	 * 获取电梯当前状态
	 */
	@Override
	@GetMapping("/getLiftStatus")
	@ApiOperation(value = "获取电梯当前状态")
	public R getLiftStatus(@RequestParam(value = "registerCode")String registerCode){
		ElevatorApiResultBean liftStatus = elevatorUtil.getLiftStatus(registerCode);
		return R.data(liftStatus);
	}

	@Override
	@GetMapping("/getLiftPointData")
	@ApiOperation(value = "获取电梯环境监测数据", notes = "传入registerCode")
	public R getLiftPointData(@RequestParam(value = "registerCode")String registerCode) {
		ElevatorApiResultBean liftPointData = elevatorUtil.getLiftPointData(registerCode);
		return R.data(liftPointData);
	}


	@Override
	@PostMapping("/subscribeAlarmData")
	@ApiOperation(value = "电梯告警订阅")
	public R subscribeAlarmData(String webHookUrl){
		ElevatorApiResultBean subscribeResult = elevatorUtil.subscribeAlarmData(webHookUrl);
		if (subscribeResult.getCode().equals(0)){
			return R.status(true);
		}else{
			return R.status(false);
		}
	}

}
