package org.springblade.third.doordevice.feign;

import cn.hutool.core.lang.Assert;
import lombok.RequiredArgsConstructor;
import org.springblade.common.constant.DeviceConstant;
import org.springblade.community.entity.Residential;
import org.springblade.core.tool.api.R;
import org.springblade.core.tool.utils.StringUtil;
import org.springblade.device.dto.UserInfoDto;
import org.springblade.device.entity.DoorDevice;
import org.springblade.system.expand.feign.IConfClient;
import org.springblade.third.doordevice.dto.UserDeviceDTO;
import org.springblade.third.doordevice.provider.impl.JVTMqProvider;
import org.springblade.third.doordevice.provider.impl.QinLinMqProvider;
import org.springblade.third.doordevice.provider.impl.ZHProvider;
import org.springblade.third.dto.DeviceDTO;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotBlank;
import java.util.List;
import java.util.Map;

/**
 * @author lidexi
 * @date 2021/5/24 14:25
 * 门禁设备对接控制层
 */
@RestController
@RequestMapping("/feign/third/doordevice")
@RequiredArgsConstructor
public class DoorDeviceButtClient implements IDoorDeviceButtClient {

	private final QinLinMqProvider qinLinMqProvider;
	private final JVTMqProvider jvtMqProvider;
	private final ZHProvider zhProvider;

//	@Override
//	@PostMapping("/remoteOpenDoor")
//	public R remoteOpenDoor(@RequestBody DoorDevice doorDevice,  @RequestParam Map<String,Object> userInfoDto) {
//		Assert.notNull(doorDevice.getDeviceFactory(),"the factory of the device is unknown");
//		Assert.notNull(userInfoDto.get("id"),"the params [userId] is required");
//		switch (doorDevice.getDeviceFactory()){
//			case DeviceConstant.DEVICE_FACTORY_QINLIN://亲邻设备远程开门
//				return qinLinMqProvider.remoteOpenDoor(doorDevice, userInfoDto);
//			case DeviceConstant.DEVICE_FACTROY_JVT: //巨龙设备远程开门
//				return jvtMqProvider.remoteOpenDoor(doorDevice, userInfoDto);
//		}
//		return R.fail("设备不存在");
//	}

	@Override
	@PostMapping("/addUser")
	public R addUser(@RequestBody List<DoorDevice> doorDeviceList,@RequestParam Map<String,Object> userInfoDto) {
		Assert.notNull(userInfoDto.get("id"), "the params [userId] is required");
		if(doorDeviceList != null && doorDeviceList.size() > 0){
			for(DoorDevice doorDevice : doorDeviceList){
				switch (doorDevice.getDeviceFactory()){
//					case DeviceConstant.DEVICE_FACTORY_QINLIN://亲邻设备添加人脸
//						qinLinMqProvider.addUser(doorDevice, userInfoDto);
//						break;
//					case DeviceConstant.DEVICE_FACTROY_JVT: //巨龙设备添加人脸
//						jvtMqProvider.addUser(doorDevice, userInfoDto);
//						break;
				}
			}
			return R.success("添加人脸成功");
		}else{
			return R.success("小区下无人脸机，无需下发人脸");
		}
	}

	@Override
	@PostMapping("/delUser")
	public R delUser(@RequestBody List<DoorDevice> doorDeviceList, @RequestParam Map<String,Object> userInfoDto) {
		Assert.notNull(userInfoDto.get("id"),"the params [userId] is required");
		if(doorDeviceList != null && doorDeviceList.size() > 0){
			for(DoorDevice doorDevice : doorDeviceList){
				switch (doorDevice.getDeviceFactory()){
//					case DeviceConstant.DEVICE_FACTORY_QINLIN://亲邻设备删除人脸
//						qinLinMqProvider.deleteUser(doorDevice, userInfoDto);
//						qinLinMqProvider.deleteUserPermission(doorDevice, userInfoDto);
//						break;
//					case DeviceConstant.DEVICE_FACTROY_JVT: //巨龙设备添加人脸
//						jvtMqProvider.deleteUser(doorDevice, userInfoDto);
//						break;
				}
			}
			return R.success("删除人脸成功");
		}else{
			return R.success("小区下无人脸机，无法删除人脸");
		}
	}

	@Override
	@PostMapping("/addResidential")
	public R addResidential(Residential residential) {
		Assert.notNull(residential.getId(),"the params [residentialId] is required");
		return qinLinMqProvider.addResidential(residential.getName());
	}

	@Override
	@PostMapping("/addDevices")
	public R addDevices(@RequestBody List<DoorDevice> doorDeviceList) {
		if(doorDeviceList != null && doorDeviceList.size() > 0){
			for(DoorDevice doorDevice : doorDeviceList){
				switch (doorDevice.getDeviceFactory()){
//					case DeviceConstant.DEVICE_FACTORY_QINLIN://亲邻设备注册
//						qinLinMqProvider.addDevice(doorDevice);
					default:
						return R.fail("找不到该厂家信息!");
				}
			}
			return R.success("注册设备成功");
		}else{
			return R.success("无设备需要注册到第三方");
		}
	}

	@Override
	@PostMapping("/pushFaceToDevices")
	public R pushFaceToDevices(@RequestBody UserDeviceDTO userDeviceDTO) {
		List<DeviceDTO> deviceList = userDeviceDTO.getDeviceList();
		for (DeviceDTO item : deviceList){
			switch (item.getFactory()){
				case "zongheng":
					if(userDeviceDTO.getAddOrUpdate() == 1){
						zhProvider.pushUserToDevices(userDeviceDTO, item.getSerialNum());
					}else if(userDeviceDTO.getAddOrUpdate() == 2){
						zhProvider.pushUpdateUserToDevices(userDeviceDTO, item.getSerialNum());
					}
					break;
			}
		}
		return R.status(true);
	}

	@Override
	@PostMapping("/pushFace")
	public R pushFace(@RequestBody UserDeviceDTO userDeviceDTO) {
		switch (userDeviceDTO.getDeviceFactory()){
			case "zongheng":
				if(userDeviceDTO.getAddOrUpdate() == 1){
					zhProvider.pushUserToDevices(userDeviceDTO, userDeviceDTO.getSerialNum());
				}else if(userDeviceDTO.getAddOrUpdate() == 2){
					zhProvider.pushUpdateUserToDevices(userDeviceDTO, userDeviceDTO.getSerialNum());
				}
				break;
		}
		return R.status(true);
	}

	@Override
	@PostMapping("/deleteFace")
	public R deleteFace(@RequestBody UserDeviceDTO userDeviceDTO) {
		switch (userDeviceDTO.getDeviceFactory()){
			case "zongheng":
				zhProvider.deleteUserToDevices(userDeviceDTO, userDeviceDTO.getSerialNum());
				break;
		}
		return R.status(true);
	}

	@Override
	@PostMapping("/deleteFaceToDevices")
	public R deleteFaceToDevices(@RequestBody UserDeviceDTO userDeviceDTO) {
		List<DeviceDTO> deviceList = userDeviceDTO.getDeviceList();
		for (DeviceDTO item : deviceList){
			switch (item.getFactory()){
				case "zongheng":
					zhProvider.deleteUserToDevices(userDeviceDTO, item.getSerialNum());
					break;
			}
		}
		return R.status(true);
	}

	@Override
	@PostMapping("/remoteOpenDoor")
	public R remoteOpenDoor(@RequestParam String serialNum, @RequestParam String factory, @RequestParam boolean isOpen) {
		switch (factory){
			case "zongheng":
				zhProvider.remoteOpenDoor(serialNum, isOpen);
		}
		return R.status(true);
	}

}

