package com.rera.mss.measurement.inputDataParser

import com.rera.mss.measurement.data.MssMeasurementData
import com.rera.mss.payload.MssMeasurementDto
import org.springframework.stereotype.Component

@Component
class DataParserServiceImpl : DataParserService {
    override fun parseInstrumentMeasurement(rawData: MssMeasurementDto): MssMeasurementData {
        val splited = rawData.data.split(";")
        val keyValuePairs = mutableMapOf<String, Float>()
        splited.forEachIndexed { index, entry: String ->
            if (index == 0)
                return@forEachIndexed
            val pair = entry.split(":")
            if (pair.size != 2) {
                throw IllegalArgumentException("Could not parse key - value pairs of $pair")
            }
            keyValuePairs[pair[0]] = pair[1].toFloat()
        }
        return MssMeasurementData(
                guid = rawData.coreid,
                measurements = keyValuePairs
        )
    }
}