package com.cico.nest.lcdp.boot.core.qxsd.infrastructure.gatewayimpl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.extra.spring.SpringUtil;
import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import com.cico.nest.lcdp.boot.core.qxsd.adapter.web.vo.event.EventDetailRespVO;
import com.cico.nest.lcdp.boot.core.qxsd.adapter.ws.AbstractRealTimeEventWs;
import com.cico.nest.lcdp.boot.core.qxsd.adapter.ws.IRealTimeWs;
import com.cico.nest.lcdp.boot.core.qxsd.domain.gateway.EventGateway;
import com.cico.nest.lcdp.boot.core.qxsd.infrastructure.gatewayimpl.database.EventDetailMapper;
import com.cico.nest.lcdp.boot.core.qxsd.infrastructure.gatewayimpl.database.dataobject.EventDetailDO;
import com.cico.nest.lcdp.boot.core.qxsd.infrastructure.gatewayimpl.http.EventMapper;
import com.cico.nest.lcdp.framework.common.util.date.DateUtils;
import jakarta.annotation.Resource;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @author zws
 * @date 2024/8/15
 */

@Service
public class EventGatewayImpl implements EventGateway {
    @Resource
    private EventMapper eventMapper;
    @Resource
    private EventDetailMapper eventDetailMapper;


    public final Map<String, IRealTimeWs> realTimeWsMap = new ConcurrentHashMap<>();

    @Override
    public void pushRealTimeEventData(String tunnelId) {
        List<EventDetailDO> eventDetailDOList = eventDetailMapper.selectByTunnelIdAndThirdEventStatusNotFinish(tunnelId);
        List<EventDetailRespVO> eventDetailRespList = eventDetailDOList.stream().map(eventDetailDO -> {
            EventDetailRespVO eventDetailRespVO = new EventDetailRespVO();
            BeanUtils.copyProperties(eventDetailDO,eventDetailRespVO);
            if(Objects.nonNull(eventDetailDO.getCarInfoList())){
                eventDetailRespVO.setCarInfoList(JSONArray.parseArray(eventDetailDO.getCarInfoList().toJSONString(), EventDetailDO.EventCarInfo.class));
            }
            long diffSecond = DateUtils.diff(new Date(),eventDetailDO.getEventStartTime())/1000;
            int excessSecond = (int)diffSecond % 60;
            int minute = (int)diffSecond / 60;
            eventDetailRespVO.setRetainedTime(minute+"分"+excessSecond+"秒");
            return eventDetailRespVO;
        }).toList();
        String eventDetailJson = JSONObject.toJSONString(eventDetailRespList);
        IRealTimeWs realTimeWs = getRealTimeWs(tunnelId);
        realTimeWs.sendMessage(eventDetailJson);
    }

    @Override
    public List<EventDetailDO> syncEventDetailList(Date startDateTime, Date endDateTime) {
        return eventMapper.syncEventDetailList(startDateTime,endDateTime);
    }

    public IRealTimeWs getRealTimeWs(String tunnelId){
        IRealTimeWs realTimeWs = realTimeWsMap.get(tunnelId);
        if(Objects.isNull(realTimeWs)){
            Map<String, AbstractRealTimeEventWs> beansOfType = SpringUtil.getBeansOfType(AbstractRealTimeEventWs.class);
            if(CollUtil.isNotEmpty(beansOfType)){
                for(Map.Entry<String, AbstractRealTimeEventWs> entry : beansOfType.entrySet()){
                    AbstractRealTimeEventWs abstractRealTimeEventWs = entry.getValue();
                    realTimeWsMap.put(abstractRealTimeEventWs.tunnelId(),abstractRealTimeEventWs);
                }
            }
        }
        return realTimeWsMap.get(tunnelId);
    }
}
