package com.ho.filecenter.controller;

import cn.hutool.core.date.DateUnit;
import cn.hutool.core.date.DateUtil;
import com.ho.business.vo.req.DeviceReqVO;
import com.ho.business.vo.resp.DeviceRespVO;
import com.ho.common.tools.constant.CommonConstant;
import com.ho.common.tools.constant.ContextConstant;
import com.ho.common.tools.constant.RedisKeyConstant;
import com.ho.common.tools.exception.DataResult;
import com.ho.common.tools.service.RedisService;
import com.ho.filecenter.feignclient.BusinessFeignClient;
import com.ho.filecenter.vo.mqtt.HeartbeatReq;
import com.ho.filecenter.vo.resp.HeartbeatResp;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @Description 心跳检测
 * Author xueweizhi
 * Date 2023/3/24 16:09
 */
@RestController
@RequestMapping(ContextConstant.MEDIA_CONTEXT + "heartbeatCheck")
@Api(tags = "心跳检测")
@Slf4j
public class HeartbeatCheckController {

    @Autowired
    BusinessFeignClient businessFeignClient;

    @Autowired
    RedisService redisService;

    public static final Long TIME_LIMIT = 100L;

    @PostMapping("getHeartbeat")
    @ApiOperation(value = "心跳检测")
    public DataResult<HeartbeatResp> getHeartbeat(@RequestBody HeartbeatReq vo, HttpServletRequest request) {
        String lang = request.getHeader(CommonConstant.LANG);
        HeartbeatResp heartbeatResp = new HeartbeatResp();
        //检测心跳
        DeviceReqVO deviceReqVO = new DeviceReqVO();
        deviceReqVO.setStationId(vo.getStationId());
        deviceReqVO.setSrcId(vo.getSrcId());
        List<DeviceRespVO> deviceList = businessFeignClient.getDeviceSN(deviceReqVO);
        if (deviceList.size() == 0 || null == deviceList.get(0).getSerialNo()) {
            log.info("getHeartbeat 当前设备没有配置SN");
            heartbeatResp.setHeartbeatStatus(CommonConstant.ZERO);
            heartbeatResp.setMsg(CommonConstant.DEVICE_NO_SN);
            if(CommonConstant.langTemp.EN_US.equals(lang)){
                heartbeatResp.setMsg(CommonConstant.DEVICE_NO_SN_EN);
            }else if(CommonConstant.langTemp.FR_FR.equals(lang)){
                heartbeatResp.setMsg(CommonConstant.DEVICE_NO_SN_FRA);
            }else if(CommonConstant.langTemp.ES_ES.equals(lang)){
                heartbeatResp.setMsg(CommonConstant.DEVICE_NO_SN_SPA);
            }else if(CommonConstant.langTemp.DE_DE.equals(lang)){
                heartbeatResp.setMsg(CommonConstant.DEVICE_NO_SN_DE);
            }else if(CommonConstant.langTemp.PL_PL.equals(lang)){
                heartbeatResp.setMsg(CommonConstant.DEVICE_NO_SN_PL);
            }else if(CommonConstant.langTemp.AR_EG.equals(lang)){
                heartbeatResp.setMsg(CommonConstant.DEVICE_NO_SN_ARA);
            }
            return DataResult.success(heartbeatResp);
        }
        String sn = deviceList.get(0).getSerialNo();
        String heartbeatKey = RedisKeyConstant.HEARTBEAT + sn;
        String msg = CommonConstant.Heartbeat.MSG + sn;
        if(CommonConstant.langTemp.EN_US.equals(lang)){
            msg = CommonConstant.Heartbeat.MSG_EN + sn;
        }else if(CommonConstant.langTemp.FR_FR.equals(lang)){
            msg = CommonConstant.Heartbeat.MSG_FRA + sn;
        }else if(CommonConstant.langTemp.ES_ES.equals(lang)){
            msg = CommonConstant.Heartbeat.MSG_SPA + sn;
        }else if(CommonConstant.langTemp.DE_DE.equals(lang)){
            msg = CommonConstant.Heartbeat.MSG_DE + sn;
        }else if(CommonConstant.langTemp.PL_PL.equals(lang)){
            msg = CommonConstant.Heartbeat.MSG_PL + sn;
        }else if(CommonConstant.langTemp.AR_EG.equals(lang)){
            msg = CommonConstant.Heartbeat.MSG_ARA + sn;
        }

        if (redisService.hasKey(heartbeatKey)) {
            Map<Object, Object> hgetall = redisService.hgetall(heartbeatKey);
            Date updateTime = (Date) hgetall.get(CommonConstant.UPDATE_TIME);
            Date date = new Date();
            long between = DateUtil.between(updateTime, date, DateUnit.MINUTE);
            if (between >= 5) {
                heartbeatResp.setHeartbeatStatus(CommonConstant.ZERO);
                heartbeatResp.setMsg(msg + CommonConstant.Heartbeat.FAIL);
                if(CommonConstant.langTemp.EN_US.equals(lang)){
                    heartbeatResp.setMsg(msg + CommonConstant.Heartbeat.FAIL_EN);
                }else if(CommonConstant.langTemp.FR_FR.equals(lang)){
                    heartbeatResp.setMsg(msg + CommonConstant.Heartbeat.FAIL_FRA);
                }else if(CommonConstant.langTemp.ES_ES.equals(lang)){
                    heartbeatResp.setMsg(msg + CommonConstant.Heartbeat.FAIL_SPA);
                }else if(CommonConstant.langTemp.DE_DE.equals(lang)){
                    heartbeatResp.setMsg(msg + CommonConstant.Heartbeat.FAIL_DE);
                }else if(CommonConstant.langTemp.PL_PL.equals(lang)){
                    heartbeatResp.setMsg(msg + CommonConstant.Heartbeat.FAIL_PL);
                }else if(CommonConstant.langTemp.AR_EG.equals(lang)){
                    heartbeatResp.setMsg(msg + CommonConstant.Heartbeat.FAIL_ARA);
                }
                log.info("getHeartbeat 超过5分钟没有检测到心跳：{}", between);
                return DataResult.success(heartbeatResp);
            }
            heartbeatResp.setMsg(msg + CommonConstant.Heartbeat.SUCCESS);
            if(CommonConstant.langTemp.EN_US.equals(lang)){
                heartbeatResp.setMsg(msg + CommonConstant.Heartbeat.SUCCESS_EN);
            }else if(CommonConstant.langTemp.FR_FR.equals(lang)){
                heartbeatResp.setMsg(msg + CommonConstant.Heartbeat.SUCCESS_FRA);
            }else if(CommonConstant.langTemp.ES_ES.equals(lang)){
                heartbeatResp.setMsg(msg + CommonConstant.Heartbeat.SUCCESS_SPA);
            }else if(CommonConstant.langTemp.DE_DE.equals(lang)){
                heartbeatResp.setMsg(msg + CommonConstant.Heartbeat.SUCCESS_DE);
            }else if(CommonConstant.langTemp.PL_PL.equals(lang)){
                heartbeatResp.setMsg(msg + CommonConstant.Heartbeat.SUCCESS_PL);
            }else if(CommonConstant.langTemp.AR_EG.equals(lang)){
                heartbeatResp.setMsg(msg + CommonConstant.Heartbeat.SUCCESS_ARA);
            }
            heartbeatResp.setHeartbeatStatus(CommonConstant.ONE);
            return DataResult.success(heartbeatResp);
        } else {
            log.info("getHeartbeat 没有检测到心跳{}", heartbeatKey);
            heartbeatResp.setHeartbeatStatus(CommonConstant.ZERO);
            heartbeatResp.setMsg(msg + CommonConstant.Heartbeat.FAIL);
            if(CommonConstant.langTemp.EN_US.equals(lang)){
                heartbeatResp.setMsg(msg + CommonConstant.Heartbeat.FAIL_EN);
            }else if(CommonConstant.langTemp.FR_FR.equals(lang)){
                heartbeatResp.setMsg(msg + CommonConstant.Heartbeat.FAIL_FRA);
            }else if(CommonConstant.langTemp.ES_ES.equals(lang)){
                heartbeatResp.setMsg(msg + CommonConstant.Heartbeat.FAIL_SPA);
            }else if(CommonConstant.langTemp.DE_DE.equals(lang)){
                heartbeatResp.setMsg(msg + CommonConstant.Heartbeat.FAIL_DE);
            }else if(CommonConstant.langTemp.PL_PL.equals(lang)){
                heartbeatResp.setMsg(msg + CommonConstant.Heartbeat.FAIL_PL);
            }else if(CommonConstant.langTemp.AR_EG.equals(lang)){
                heartbeatResp.setMsg(msg + CommonConstant.Heartbeat.FAIL_ARA);
            }
            return DataResult.success(heartbeatResp);
        }
    }

}
