import ch.hevs.isi.MinecraftController;
import ch.hevs.isi.core.BooleanDataPoint;
import ch.hevs.isi.core.DataPoint;
import ch.hevs.isi.core.FloatDataPoint;
import ch.hevs.isi.db.DatabaseConnector;
import ch.hevs.isi.web.WebConnector;

/**
 * This class is used to test the web interface.
 * It creates a web server on localhost:8888 and sends data to it.
 *  - Values are sent every 2 seconds
 *  - A variation of the values is simulated (see the code)
 *  - Some values are set to the same value as the remote values (see the code)
 */
public class Web {
    public static void main(String[] args) {
        // set erase on true
        MinecraftController.ERASE_PREVIOUS_DATA_INB_DB = true;

        // create data points for the web
        FloatDataPoint clock = new FloatDataPoint("CLOCK_FLOAT", false);
        FloatDataPoint gridVoltage = new FloatDataPoint("GRID_U_FLOAT", true);

        // initialize the database and the web
        DatabaseConnector.getMySelf().initialize(null);
        WebConnector.getMySelf().initialize("localhost", 8888);

        // initialize time to 0 and set the value
        float time = 0f;
        clock.setValue(time);



        while (WebConnector.getMySelf().wss != null) {
            try {

                // wait 2 seconds
                Thread.sleep(2000);

                if(WebConnector.getMySelf().wss.getConnections().size() > 0){

                    // Set the REMOTE_FACTORY_SP value to FACTORY_ST
                    FloatDataPoint rfSP = (FloatDataPoint) DataPoint.getDataPointOnListFromLabel("REMOTE_FACTORY_SP");
                    if(rfSP != null) new FloatDataPoint("FACTORY_ST", true).setValue(rfSP.getValue());

                    // Set the REMOTE_COAL_SP value to COAL_ST
                    FloatDataPoint cfSP = (FloatDataPoint) DataPoint.getDataPointOnListFromLabel("REMOTE_COAL_SP");
                    if(cfSP != null) new FloatDataPoint("COAL_ST", true).setValue(cfSP.getValue());

                    // Set the REMOTE_SOLAR_SP value to SOLAR_ST
                    BooleanDataPoint solar = (BooleanDataPoint) DataPoint.getDataPointOnListFromLabel("REMOTE_SOLAR_SW");
                    if(solar != null) new BooleanDataPoint("SOLAR_CONNECT_ST", true).setValue(solar.getValue());

                    // Set the REMOTE_WIND_SP value to WIND_ST
                    BooleanDataPoint wind = (BooleanDataPoint) DataPoint.getDataPointOnListFromLabel("REMOTE_WIND_SW");
                    if(wind != null) new BooleanDataPoint("WIND_CONNECT_ST", true).setValue(wind.getValue());


                    // increment time and set the value
                    time += 0.1f;
                    clock.setValue(time);

                    // set the grid voltage value with a modulated value
                    gridVoltage.setValue(750 + (100*time)%100);
                }
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }


    }
}
