package com.xhpc.evcs.api;

import com.xhpc.common.api.dto.ChargingStationDto;
import com.xhpc.evcs.dto.*;
import com.xhpc.evcs.utils.JSONUtil;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.*;

import static cn.hutool.core.util.NumberUtil.isInteger;
import static com.xhpc.common.data.redis.StaticBeanUtil.REDIS;

@RestController
public class QueryStationStatusController {


    @PostMapping("/v1/query_station_status")
    public CommonResponse queryStationsInfo(@RequestBody CommonRequest<StationStatusRequest> commonRequest) throws Exception {

        StationStatusRequest stationStatusRequest = JSONUtil.readParams(commonRequest.getData(), StationStatusRequest.class);
        String[] stationIDs = stationStatusRequest.getStationIds();
        List<StationStatusInfo> stationStatusInfos = new ArrayList<>();
        //Loading data through redis at first.
        Collection<String> guns = REDIS.keys("gun:*");
        List<String> gunsNewList = new ArrayList<>();
        for (String value : guns) {
            gunsNewList.add(value.substring(4));
        }
        List<String> gunsIds = new ArrayList<>();
        for (String value : gunsNewList) {
            if (value.length() == 16) {
                gunsIds.add(value);
            }
        }
        Map<String, Integer> statusMap = new HashMap<>();
        String[] keys = {"离线", "空闲", "故障"};
        Integer[] values = {0, 1, 255};
        for (int i = 0; i < keys.length; i++) {
            statusMap.put(keys[i], values[i]);
        }
        for (String stationID : stationIDs) {
            ChargingStationDto chargingStationDto = REDIS.getCacheObject("station:" + stationID);
            Set<String> pileIds = new HashSet<>();
            if (null != chargingStationDto) {
                pileIds = chargingStationDto.getPiles();
            }
            boolean existsGun = false;
            List<ConnectorStatusInfo> connectorStatusInfos = new ArrayList<>();
            for (String pileId : pileIds) {
                for (String value : gunsIds) {
                    if (pileId.equals(value.substring(0, 14))) {
                        existsGun = true;
                        ConnectorStatusInfo connectorStatusInfo = new ConnectorStatusInfo();
                        Object status = REDIS.getCacheMapValue("gun:" + value, "status");
                        if (isInteger(status.toString())) {
                            connectorStatusInfo.setStatus(3);
                        } else {
                            connectorStatusInfo.setStatus(statusMap.get(status));
                        }
                        connectorStatusInfo.setConnectorID(value);
                        connectorStatusInfos.add(connectorStatusInfo);
                    }
                }
            }
            if (existsGun) {
                StationStatusInfo stationStatusInfo = new StationStatusInfo();
                stationStatusInfo.setStationID(stationID);
                stationStatusInfo.setConnectorStatusInfos(connectorStatusInfos);
                stationStatusInfos.add(stationStatusInfo);
            }
        }

        StationStatusInfoWrapper stationStatusInfoWrappers = new StationStatusInfoWrapper();
        stationStatusInfoWrappers.setTotal(stationStatusInfos.size());
        if (stationStatusInfos.size() != 0) {
            stationStatusInfoWrappers.setStationStatusInfos(stationStatusInfos);
        }
        CommonResponse resp = new CommonResponse();
        resp.setRet("0");
        resp.setMsg("Query station status success");
        resp.setData(JSONUtil.toJSONString(stationStatusInfoWrappers));
        return resp;
    }

}
