package org.springblade.third.doordevice.provider.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.json.JSONUtil;
import org.springblade.common.beans.ApiResultBean;
import org.springblade.core.tool.api.R;
import org.springblade.core.tool.jackson.JsonUtil;
import org.springblade.core.tool.utils.StringUtil;
import org.springblade.device.dto.UserInfoDto;
import org.springblade.device.entity.DoorDevice;
import org.springblade.third.doordevice.dto.UserDeviceDTO;
import org.springblade.third.doordevice.entity.QinlinApiLog;
import org.springblade.third.doordevice.entity.qinlin.QLinCommunityDevices;
import org.springblade.third.doordevice.entity.qinlin.QLinUser;
import org.springblade.third.doordevice.entity.qinlin.QLinUserDevice;
import org.springblade.third.doordevice.provider.IDeviceUserService;
import org.springblade.third.doordevice.provider.IDoorDeviceService;
import org.springblade.third.doordevice.provider.IOpenDoorService;
import org.springblade.third.doordevice.provider.IQinLinButtService;
import org.springblade.third.doordevice.service.IQinlinApiLogService;
import org.springblade.third.doordevice.utils.QinLinHttpUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

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

/**
 * @author lidexi
 * @date 2021/5/24 17:23
 */
@Service
public class QinLinMqProvider implements IQinLinButtService<String>, IOpenDoorService<Map<String,Object>>, IDoorDeviceService, IDeviceUserService<Map<String,Object>> {

	@Autowired
	private IQinlinApiLogService qinlinApiLogService;

	private QinLinHttpUtil qinLinHttpUtil;

	public QinLinMqProvider(QinLinHttpUtil qinLinHttpUtil){
		this.qinLinHttpUtil = qinLinHttpUtil;
	}

	@Override
	public R addUser(DoorDevice doorDevice,  Map<String,Object> userInfoDto) {
		QLinUser qLinUser = new QLinUser();
		qLinUser.setUserId(userInfoDto.get("id").toString());
		qLinUser.setDeviceIds(doorDevice.getSerialNum());
		qLinUser.setFaceUrl(userInfoDto.get("faceUrl").toString());
		String endDate = userInfoDto.get("endDate").toString();
		try{
			if(StringUtil.isNotBlank(endDate)){
				/*String strDateFormat = "yyyy-MM-dd HH:mm:ss";
				SimpleDateFormat sdf = new SimpleDateFormat(strDateFormat);*/
				qLinUser.setExpires(endDate);
			}else{
				Calendar cd = Calendar.getInstance();
				cd.setTime(new Date());
				cd.add(Calendar.YEAR, 10);
				Date expireDate = cd.getTime();
				String strDateFormat = "yyyy-MM-dd HH:mm:ss";
				SimpleDateFormat sdf = new SimpleDateFormat(strDateFormat);
				qLinUser.setExpires(sdf.format(expireDate));
			}
			ApiResultBean apiResultBean = qinLinHttpUtil.addUser(qLinUser);
			QinlinApiLog log = BeanUtil.toBean(apiResultBean, QinlinApiLog.class);
			log.setReqData(JsonUtil.toJson(qLinUser));
			log.setMethod("新增用户");
			qinlinApiLogService.save(log);
//			qinlinApiLogService.recordApiLog(log);
			if(apiResultBean.getSuccess()){
				return R.success("新增用户成功");
			}else{
				return R.fail(apiResultBean.getMessage());
			}
		}catch (Exception e){
			QinlinApiLog log = new QinlinApiLog();
			log.setCode(-1);
			log.setReqData(JsonUtil.toJson(qLinUser));
			log.setMessage(e.getMessage());
			log.setMethod("新增用户异常");
			qinlinApiLogService.save(log);
			return R.fail("新增用户异常");
		}

	}

	@Override
	public R deleteUserPermission(DoorDevice doorDevice, Map<String,Object> userInfoDto) {
		QLinUserDevice qLinUser = new QLinUserDevice();
		try{
			qLinUser.setUserId(userInfoDto.get("id").toString());
			qLinUser.setDeviceIds(doorDevice.getSerialNum());
			ApiResultBean apiResultBean = qinLinHttpUtil.deleteUserPermission(qLinUser);
			QinlinApiLog log = BeanUtil.toBean(apiResultBean, QinlinApiLog.class);
			log.setReqData(JsonUtil.toJson(qLinUser));
			log.setData(JsonUtil.toJson(apiResultBean.getData()) + "---" + apiResultBean.getMessage());
			log.setMethod("删除用户权限");
			qinlinApiLogService.save(log);
			if(apiResultBean.getSuccess()){
				return R.success("删除用户权限成功");
			}else{
				return R.fail(apiResultBean.getMessage());
			}
		}catch (Exception e){
			QinlinApiLog log = new QinlinApiLog();
			log.setMethod("删除用户权限异常");
			log.setCode(-1);
			log.setReqData(JsonUtil.toJson(qLinUser));
			log.setMessage(e.getMessage());
			qinlinApiLogService.save(log);
			return R.fail("删除用户权限异常");
		}
	}

