package org.springblade.openinterface.elevator.controller;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springblade.community.elevator.elevator.entity.Elevator;
import org.springblade.community.elevator.elevator.service.IElevatorService;
import org.springblade.community.elevator.elevatoralarm.entity.ElevatorAlarm;
import org.springblade.community.elevator.elevatoralarm.service.IElevatorAlarmService;
import org.springblade.community.residential.entity.Residential;
import org.springblade.community.residential.service.IResidentialService;
import org.springblade.openinterface.elevator.bean.ElevatorApiResultBean;
import org.springblade.openinterface.elevator.util.ElevatorUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.text.SimpleDateFormat;
import java.util.*;

@RestController
@AllArgsConstructor
@RequestMapping("openinterface/elevator")
@Slf4j
public class OpenElevatorController {

	@Autowired
	private ElevatorUtil elevatorUtil;
	@Autowired
	private IElevatorAlarmService elevatorAlarmService;
	@Autowired
	private IElevatorService elevatorService;
	@Autowired
	private IResidentialService residentialService;

	@GetMapping("/getToken")
	public String getToken() {

		String token = elevatorUtil.getEffectiveToken();

		return token;
	}

	@GetMapping("/test1")
	public String test1() {
		ElevatorApiResultBean liftRegCodes = elevatorUtil.getLiftRegCodes();
		return liftRegCodes.getData().toString();
	}

	@GetMapping("/test2")
	public String test2() {

		List<String> registerCodes = new ArrayList<>();
		registerCodes.add("31106401062013060032");
		registerCodes.add("31106401062013060036");

		ElevatorApiResultBean liftRegCodes = elevatorUtil.getCameraInfo(registerCodes);
		return liftRegCodes.getData().toString();
	}

	@GetMapping("/test3")
	public String test3() {

		Map<String, Object> map = new HashMap<>();
		map.put("PageSize", 100);
		map.put("PageIndex", 1);
		map.put("beginTime", 1611104400);
		map.put("endTime", 1611709200);

		Date now = new Date();
		long time = now.getTime();

		List<String> registerCodes = new ArrayList<>();
		registerCodes.add("31106401062013060032");
		registerCodes.add("31106401062013060036");

		map.put("registerCodes", registerCodes);

		ElevatorApiResultBean liftRegCodes = elevatorUtil.getLiftHisAlarm(map);
		return liftRegCodes.getData().toString();
	}

	/**
	 * 电梯告警接收地址
	 * @param
	 * @return
	 */
	@PostMapping("/receive/elevator/alarm")
	public Map<String, Object> receiveElevatorAlarm(@RequestBody Map<String, Object> map){
		Object alarmTime = map.get("alarmTime");
		Object alarmEndTime = map.get("alarmEndTime");
		SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		simpleDateFormat.setTimeZone(TimeZone.getTimeZone("Etc/GMT-8"));
		if (alarmTime != null){
			Long temTime = (alarmTime.toString().length() == 10) ? Long.parseLong(alarmTime.toString()) * 1000 : Long.parseLong(alarmTime.toString());
			map.replace("alarmTime", simpleDateFormat.format(temTime));
		}
		if (alarmEndTime != null){
			Long temTime = (alarmEndTime.toString().length() == 10) ? Long.parseLong(alarmEndTime.toString()) * 1000 : Long.parseLong(alarmEndTime.toString());
			map.replace("alarmEndTime", simpleDateFormat.format(temTime));
		}

		ElevatorAlarm elevatorAlarm = new ElevatorAlarm();
		BeanUtil.copyProperties(map, elevatorAlarm);

		Map<String, Object> resultMap = new HashMap<>();
		if (BeanUtil.isNotEmpty(elevatorAlarm)){
			boolean result = elevatorAlarmService.save(elevatorAlarm);
			if (result){
				// 回写tenantid、orgposition
				LambdaQueryWrapper<Elevator> elevatorWrapper = new QueryWrapper<>(new Elevator()).lambda().eq(Elevator::getRegisterCode, elevatorAlarm.getRegisterCode());
				Elevator elevator = elevatorService.getOne(elevatorWrapper);
				if (elevator != null) {
					Long residentialId = elevator.getResidentialId();
					if (residentialId != null) {
						Residential residential = residentialService.getById(residentialId);
						elevatorAlarm.setResidentialId(residentialId);
						elevatorAlarm.setResidentialName(residential.getName());
						elevatorAlarm.setTenantId(residential.getTenantId());
						elevatorAlarm.setOrgPosition(residential.getOrgPosition());
						elevatorAlarmService.updateById(elevatorAlarm);
					} else {
						log.warn("【" + elevator.getNbhdName() + "+" + elevator.getRegisterCode() + "】该电梯暂未绑定小区");
					}
				} else {
					log.warn("找不到该注册码【" + elevatorAlarm.getRegisterCode() +"】对应的电梯记录");
				}

				// 发送通知
				boolean directorAndSendMsg = elevatorAlarmService.getDirectorAndSendMsg(elevatorAlarm);

				resultMap.put("code", 0);
				resultMap.put("message", "OK");
				return resultMap;
			}
		}
		resultMap.put("code", -1);
		resultMap.put("message", "ERROR");
		return resultMap;
	}



}
