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

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import com.cico.nest.lcdp.boot.core.qxsd.adapter.config.runner.WsApplicationRunner;
import com.cico.nest.lcdp.boot.core.qxsd.domain.vehicle.HistoryTunnelVehicle;
import com.cico.nest.lcdp.boot.core.qxsd.domain.vehicle.TunnelRealTimeVehicle;
import com.cico.nest.lcdp.boot.core.qxsd.domain.vehicle.VehicleInfo;
import com.cico.nest.lcdp.boot.core.qxsd.infrastructure.gatewayimpl.http.xlw.dto.XlwEventListDTO;
import com.cico.nest.lcdp.boot.core.qxsd.infrastructure.gatewayimpl.http.xlw.dto.XlwHolographicTimeHistoryDTO;
import com.cico.nest.lcdp.boot.core.qxsd.infrastructure.gatewayimpl.ws.xlw.dto.XlwRealTimeEventDTO;
import com.cico.nest.lcdp.boot.core.qxsd.infrastructure.gatewayimpl.ws.xlw.dto.XlwRealTimeVehicleDTO;
import com.cico.nest.lcdp.framework.common.util.date.DateUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Objects;
import java.util.concurrent.atomic.AtomicReference;

import static com.cico.nest.lcdp.boot.core.qxsd.domain.vehicle.VehicleType.*;

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

public class VehicleConvertor {

