package cz.senslog.connector.model.converter;

import cz.senslog.connector.model.analytics.AnalyticsModel;
import cz.senslog.connector.model.api.Converter;
import cz.senslog.connector.model.v1.Observation;
import cz.senslog.connector.model.v1.Record;
import cz.senslog.connector.model.v1.SenslogV1Model;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class Senslog1ModelAnalyticsModelConverter implements Converter<SenslogV1Model, AnalyticsModel> {

    @Override
    public AnalyticsModel convert(SenslogV1Model model) {
        if (model.getObservations() == null || model.getObservations().isEmpty()) {
            return new AnalyticsModel(Collections.emptyList(), model.getFrom(), model.getTo());
        }

        List<cz.senslog.connector.model.analytics.Observation> analyticObservations = new ArrayList<>();
        for (Record obs : model.getObservations()) {
            if (!(obs instanceof Observation)) { continue; }

            Observation observation = (Observation) obs;
            analyticObservations.add(new cz.senslog.connector.model.analytics.Observation(
                    observation.getUnitId(),
                    observation.getSensorId(),
                    observation.getTime(),
                    observation.getValue()
            ));
        }

        return new AnalyticsModel(analyticObservations, model.getFrom(), model.getTo());
    }
}
