package com.yuchen.saas.park.service.pull.input;

import com.thinkunion.park.service.common.constant.PKCONSTANT;
import com.yuchen.saas.message.api.feign.ws.WebSocketFeignServer;
import com.yuchen.saas.park.api.entity.device.PmgBatSmartEquipment;
import com.yuchen.saas.park.api.enums.IotProductEnum;
import com.yuchen.saas.park.service.pull.service.EventHandler;
import com.yuchen.saas.park.service.pull.untils.GsonUtils;
import com.yuchen.saas.park.service.service.FaceCaptureService;
import com.yuchen.saas.park.service.service.PmgBatSmartEquipmentService;
import com.yuchen.saas.parking.api.vo.IoTEvent;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

@Service
public class DeviceEventInput {

    private final Logger logger = LoggerFactory.getLogger(DeviceEventInput.class);

    @Autowired
    private EventHandler eventHandler;

    @Autowired
    private RfidInput rfidInput;


    @Autowired
    private WebSocketFeignServer pushService;

    //@Autowired
    //private ParkingEventClient parkingEventRouter;

    @Autowired
    private PmgBatSmartEquipmentService smartEquipmentService;

    @Resource
    private FaceCaptureService faceCaptureService;

    public void inputData(IoTEvent ioTEvent) {
        try {
            logger.info("接收到设备事件 : {}", GsonUtils.toJson(ioTEvent));

            String productKey = ioTEvent.getProductKey();
            String deviceName = ioTEvent.getDeviceName();

            // 获取设备信息
            PmgBatSmartEquipment deviceInfo = smartEquipmentService
                    .getDetails(productKey, deviceName);

            if (null != deviceInfo) {

                // 人脸抓拍设备
                if (deviceInfo.getProductKey().equals(IotProductEnum.FACE_CAPTURE_DEVICE.getValue())) {
                    faceCaptureService.inputEventData(deviceInfo, ioTEvent);
                    return;
                }

                if (productKey.equals(PKCONSTANT.FACEPANEL)) {
                    eventHandler.FaceDatahandler(deviceInfo, ioTEvent);
                    return;
                }
                if (productKey.equals(PKCONSTANT.HIKFACERECOGNITION)) {
                    eventHandler.FaceRecognition(deviceInfo, ioTEvent);
                    return;
                }
                /*if (productKey.equals(PKCONSTANT.GATE_PHOTO_PK)) {
                    parkingEventRouter.input(deviceInfo, ioTEvent);
                    return;
                }*/

                if (productKey.equals(PKCONSTANT.FIREXTINGUISHER)) {
                    rfidInput.input(ioTEvent);
                    return;
                }
            }

            // TODO 下部分代码未经测试，有风险
            /*
            PmgBatWarningCategoryVo pmgBatWarningCategoryVo = iWarningCategoryService
                    .getByIdentity(ioTEvent.getIdentifier());

            if (null == pmgBatWarningCategoryVo) {
                logger.info("告警数据为空");
            } else if (null != deviceInfo && null != productKey) {
                if (logger.isDebugEnabled()) {
                    logger.debug("设备数据为空, device : {},  ", deviceInfo.getName());
                }

                // 告警事件处理
                PmgBatHandlerWarning pmgBatHandlerWarning = iHandlerWarningService
                        .getHandlerResult(productKey, deviceName);

                if (pmgBatHandlerWarning != null) {
                    long event_time = ioTEvent.getEventTime();
                    if (pmgBatHandlerWarning.getEndTime() != null && event_time < pmgBatHandlerWarning.getEndTime()) {
                        return;
                    }
                }
                logger.info("结果为：{}", pmgBatHandlerWarning);

                // 封装数据保存历史报警
                PmgBatWarningInfo pmgBatWarningInfo = new PmgBatWarningInfo();
                pmgBatWarningInfo.setProductKey(ioTEvent.getProductKey());
                pmgBatWarningInfo.setDeviceName(ioTEvent.getDeviceName());
                pmgBatWarningInfo.setIdentity(ioTEvent.getIdentifier());
                pmgBatWarningInfo.setRemark(pmgBatWarningCategoryVo.getRemark());
                pmgBatWarningInfo.setLevel(pmgBatWarningCategoryVo.getLevel());
                pmgBatWarningInfo.setIsHandling("0");
                pmgBatWarningInfo.setName(deviceInfo.getName());
                pmgBatWarningInfo.setLevelName(pmgBatWarningCategoryVo.getLevelName());
                pmgBatWarningInfo.setParkId(deviceInfo.getParkId());
                pmgBatWarningInfo.setEventTime(ioTEvent.getEventTime());
                pmgBatWarningInfo.setTenantId(deviceInfo.getTenantId());
                pmgBatWarningInfo.setSetPosition(deviceInfo.getSetPosition());
                pmgBatWarningInfo.setCreateDept(deviceInfo.getCreateDept());

                pmgBatWarningInfo.setBuildingId(deviceInfo.getBuildingId());
                pmgBatWarningInfo.setFloorId(deviceInfo.getFloorId());
                pmgBatWarningInfo.setRoomId(pmgBatWarningInfo.getRoomId());

                iWarningInfoService.save(pmgBatWarningInfo);

                // 封装数据发送到stomp
                DeviceEventVo deviceEventVo = new DeviceEventVo();
                deviceEventVo.setEquipmentName(deviceInfo.getName());
                deviceEventVo.setProduct("");
                deviceEventVo.setOut(ioTEvent.getOut());
                deviceEventVo.setPosition("");
                deviceEventVo.setLevel(pmgBatWarningCategoryVo.getLevel());
                deviceEventVo.setLevelName(pmgBatWarningCategoryVo.getLevelName());
                deviceEventVo.setRemark(pmgBatWarningCategoryVo.getRemark());
                deviceEventVo.setIdentify(ioTEvent.getIdentifier());
                deviceEventVo.setProductKey(ioTEvent.getProductKey());
                deviceEventVo.setDeviceName(ioTEvent.getDeviceName());
                deviceEventVo.setEventTime(ioTEvent.getEventTime());
                deviceEventVo.setParkId(String.valueOf(deviceInfo.getParkId()));
                deviceEventVo.setSetPosition(deviceInfo.getSetPosition());

                deviceEventVo.setBuildingId(deviceInfo.getBuildingId());
                deviceEventVo.setFloorId(deviceInfo.getFloorId());
                deviceEventVo.setRoomId(pmgBatWarningInfo.getRoomId());
                pushService.postEvent(String.valueOf(deviceInfo.getParkId()),
                        JSON.toJSONString(deviceEventVo));

                // 已处理未处理
                stompClient.getWarningHandlerResult(String.valueOf(deviceInfo.getParkId()));
            }

             */
        } catch (Exception e) {
            e.printStackTrace();
            logger.error("处理数据发生错误, {}", e.getMessage());
        }
    }


}
