package com.ningdatech.carapi.road.manage;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.ningdatech.cache.model.cache.CacheKey;
import com.ningdatech.cache.repository.CachePlusOps;
import com.ningdatech.carapi.road.constant.RoadStatusTypeEnum;
import com.ningdatech.carapi.road.model.vo.ComprehensiveSituationVO;
import com.ningdatech.carapi.scheduler.contants.TaskContant;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Component;

import java.util.Objects;

/**
 * @author CMM
 * @since 2024/10/23 10:11
 */
@Slf4j
@Component
@RequiredArgsConstructor
public class RoadMonitorManage {

    private final CachePlusOps cachePlusOps;

    public ComprehensiveSituationVO getComSitData() {
        ComprehensiveSituationVO vo = new ComprehensiveSituationVO();
        // 设置默认值
        vo.setRoadSafetyIndex(100);
        vo.setRoadStatus("通畅");
        vo.setWeatherStatus("良好");
        vo.setDangerousActionCount(0);
        // 实时从缓存中获取算法分析的结果
        String allData = cachePlusOps.get(new CacheKey(TaskContant.RedisKey.ALGORITHM_REDIS_ALL_DATA_KEY));
        JSONObject jsonObject = JSON.parseObject(allData);
        if (Objects.nonNull(jsonObject)) {
            // 道路情况
            String congestion = jsonObject.getString("Congestion");
            RoadStatusTypeEnum roadStatusTypeEnum = RoadStatusTypeEnum.valueOf(congestion);
            if (StringUtils.isNotBlank(congestion)){
                switch (roadStatusTypeEnum){
                    case HEAVY:
                        vo.setRoadStatus(RoadStatusTypeEnum.getDescByCode(RoadStatusTypeEnum.HEAVY.getCode()));
                        break;
                    case MEDIUM:
                        vo.setRoadStatus(RoadStatusTypeEnum.getDescByCode(RoadStatusTypeEnum.MEDIUM.getCode()));
                        break;
                    case LIGHT:
                        vo.setRoadStatus(RoadStatusTypeEnum.getDescByCode(RoadStatusTypeEnum.LIGHT.getCode()));
                        break;
                    default:
                        vo.setRoadStatus("良好");
                }
            }
            // 天气情况
            String weather = jsonObject.getString("Weather");

            vo.setWeatherStatus(jsonObject.getString("weatherStatus"));
            vo.setRoadSafetyIndex(jsonObject.getInteger("roadSafetyIndex"));
            vo.setDangerousActionCount(jsonObject.getInteger("dangerousActionCount"));
            return vo;
        }
        return null;
    }
}