    public static TunnelRealTimeVehicle convert2TunnelRTV(XlwRealTimeVehicleDTO dto,String tunnelId,String direction,int startMilestone) {

        TunnelRealTimeVehicle tunnelRealTimeVehicle = new TunnelRealTimeVehicle();
        BeanUtils.copyProperties(dto,tunnelRealTimeVehicle);


        List<XlwRealTimeVehicleDTO.CarInfo> carInfo = dto.getCarInfo();
        tunnelRealTimeVehicle.setVehicleInfoList(new ArrayList<>());
        //危化品车辆数量
        AtomicReference<Integer> dangerousCount = new AtomicReference<>(0);
        //轿车数量
        AtomicReference<Integer> sedanCount = new AtomicReference<>(0);
        //客车数量
        AtomicReference<Integer> passengerCount = new AtomicReference<>(0);
        //货车数量
        AtomicReference<Integer> truckCount = new AtomicReference<>(0);
        //未知数量
        AtomicReference<Integer> otherCount = new AtomicReference<>(0);
        if(Objects.nonNull(carInfo)){
            Long currentTime = System.currentTimeMillis();
            List<VehicleInfo> vehicleInfoList = carInfo.stream().map(xlwCarInfo -> {
                VehicleInfo vehicleInfo = new VehicleInfo();
                BeanUtils.copyProperties(xlwCarInfo,vehicleInfo);
                //vehicleInfo.setOriginDistance(3055f);
                //设备桩米号
                if(Objects.nonNull(xlwCarInfo.getDeviceSn())){
                    String deviceMilestone = WsApplicationRunner.deviceMap.get(xlwCarInfo.getDeviceSn());
                    if(Objects.nonNull(deviceMilestone)){
                        vehicleInfo.setDeviceMilestone(XlwRealTimeEventDTO.convertMilestoneStr(deviceMilestone));
                    }
                }
                //车头图片和车牌图片
                vehicleInfo.setHeadImageUrl(xlwCarInfo.getHeadImage());
                vehicleInfo.setPlateImageUrl(xlwCarInfo.getPlateImage());
                //车辆桩米号
                //上行=结束桩号-行驶距离= endMilestone-xlwCarInfo.getOriginDistance()
                //下行=开始桩号+行驶距离= startMilestone+xlwCarInfo.getOriginDistance()
                if("下行".equals(direction)) {
                    vehicleInfo.setCarMilestone((int) (xlwCarInfo.getOriginDistance() + startMilestone));
                }else {
                    vehicleInfo.setCarMilestone((int) (startMilestone-xlwCarInfo.getOriginDistance()));
                }
                //车辆类型
                Integer vehicleType = VehicleConvertor.convert2VehicleType(xlwCarInfo);
                vehicleInfo.setTunnelId(tunnelId);
                vehicleInfo.setVehicleType(vehicleType);
                if(vehicleType == hazardous_chemicals.type){
                    dangerousCount.set(dangerousCount.get() + 1);
                } else if (vehicleType == passenger.type) {
                    passengerCount.getAndSet(passengerCount.get() + 1);
                }else if(vehicleType == sedan.type){
                    sedanCount.getAndSet(sedanCount.get() + 1);
                }else if (vehicleType == truck.type){
                    truckCount.getAndSet(truckCount.get() + 1);
                }else if(vehicleType == other_car.type){
                    otherCount.getAndSet(otherCount.get() + 1);
                }
                //留存时间计算
                vehicleInfo.setRetainedMinutesTime(5);
                if(Objects.nonNull(xlwCarInfo.getInTunnelTime())){
                    long diffMinTime = ((currentTime - xlwCarInfo.getInTunnelTime())/1000/60);
                    if(diffMinTime < 0){
//                        log.error("留存时间计算异常:值{},currentTime:{}-xlwCarInfo.getInTunnelTime():{}",diffMinTime,DateUtil.formatDateTime(new Date(currentTime)),DateUtil.formatDateTime(new Date(xlwCarInfo.getInTunnelTime())));
                        diffMinTime = 0;
                    }
                    vehicleInfo.setRetainedMinutesTime((int) diffMinTime);
                }
                //进入隧道时间转换
                if(Objects.nonNull(xlwCarInfo.getInTunnelTime())){
                    Date date = DateUtil.date(xlwCarInfo.getInTunnelTime());
                    String format = DateUtil.format(date, DateUtils.FORMAT_YEAR_MONTH_DAY_HOUR_MINUTE_SECOND);
                    vehicleInfo.setInTunnelTime(format);
                }else{
                    vehicleInfo.setInTunnelTime("2024-01-01 24:00:00");
                }
                vehicleInfo.setDirection(direction);
                return vehicleInfo;
            }).toList();
            //获取卡口图片
            //TunnelRealTimeVehicle.setBayonetPictures(vehicleInfoList);

            tunnelRealTimeVehicle.setDangerousCount(dangerousCount.get());
            tunnelRealTimeVehicle.setSedanCount(sedanCount.get());
            tunnelRealTimeVehicle.setPassengerCount(passengerCount.get());
            tunnelRealTimeVehicle.setTruckCount(truckCount.get());
            tunnelRealTimeVehicle.setOtherCount(otherCount.get());
            tunnelRealTimeVehicle.setVehicleInfoList(vehicleInfoList);
        }
        return tunnelRealTimeVehicle;
    }

    public static List<HistoryTunnelVehicle> convert2HistoryTunnelVehicle(List<XlwHolographicTimeHistoryDTO> dto,String tunnelId,String direction,String startMilestone){
        return dto.stream().map(e->VehicleConvertor.convert2HistoryTunnelVehicle(e,tunnelId,direction,startMilestone)).toList();
    }

