package com.cico.nest.lcdp.boot.core.qxsd.application.service.impl;

import cn.hutool.core.date.DateUnit;
import cn.hutool.core.date.DateUtil;
import com.alibaba.fastjson2.JSONArray;
import com.cico.nest.lcdp.boot.core.qxsd.application.service.HistoryService;
import com.cico.nest.lcdp.boot.core.qxsd.domain.gateway.HistoryGateway;
import com.cico.nest.lcdp.boot.core.qxsd.domain.vehicle.HistoryTunnelVehicle;
import com.cico.nest.lcdp.boot.core.qxsd.infrastructure.gatewayimpl.database.EventHisMapper;
import com.cico.nest.lcdp.boot.core.qxsd.infrastructure.gatewayimpl.database.TunnelDetailMapper;
import com.cico.nest.lcdp.boot.core.qxsd.infrastructure.gatewayimpl.database.dataobject.EventDetailDO;
import com.cico.nest.lcdp.boot.core.qxsd.infrastructure.gatewayimpl.database.dataobject.EventHisDO;
import com.cico.nest.lcdp.boot.core.qxsd.infrastructure.gatewayimpl.database.dataobject.TunnelDetailDO;
import com.cico.nest.lcdp.boot.core.qxsd.infrastructure.gatewayimpl.http.hk.HkVideoMapper;
import com.cico.nest.lcdp.boot.core.qxsd.infrastructure.gatewayimpl.http.xlw.dto.XlwHolographicTimeHistoryDTO;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.simpleframework.xml.Path;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.*;

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

@Service
@Slf4j
public class HistoryServiceImpl implements HistoryService {

    @Resource
    private HistoryGateway historyGateway;
    @Resource
    private EventHisMapper eventHisMapper;
    @Resource
    private TunnelDetailMapper tunnelDetailMapper;

    /**
     * 保存事件历史数据
     * @param eventDetailDO 事件信息
     */
    @Override
    public void saveEventHistoryData(EventDetailDO eventDetailDO) {
        EventHisDO eventHisDO = new EventHisDO();
        eventHisDO.setEventId(eventDetailDO.getId());
        eventHisDO.setCreateTime(new Date());
        //保存事件图片
        try {
            List<String> eventImagesPathList = eventDetailDO.getEventImagesPath();
            List<String> eventImagesSavePath = new ArrayList<>();
            for(String urlPath : eventImagesPathList){
                if(urlPath.startsWith("/")){
                    String saveUrlPath = "";
                    try{
                        saveUrlPath = historyGateway.saveEventImage(urlPath);
                    }catch (Throwable e){
                        saveUrlPath = "error获取事件图片失败";
                        log.error("获取事件图片失败",e);
                    }
                    eventImagesSavePath.add(saveUrlPath);
                }
            }
            eventHisDO.setEventImagesPath(eventImagesSavePath);
        } catch (Exception e) {

        }
        //获取轨迹数据
        try{
            TunnelDetailDO tunnelDetailDO = tunnelDetailMapper.selectById(eventDetailDO.getTunnelId());
            List<HistoryTunnelVehicle> historyTunnelVehicleList =
                    historyGateway.getVehicleInfo(eventDetailDO.getThirdPartyId(),
                            eventDetailDO.getTunnelId(),
                            eventDetailDO.getDirection(),
                            tunnelDetailDO.getStartMilestone());
            eventHisDO.setHisVehicleInfo(JSONArray.from(historyTunnelVehicleList));
        }catch (Throwable e){
            eventHisDO.setHisVehicleInfo(JSONArray.from(new ArrayList<>()));
            log.error("获取轨迹数据失败",e);
        }
        //获取历史视频数据
        try{
            Date eventStartTime = eventDetailDO.getEventStartTime();
            Date eventEndTime = eventDetailDO.getEventEndTime();
            long diffSecond = DateUtil.between(eventStartTime,eventEndTime, DateUnit.SECOND);
            if(Objects.nonNull(eventDetailDO.getDeviceMilestone())){
                String beginTime = DateUtil.format(DateUtil.offsetSecond(eventStartTime,-6),"yyyy-MM-dd'T'HH:mm:ss.SSSXXX");
                String endTime = DateUtil.format(eventEndTime,"yyyy-MM-dd'T'HH:mm:ss.SSSXXX");
                if(diffSecond > 24){
                    endTime = DateUtil.format(DateUtil.offsetSecond(eventStartTime,24),"yyyy-MM-dd'T'HH:mm:ss.SSSXXX");
                }

                int direction = 0;
                if("下行".equals(eventDetailDO.getDirection())){
                    direction = 1;
                }
                    //todo 自动生成的id不能写死啊 我日
                    String videoUrl = historyGateway.saveVideo("d08071ebf16f0047ee5ff770bff1752f"
                            ,Integer.valueOf(eventDetailDO.getDeviceMilestone())
                            ,direction
                            ,beginTime
                            ,endTime
                            ,eventDetailDO.getId() + ".mp4");
                    eventHisDO.setVideoPath("/" + videoUrl);
            }
        }catch (Throwable e){
            eventHisDO.setVideoPath("error获取历史视频失败");
            log.error("获取历史视频失败",e);
        }
        //获取卡口图片
        try {
            List<String> carPlateList = eventDetailDO.getEventCarPlate();
            JSONArray jsonArray = new JSONArray();
            for(String carPlate : carPlateList){
                try{
                    List<String> bayonetImageList = historyGateway.saveBayonetImage(carPlate,eventDetailDO.getEventStartTime());
                    Map<String,Object> bayonetImageMap = Map.of(carPlate,bayonetImageList);
                    jsonArray.add(bayonetImageMap);
                }catch (Throwable e){
                   log.error("获取卡口图片失败",e);
                }
            }
            eventHisDO.setBayonetImage(jsonArray);
        } catch (Exception e) {
        }

        eventHisMapper.insert(eventHisDO);
    }




}
