package modele;

import javafx.beans.property.SimpleStringProperty;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class VirtualSensor extends Sensor {
    private List<ConcreteSensor> sensors  =new ArrayList<>();
    private Map<ConcreteSensor, Double> weights = new HashMap<>();

    public VirtualSensor(List<Sensor> sensorsList, int id, String name, int weight) {
        this.name = new SimpleStringProperty(name);
        this.id = id;
        this.weight = weight;
    }

    public void addSensor(ConcreteSensor sensor, double weight) {
        sensors.add(sensor);
        weights.put(sensor, weight);
        sensor.getTempProperty().addListener((observable, oldValue, newValue) -> updateTemperature());
    }

    public void updateTemperature() {
        double tempSum = 0;
        double weightSum = 0;
        for (ConcreteSensor sensor : sensors) {
            tempSum += sensor.getTemp() * weights.get(sensor);
            weightSum += weights.get(sensor);
        }
        setTemp(tempSum / weightSum);
    }

    @Override
    public double generateTemperature() {
        double value = 0;
        double count = 0;
        for (Sensor sensor:
             sensors) {
            value += sensor.getTemp();
            count += 1;
        }
        value = value/count;
        return value;
    }
}