    public static HistoryTunnelVehicle convert2HistoryTunnelVehicle(XlwHolographicTimeHistoryDTO dto,String tunnelId,String direction,String startMilestone) {
        HistoryTunnelVehicle historyTunnelVehicle = new HistoryTunnelVehicle();
        historyTunnelVehicle.setTime(dto.getTime());
        List<XlwRealTimeVehicleDTO.CarInfo> carInfo = dto.getFrame();

        if(Objects.nonNull(carInfo)){
            Long currentTime = System.currentTimeMillis();
            List<VehicleInfo> vehicleInfoList = carInfo.stream().map(xlwCarInfo -> {
                VehicleInfo vehicleInfo = new VehicleInfo();
                BeanUtils.copyProperties(xlwCarInfo,vehicleInfo);
                //设备桩米号
                if(Objects.nonNull(xlwCarInfo.getDeviceSn())){
                    String deviceMilestone = WsApplicationRunner.deviceMap.get(xlwCarInfo.getDeviceSn());
                    if(Objects.nonNull(deviceMilestone)){
                        vehicleInfo.setDeviceMilestone(XlwRealTimeEventDTO.convertMilestoneStr(deviceMilestone));
                    }
                }
                //车头图片和车牌图片
                vehicleInfo.setHeadImageUrl(xlwCarInfo.getHeadImage());
                vehicleInfo.setPlateImageUrl(xlwCarInfo.getPlateImage());
                //车辆桩米号
                vehicleInfo.setCarMilestone((int) (xlwCarInfo.getOriginDistance() +  Integer.parseInt(startMilestone)));
                //车辆类型
                Integer vehicleType = VehicleConvertor.convert2VehicleType(xlwCarInfo);
                vehicleInfo.setTunnelId(tunnelId);
                vehicleInfo.setVehicleType(vehicleType);
                //留存时间计算
                vehicleInfo.setRetainedMinutesTime(5);
                if(Objects.nonNull(xlwCarInfo.getInTunnelTime())){
                    long diffMinTime = ((currentTime - xlwCarInfo.getInTunnelTime())/1000/60);
                    vehicleInfo.setRetainedMinutesTime((int) diffMinTime);
                }
                //进入隧道时间转换
                //vehicleInfo.setInTunnelTime("2024-01-01 24:00:00");
                if(Objects.nonNull(xlwCarInfo.getInTunnelTime())){
                    Date date = DateUtil.date(xlwCarInfo.getInTunnelTime());
                    String format = DateUtil.format(date, DateUtils.FORMAT_YEAR_MONTH_DAY_HOUR_MINUTE_SECOND);
                    vehicleInfo.setInTunnelTime(format);
                }
                vehicleInfo.setDirection(direction);
                return vehicleInfo;
            }).toList();
            historyTunnelVehicle.setFrame(vehicleInfoList);
        }

        /*if(Objects.nonNull(carInfo)){
            Long currentTime = System.currentTimeMillis();
            List<VehicleInfo> vehicleInfoList = carInfo.stream().map(xlwCarInfo -> {
                VehicleInfo vehicleInfo = new VehicleInfo();
                BeanUtils.copyProperties(xlwCarInfo,vehicleInfo);
                Integer vehicleType = VehicleConvertor.convert2VehicleType(xlwCarInfo);
                vehicleInfo.setVehicleType(vehicleType);
                //留存时间计算
                vehicleInfo.setRetainedMinutesTime(0);
                if(Objects.nonNull(xlwCarInfo.getInTunnelTime())){
                    long diffMinTime = ((currentTime - xlwCarInfo.getInTunnelTime())/1000/60);
                    vehicleInfo.setRetainedMinutesTime((int) diffMinTime);
                }
                //进入隧道时间转换
                if(Objects.nonNull(xlwCarInfo.getInTunnelTime())){
                    Date date = DateUtil.date(xlwCarInfo.getInTunnelTime());
                    String format = DateUtil.format(date, DateUtils.FORMAT_YEAR_MONTH_DAY_HOUR_MINUTE_SECOND);
                    vehicleInfo.setInTunnelTime(format);
                }
                return vehicleInfo;
            }).toList();
            historyTunnelVehicle.setFrame(vehicleInfoList);
        }*/
        return historyTunnelVehicle;
    }


    public static Integer convert2VehicleType(XlwRealTimeVehicleDTO.CarInfo carInfo) {
        if(Objects.nonNull(carInfo.getIsDangerous()) && carInfo.getIsDangerous() == 1){
            return hazardous_chemicals.type;
        }
        if(StrUtil.isEmpty(carInfo.getCarType())){
            return other_car.type;
        }
        return switch (carInfo.getCarType()) {
            case "轿车","SUV/MPV","SUV/MPV车型","小型轿车","微型轿车","面包车" -> sedan.type;
            case "皮卡","微卡","货车","小货车","皮卡车","重中型货车","轻微型货车" -> truck.type;
            case "大中型客车","大型客车","中型客车","客车","越野客车" -> passenger.type;
            default -> other_car.type;
        };
    }

}
