package com.cico.nest.lcdp.boot.core.qxsd.domain.vehicle;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.extra.spring.SpringUtil;
import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONObject;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cico.nest.lcdp.boot.core.qxsd.adapter.web.vo.RealTimeVehicleVO;
import com.cico.nest.lcdp.boot.core.qxsd.infrastructure.common.constatnt.RedisConstants;
import com.cico.nest.lcdp.boot.core.qxsd.infrastructure.gatewayimpl.database.dataobject.BayonetPicturesDO;
import com.cico.nest.lcdp.framework.common.pojo.CommonResult;
import lombok.Data;
import org.springframework.beans.BeanUtils;
import org.springframework.data.redis.core.StringRedisTemplate;

import java.util.*;
import java.util.concurrent.atomic.AtomicReference;
import java.util.stream.Collectors;

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

/**
 * 隧道实时车辆信息
 * @author zws
 * @date 2024/8/8
 */
@Data
public class TunnelRealTimeVehicle {
    //车辆列表
    private List<VehicleInfo> vehicleInfoList;

    //危化品车辆数量
    private Integer dangerousCount = 0;
    //轿车数量
    private Integer sedanCount = 0;
    //客车数量
    private Integer passengerCount = 0;
    //货车数量
    private Integer truckCount = 0;
    //未知数量
    private Integer otherCount = 0;


   /* //大客车数量
    private Integer commercialCount;

    //数据帧最后时间，格式：时间戳
    private Long endImageTime;
    //数据帧发送时间，格式：时间戳
    private Long frameSendTIme;
    //大型车数量
    private Integer largeCount;
    //大型车时速
    private Float largeSpeed;
    //通行等级
    private Integer passLevel;
    //小型车数量
    private Integer smallCount;
    //小型车速度
    private Float smallSpeed;*/

    /**
     * 重新计算指标汇总数据
     */
    public  TunnelRealTimeVehicle calculateMetrics(List<VehicleInfo> filterVehicleList){
        if(Objects.nonNull(filterVehicleList)){
            //危化品车辆数量
            int dangerousCount = 0;
            //轿车数量
            int sedanCount = 0;
            //客车数量
            int passengerCount = 0;
            //货车数量
            int truckCount = 0;
            //未知数量
            int otherCount = 0;
            for(VehicleInfo vehicleInfo : filterVehicleList){
                Integer vehicleType = vehicleInfo.getVehicleType();
                if(vehicleType == hazardous_chemicals.type){
                    dangerousCount = dangerousCount + 1;
                } else if (vehicleType == passenger.type) {
                    passengerCount = passengerCount + 1;
                }else if(vehicleType == sedan.type){
                    sedanCount = sedanCount + 1;
                }else if (vehicleType == truck.type){
                    truckCount = truckCount + 1;
                }else if(vehicleType == other_car.type){
                    otherCount = otherCount + 1;
                }
            }
            this.setVehicleInfoList(filterVehicleList);
            this.setDangerousCount(dangerousCount);
            this.setSedanCount(sedanCount);
            this.setPassengerCount(passengerCount);
            this.setTruckCount(truckCount);
            this.setOtherCount(otherCount);
        }

        return this;
    }

    /**
     * 多个隧道的留存车辆相加
     * @param addVehicle
     * @return
     */
    public TunnelRealTimeVehicle add(TunnelRealTimeVehicle addVehicle){
        TunnelRealTimeVehicle newVehicle = new TunnelRealTimeVehicle();

        List<VehicleInfo> vehicleInfoList = this.getVehicleInfoList();
        List<VehicleInfo> addVehicleList = addVehicle.getVehicleInfoList();
        vehicleInfoList.addAll(addVehicleList);
        vehicleInfoList = vehicleInfoList.stream()
                .sorted(Comparator.comparing(VehicleInfo::getCarId,Comparator.nullsFirst(Comparator.naturalOrder())))
                .collect(Collectors.toList());
        newVehicle.setVehicleInfoList(vehicleInfoList);

        //货车数量
        newVehicle.setTruckCount(this.getTruckCount() + addVehicle.getTruckCount());
        //危化品车辆数量
        newVehicle.setDangerousCount(this.getDangerousCount() + addVehicle.getDangerousCount());
        //客车数量
        newVehicle.setPassengerCount(this.getPassengerCount() + addVehicle.getPassengerCount());
        //轿车数量
        newVehicle.setSedanCount(this.getSedanCount() + addVehicle.getSedanCount());
        //其它
        newVehicle.setOtherCount(this.getOtherCount() + addVehicle.getOtherCount());
        return newVehicle;
    }