	@Override
	public R deleteUser(DoorDevice doorDevice, Map<String,Object> userInfoDto) {
		QLinUser qLinUser = new QLinUser();
		try{
			qLinUser.setUserId(userInfoDto.get("id").toString());
			ApiResultBean apiResultBean = qinLinHttpUtil.deleteUser(qLinUser);
			QinlinApiLog log = BeanUtil.toBean(apiResultBean, QinlinApiLog.class);
			log.setReqData(JsonUtil.toJson(qLinUser));
			log.setData(JsonUtil.toJson(apiResultBean.getData()) + "---" + apiResultBean.getMessage());
			log.setMethod("删除用户");
			qinlinApiLogService.save(log);
			if(apiResultBean.getSuccess()){
				return R.success("删除用户成功");
			}else{
				return R.fail(apiResultBean.getMessage());
			}
		}catch (Exception e){
			QinlinApiLog log = new QinlinApiLog();
			log.setMethod("删除用户异常");
			log.setCode(-1);
			log.setReqData(JsonUtil.toJson(qLinUser));
			qinlinApiLogService.save(log);
			return R.fail("删除用户异常");
		}
	}

	@Override
	public R addDevice(DoorDevice doorDevice) {
		QLinCommunityDevices qLinCommunityDevices = new QLinCommunityDevices();
		try{
			qLinCommunityDevices.setCommunityId(doorDevice.getResidentialId());
			qLinCommunityDevices.setMac(Arrays.asList(doorDevice.getSerialNum()));
			ApiResultBean apiResultBean = qinLinHttpUtil.addDevices(qLinCommunityDevices);
			QinlinApiLog log = BeanUtil.toBean(apiResultBean, QinlinApiLog.class);
			log.setReqData(JsonUtil.toJson(qLinCommunityDevices));
			log.setData(JsonUtil.toJson(apiResultBean.getData()));
			log.setMethod("新增设备");
			qinlinApiLogService.recordApiLog(log);
			if(apiResultBean.getSuccess()){
				return R.success("注册设备成功");
			}else{
				return R.fail(apiResultBean.getMessage());
			}
		}catch (Exception e){
			QinlinApiLog log = new QinlinApiLog();
			log.setMethod("注册设备成功");
			log.setReqData(JsonUtil.toJson(qLinCommunityDevices));
			log.setCode(-1);
			log.setMessage(e.getMessage());
			qinlinApiLogService.recordApiLog(log);
			return R.fail("注册设备异常");
		}
	}

	@Override
	public R deleteDevice(DoorDevice doorDevice) {
		return null;
	}

	@Override
	public R pushUserToDevices(UserDeviceDTO userDeviceDTO, String serialNum) {
		return null;
	}

	@Override
	public R deleteUserToDevices(UserDeviceDTO userDeviceDTO, String serialNum) {
		return null;
	}

	@Override
	public R remoteOpenDoor(String serialNum, boolean isOpen) {
		return null;
	}


	@Override
	public R remoteOpenDoor(DoorDevice doorDevice, Map<String,Object> userInfoDto) {
		QLinUserDevice qLinUserDevice = new QLinUserDevice();
		qLinUserDevice.setUserId(userInfoDto.get("id").toString());
		qLinUserDevice.setDeviceId(doorDevice.getSerialNum());
		try{
			ApiResultBean apiResultBean = qinLinHttpUtil.remoteOpenDoor(qLinUserDevice);
			QinlinApiLog log = BeanUtil.toBean(apiResultBean, QinlinApiLog.class);
			log.setReqData(JsonUtil.toJson(qLinUserDevice));
			log.setData(JsonUtil.toJson(apiResultBean.getData()));
			log.setMethod("远程开门");
			qinlinApiLogService.recordApiLog(log);
			if(apiResultBean.getSuccess()){
				return R.success("开门成功");
			}else{
				return R.fail(apiResultBean.getMessage());
			}
		}catch (Exception e){
			QinlinApiLog log = new QinlinApiLog();
			log.setCode(-1);
			log.setReqData(JsonUtil.toJson(qLinUserDevice));
			log.setMessage(e.getMessage());
			log.setMethod("远程开门");
			qinlinApiLogService.recordApiLog(log);
			return R.fail("开门异常");
		}
	}

	@Override
	public R addResidential(String communityName) {
		try{
			QLinCommunityDevices qLinCommunityDevices = new QLinCommunityDevices();
			qLinCommunityDevices.setCommunityName(communityName);
			ApiResultBean<Long> apiResultBean = qinLinHttpUtil.addCommunity(qLinCommunityDevices);
			QinlinApiLog log = BeanUtil.toBean(apiResultBean, QinlinApiLog.class);
			log.setMethod("新增小区");
			log.setData(apiResultBean.getData().toString());
			qinlinApiLogService.recordApiLog(log);
			if(apiResultBean.getSuccess()){
				return R.data(apiResultBean.getData());
			}else{
				return R.fail(apiResultBean.getMessage());
			}
		}catch (Exception e){
			QinlinApiLog log = new QinlinApiLog();
			log.setCode(-1);
			log.setMethod("新增小区");
			log.setReqData(JsonUtil.toJson(communityName));
			qinlinApiLogService.recordApiLog(log);
			return R.fail("新增小区异常");
		}
	}
}
