// Copyright (c) 2020 UWB & LESP.
// The UWB & LESP license this file to you under the MIT license.

package cz.senslog.connector.model.converter;

import cz.senslog.connector.tools.util.Tuple;

import java.util.HashMap;
import java.util.Map;

/**
 * Converter sensor EUI in hex value to sensor ID in dec value.
 */
public class AzureUnitConverter {

    private final static ReverseMap<String, Long> UNITS = new ReverseMap<>();
    private final static Map<Long, ReverseMap<String, Long>> SENSORS = new HashMap<>();

    private static void register(String eui, long unitId, ReverseMap<String, Long> sensorsReverseMap) {
        UNITS.put(eui, unitId); SENSORS.put(unitId, sensorsReverseMap);
    }


    static {
        ReverseMap<String, Long> sensors = new ReverseMap<>();

        sensors.put("temperature", 340020000L);
        sensors.put("humidity", 410010000L);
        sensors.put("co2", 780020000L);
        sensors.put("rssi", 380010000L);
        sensors.put("snr", 380090000L);
        sensors.put("batteryLevel", 560030000L);

        register("8CF9574000000948", 10002376L, sensors);
    }

    static {
        ReverseMap<String, Long> sensors = new ReverseMap<>();

        sensors.put("temperature", 340020000L);
        sensors.put("humidity", 410010000L);
        sensors.put("co2", 780020000L);
        sensors.put("rssi", 380010000L);
        sensors.put("snr", 380090000L);
        sensors.put("batteryLevel", 560030000L);

        register("8CF95740000008AE", 10002222L, sensors);
    }

    static {
        ReverseMap<String, Long> sensors = new ReverseMap<>();

        sensors.put("vitality", 860010000L);
        sensors.put("rumination", 860020000L);
        sensors.put("pedometer", 860030000L);
        sensors.put("temperature", 340470000L);
        sensors.put("pH", 590030000L);
        sensors.put("ORP", 600030000L);

        register("086BD7D4B501", 20046337L, sensors);
    }


    static {
        ReverseMap<String, Long> sensors = new ReverseMap<>();

        sensors.put("vitality", 860010000L);
        sensors.put("rumination", 860020000L);
        sensors.put("pedometer", 860030000L);
        sensors.put("temperature", 340470000L);
        sensors.put("pH", 590030000L);
        sensors.put("ORP", 600030000L);

        register("90FD9FA5F420", 20062496L, sensors);
    }


    public static Long convertEuiToUnitId(String eui) {
        return UNITS.getValue(eui);
    }

    public static Long convertSensorTypeToSensorId(String eui, String sensorType) {
        if (UNITS.containsKey(eui)) {
            return SENSORS.get(convertEuiToUnitId(eui)).getValue(sensorType);
        }
        return null;
    }

    public static Tuple<Long , Long> convertToSensLog(String eui, String sensorType) {
        if (!UNITS.containsKey(eui)) {
            return null;
        }
        long unitId = UNITS.getValue(eui);

        if (!SENSORS.containsKey(unitId)) {
            return null;
        }
        long sensorId = SENSORS.get(unitId).getValue(sensorType);

        return Tuple.of(unitId, sensorId);
    }

    public static String convertUnitIdToEui(long unitId) {
        return UNITS.getKey(unitId);
    }

    public static String convertSensorIdToSensorType(long unitId, long sensorId) {
        return SENSORS.get(unitId).getKey(sensorId);
    }

    public static Tuple<String, String> convertToIma(long unitId, long sensorId) {
        String eui = convertUnitIdToEui(unitId);
        if (eui == null) {
            return null;
        }

        String sensorType = convertSensorIdToSensorType(unitId, sensorId);
        if (sensorType == null) {
            return null;
        }

        return Tuple.of(eui, sensorType);
    }
}