    public static RealTimeVehicleVO convert2VO(TunnelRealTimeVehicle tunnelRealTimeVehicle,Integer pageNo,Integer pageSize){
        RealTimeVehicleVO realTimeVehicleVO = new RealTimeVehicleVO();
        BeanUtils.copyProperties(tunnelRealTimeVehicle,realTimeVehicleVO);
        Integer allCount  = tunnelRealTimeVehicle.getTruckCount()
                + tunnelRealTimeVehicle.getSedanCount()
                + tunnelRealTimeVehicle.getPassengerCount()
                + tunnelRealTimeVehicle.getDangerousCount()
                + tunnelRealTimeVehicle.getOtherCount();
        realTimeVehicleVO.setAllCount(allCount);

        List<VehicleInfo> vehicleInfoList = tunnelRealTimeVehicle.getVehicleInfoList();
        Page<VehicleInfo> page = new Page<>(pageNo, pageSize,Objects.isNull(vehicleInfoList) ? 0 : vehicleInfoList.size() );

        if(CollUtil.isEmpty(vehicleInfoList)){
            page.setRecords(vehicleInfoList);
            realTimeVehicleVO.setVehicleInfoList(page);
            return realTimeVehicleVO;
        }
        pageNo = pageNo < 1 ? 1 : pageNo;
        pageSize = pageSize < 1 ? 1 : pageSize;
        int totalSize = vehicleInfoList.size();
        int start = Math.min((pageNo - 1) * pageSize, totalSize);
        int end = Math.min(pageNo * pageSize, totalSize);
        vehicleInfoList = vehicleInfoList.subList(start,end);
        //获取车头车牌图片路径
        //setBayonetPictures(vehicleInfoList);

        page.setRecords(vehicleInfoList);
        realTimeVehicleVO.setVehicleInfoList(page);
        return realTimeVehicleVO;
    }

    public static void setBayonetPictures(List<VehicleInfo> vehicleInfoList){
        if(CollUtil.isEmpty(vehicleInfoList)){
            return;
        }
        Set<String> redisKeys = vehicleInfoList
                .stream()
                .map(value-> RedisConstants.REDIS_BAYONET_PICTURES + value.getCarPlate())
                .collect(Collectors.toSet());
        StringRedisTemplate stringRedisTemplate = SpringUtil.getBean(StringRedisTemplate.class);
        List<String> imagesList = stringRedisTemplate.opsForValue().multiGet(redisKeys)
                .stream().filter(Objects::nonNull).toList();
        if(CollUtil.isEmpty(imagesList)){
            return;
        }
        Map<String,BayonetPicturesDO> bayonetPicturesDOMap =imagesList.stream()
                .map(str-> JSONObject.parseObject(str, BayonetPicturesDO.class))
                .collect(Collectors.toMap(BayonetPicturesDO::getPlateNumber, t->t));
        for(VehicleInfo vehicleInfo:vehicleInfoList){
            BayonetPicturesDO bayonetPicturesDO = bayonetPicturesDOMap.get(vehicleInfo.getCarPlate());
            if(Objects.nonNull(bayonetPicturesDO)){
                vehicleInfo.setHeadImageUrl(bayonetPicturesDO.getHeadImageUrl());
                vehicleInfo.setPlateImageUrl(bayonetPicturesDO.getPlateImageUrl());
            }
        }

    }

}
