package com.yida.data.device.studentCard.controller;

import cn.hutool.core.date.LocalDateTimeUtil;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.yida.data.common.core.common.ResultBean;
import com.yida.data.common.core.entity.card.EduStudentCardAlarm;
import com.yida.data.common.core.entity.card.EduStudentCardFence;
import com.yida.data.common.core.enums.CardAlarmType;
import com.yida.data.common.core.utils.EnumUtils;
import com.yida.data.device.studentCard.constant.JMAlarmType;
import com.yida.data.device.studentCard.mapper.EduStudentCardFenceMapper;
import com.yida.data.device.studentCard.service.EduStudentCardAlarmService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import java.time.LocalDateTime;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@Api(tags = "学生证-告警信息（不鉴权）")
@Slf4j
@Validated
@RestController
@RequiredArgsConstructor
@RequestMapping("/in/studentCard/alarm")
public class InEduStudentCardAlarmController {

    private final EduStudentCardAlarmService eduStudentCardAlarmService;
    private final EduStudentCardFenceMapper eduStudentCardFenceMapper;

    @ApiOperation("接收推送消息")
    @RequestMapping("/pushMsg")
    public ResultBean pushMsg(String msgType,
        String data) throws NoSuchMethodException {
        log.info("receive alarm:[{}],[{}]", msgType, data);
        if ("jimi.push.device.alarm".equals(msgType)) {
            JSONObject json = JSONUtil.parseObj(data);
            String imei = json.getStr("imei");
            String alarmType = json.getStr("alarmType");
            String alarmName = json.getStr("alarmName");
            Double lng = json.getDouble("lng");
            Double lat = json.getDouble("lat");
            LocalDateTime alarmTime = LocalDateTimeUtil.parse(json.getStr("alarmTime"), "yyyy-MM-dd HH:mm:ss");
            EduStudentCardAlarm alarm = new EduStudentCardAlarm();
            alarm.setImei(imei);
            alarm.setLng(lng);
            alarm.setLat(lat);
            alarm.setAlarmTime(alarmTime);
            JMAlarmType jmAlarmType = EnumUtils
                .valueOf(JMAlarmType.class, alarmType, JMAlarmType.class.getMethod("getValue"));
            if (jmAlarmType != null) {
                switch (jmAlarmType) {
                    case FENCE_IN:
                        alarm.setAlarmType(CardAlarmType.IN_FENCE.getValue());
                        break;
                    case FENCE_OUT:
                        alarm.setAlarmType(CardAlarmType.OUT_FENCE.getValue());
                        break;
                }
                if (alarm.getAlarmType().equals(CardAlarmType.IN_FENCE.getValue()) || alarm.getAlarmType()
                    .equals(CardAlarmType.OUT_FENCE.getValue())) {
                    alarmName = alarmName.substring(8, alarmName.length() - 1);
                    EduStudentCardFence fence = eduStudentCardFenceMapper.getFenceByJimiFenceName(alarmName);
                    if (fence != null) {
                        alarm.setRemarkValue(fence.getName());
                        alarm.setRemark(fence.getId().toString());
                    }
                }
                eduStudentCardAlarmService.saveAlarm(alarm);
            }
        }
        return ResultBean.buildSuccess();
    }
}
