package ch.hevs.isi.core;

import ch.hevs.isi.db.DatabaseConnector;
import ch.hevs.isi.field.FieldConnector;
import ch.hevs.isi.web.WebConnector;

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

public abstract class DataPoint{


    public static Map<String, DataPoint> dataPointMap = new HashMap<>(); // Map of all DataPoints
    private final String label; // Label of this DataPoint
    private final boolean isOutput; // True if this DataPoint is an output, false if it's an input

    /**
     * Constructor of DataPoint
     * @param label label of this DataPoint
     * @param isOutput true if this DataPoint is an output, false if it's an input
     */
    protected DataPoint(String label, boolean isOutput){
        this.label = label;
        this.isOutput = isOutput;
    }


    /**
     * Update the value of this DataPoint and notify every connector
     * If this is a new value (doesn't exist), add it to the dataPointMap
     * @param isNewValue true if this is a new value, false if it's an update
     */
    protected void update(boolean isNewValue){

        if(isNewValue){
            dataPointMap.put(this.label, this);
        } else {
            dataPointMap.replace(this.label, this);
        }

        // Update every connector
        DataPointListener.listeners.forEach(listener -> listener.onNewValue(this));
    }

    /**
     * Get the DataPoint from the label
     * @param label label of the DataPoint
     * @return the DataPoint if it exists, null otherwise
     */
    public static DataPoint getDataPointOnListFromLabel(String label){
        if( !dataPointMap.containsKey(label) ){
            return null;
        } else {
            return dataPointMap.get(label);
        }

    }
    /**
     * Just get the label of this DataPoint
     * @return the label in a string
     */
    public String getLabel(){
        return this.label;
    }
    public boolean isOutput(){
        return this.isOutput;
    }

}

