package com.fizz.business.comm.RabbitMQ;

import cn.hutool.json.JSONUtil;
import com.fizz.business.constants.CommonConstants;
import com.fizz.business.constants.enums.WsTypeEnum;
import com.fizz.business.service.ModSetupResultService;
import com.fizz.business.service.impl.ModSetupResultServiceImpl;
import com.fizz.business.utils.WebSocketUtil;
import com.fizz.business.vo.ModSetupResultKeyVO;
import com.fizz.business.vo.ModSetupResultVO;
import lombok.extern.log4j.Log4j2;
import org.apache.commons.compress.utils.Lists;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.messaging.handler.annotation.Headers;
import org.springframework.messaging.handler.annotation.Payload;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

@Log4j2
//@Component
public class RabbitQueueListener {

    public static List<String> measureList = Lists.newArrayList();

    @Resource
    ModSetupResultService modSetupResultService;

    private double LastRolledLength = 0d;
    private int LastPassNo = -1;

//    @RabbitListener(queues = CommonConstants.RabbitMQ.RECEIVE_MODEL)
//    @RabbitHandler
//    public void onHmiMessage(String message) {
//        log.info("消费端ProcData: " + message);
//
//        //socket
//    }

    @RabbitListener(queues = CommonConstants.RabbitMQ.RECEIVE_REAL_TIME)
    @RabbitHandler
    public void onProcDataMessage(String message) {
        log.info("消费端ProcData: " + message);
        Double rolledLength = Double.parseDouble(
                JSONUtil.parse(message).getByPath("rolledLength").toString()
        );
        if(Math.abs(LastRolledLength-rolledLength)<1){
            return;
        }
        int passNo = Integer.parseInt(
                JSONUtil.parse(message).getByPath("passNo").toString()
        );
        if(LastPassNo!=passNo) {
            measureList = Lists.newArrayList();
        }
        measureList.add(message);
        LastRolledLength=rolledLength;
        LastPassNo = passNo;
        //socket
        WebSocketUtil.sendMessage(WsTypeEnum.track_measure,message);
    }



    @RabbitListener(queues = CommonConstants.RabbitMQ.RECEIVE_MODEL)
    @RabbitHandler
    public void receiveMessage(@Payload String message, @Headers Map<String, Object> headers) {
        // 获取 messageId
        String messageId = (String) headers.get("amqp_messageId");

        // 去掉字符串两端的引号
        message = message.replaceAll("^\"|\"$", "");
        // 处理消息逻辑
        if ("TOTAL_PRESET_DONE".equalsIgnoreCase(messageId)) {

            List<ModSetupResultVO> setup = modSetupResultService.getSetupByCoilid(message);

            ModSetupResultKeyVO modSetupResultKeyVO = new ModSetupResultKeyVO();
            modSetupResultKeyVO.setKey(ModSetupResultServiceImpl.WS_EKY);
            modSetupResultKeyVO.setFlag(true);
            modSetupResultKeyVO.setLists(setup);
            //socket
            WebSocketUtil.sendMessage(WsTypeEnum.calc_setup_result,JSONUtil.toJsonStr(modSetupResultKeyVO));
        } else  if ("TOTAL_PRESET_FAILED".equalsIgnoreCase(messageId)) {

            ModSetupResultKeyVO modSetupResultKeyVO = new ModSetupResultKeyVO();
            modSetupResultKeyVO.setKey(ModSetupResultServiceImpl.WS_EKY);
            modSetupResultKeyVO.setFlag(false);
            WebSocketUtil.sendMessage(WsTypeEnum.calc_setup_result,JSONUtil.toJsonStr(modSetupResultKeyVO));
        }
    }

}