package com.kym.service.cache;

import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @author skyline
 * @description
 * @date 2023-08-25 22:58
 */
public enum KymCache {
    INSTANCE;

    private static ConcurrentHashMap<String, String> CONNECTOR_ID_SHORT_ID_MAPPING = new ConcurrentHashMap<>();
    private static ConcurrentHashMap<String, String> CONNECTOR_ID_STATION_ID_MAPPING = new ConcurrentHashMap<>();
    private static ConcurrentHashMap<String, String> STATION_MAPPING = new ConcurrentHashMap<>();
    private static ConcurrentHashMap<Long, String> ADMIN_USER_STATION = new ConcurrentHashMap<>();
    private static ConcurrentHashMap<Long, List<String>> ADMIN_USER_STATIONS = new ConcurrentHashMap<>();
    private static ConcurrentHashMap<String, String> EQUIPMENT_ID_PARKING_NO_MAPPING = new ConcurrentHashMap<>();

    public void putShort2EquipmentId(Map<String, String> map) {
        CONNECTOR_ID_SHORT_ID_MAPPING.putAll(map);
    }

    /**
     * 通过设备编号或者枪头编号获取设备短编号
     *
     * @param id
     * @return
     */
    public String getShortIdByEquipmentIdOrConnectorId(String id) {
        var connectorId = getConnectorId(id);
        var res = CONNECTOR_ID_SHORT_ID_MAPPING.get(connectorId);
//        if (res == null) {
//            var relation = relationService.lambdaQuery().eq(EquipmentRelation::getEquipmentId, equipmentId).one();
//            if (relation != null) {
//                res = relation.getShortId();
//                CONNECTOR_ID_SHORT_ID_MAPPING.put(equipmentId, res);
//            }
//        }
        return res;
    }


    public void putConnector2Station(Map<String, String> map) {
        CONNECTOR_ID_STATION_ID_MAPPING.putAll(map);
    }

    /**
     * 通过设备编号或者枪头编号获取站点id
     *
     * @param id
     * @return
     */
    public String getStationIdByEquipmentIdOrConnectorId(String id) {
        var connectorId = getConnectorId(id);
        var res = CONNECTOR_ID_STATION_ID_MAPPING.get(connectorId);
//        if (res == null) {
//            var relation = relationService.lambdaQuery().eq(EquipmentRelation::getConnectorId, connectorId).one();
//            if (relation != null) {
//                res = relation.getStationId();
//                CONNECTOR_STATION_MAPPING.put(connectorId, res);
//            }
//        }
        return res;
    }

    public void putStationId2Name(Map<String, String> map) {
        STATION_MAPPING.putAll(map);
    }

    /**
     * 通过站点id获取站点名称
     *
     * @param stationId
     * @return
     */
    public String getStationNameById(String stationId) {
        return STATION_MAPPING.get(stationId);
    }

    /**
     * 操作员对应有权限的站点
     *
     * @param map
     */
    public void putAdminUser2Stations(Map<Long, List<String>> map) {
        ADMIN_USER_STATIONS.putAll(map);
    }

    /**
     * 获取运营平台操作员有权限的站点ID
     *
     * @param adminUserId
     * @return
     */
    public String getAdminUserStationId(Long adminUserId) {
        var res = ADMIN_USER_STATION.get(adminUserId);
//        if (res == null) {
//            var adminUserStation = adminUserStationService.lambdaQuery().eq(AdminUserStation::getAdminUserId, adminUserId).one();
//            res = adminUserStation == null ? null : adminUserStation.getStationId();
//        }
        return res;
    }

    /**
     * 获取运营平台操作员有权限的站点ID
     *
     * @param adminUserId
     * @return
     */
    public List<String> getAdminUserStationIds(Long adminUserId) {
        return ADMIN_USER_STATIONS.get(adminUserId);
    }

    public void putConnectorId2ParkingNo(Map<String, String> map) {
        EQUIPMENT_ID_PARKING_NO_MAPPING.putAll(map);
    }

    /**
     * @param id
     * @return
     */
    public String getParkNoByEquipmentIdOrConnectorId(String id) {
        var connectorId = getConnectorId(id);
        return EQUIPMENT_ID_PARKING_NO_MAPPING.get(connectorId);
    }

    /**
     * 将equipmentId,connectorId,shortId转化成17为的connectorId
     *
     * @param id
     * @return
     */
    public String getConnectorId(String id) {
        return switch (id.length()) {
            case 17 -> id;
            case 16 -> id.concat("1");
            case 6 ->
                    CONNECTOR_ID_SHORT_ID_MAPPING.entrySet().stream().filter(entry -> id.equals(entry.getValue())).map(Map.Entry::getKey).findFirst().get();
            default -> null;
        };
    }
}
