package org.genericsystem.carcolor;

import org.genericsystem.api.core.ApiStatics;
import org.genericsystem.carcolor.model.Car;
import org.genericsystem.carcolor.model.CarColor;
import org.genericsystem.carcolor.model.Color;
import org.genericsystem.carcolor.model.Power;
import org.genericsystem.common.AbstractRoot;
import org.genericsystem.common.Generic;
import org.genericsystem.common.Statics;
import org.genericsystem.kernel.Engine;
import org.genericsystem.reactor.appserver.ApplicationServer;
import org.genericsystem.reactor.appserver.ApplicationsDeploymentConfig;
import org.genericsystem.reactor.flex.FlexDirection;
import org.genericsystem.reactor.flex.FlexEditor;
import org.genericsystem.reactor.flex.FlexSection;
import org.genericsystem.reactor.flex.FlexTable;
import org.genericsystem.reactor.html.HtmlApp;
import org.genericsystem.reactor.model.EngineModel;
import org.genericsystem.reactor.model.GenericModel;
import org.genericsystem.reactor.model.GenericModel.StringExtractor;
import org.genericsystem.reactor.model.InputGenericModel;
import org.genericsystem.reactor.model.SelectorModel;

import io.vertx.core.http.ServerWebSocket;

public class CarColorApp extends HtmlApp<EngineModel> {
	private static final String MAIN_COLOR = "#3393ff";

	public static void main(String[] args) {
		ApplicationsDeploymentConfig appsConfig = new ApplicationsDeploymentConfig(Statics.DEFAULT_HOST, args.length != 0 ? Integer.parseInt(args[0]) : Statics.DEFAULT_PORT);
		appsConfig.addApplication("/", CarColorApp.class, EngineModel.class, Engine.class, System.getenv("HOME") + "/genericsystem/cars/", Car.class, Power.class, Color.class, CarColor.class);
		new ApplicationServer(appsConfig).start();

	}

	public CarColorApp(AbstractRoot engine, ServerWebSocket webSocket) {
		super(webSocket);
		runScript(engine);
		new FlexSection<GenericModel>(this, FlexDirection.COLUMN) {
			{
				addStyle("justify-content", "center");
				new H1FlexElement(this, "Reactive System Live Demo").addStyle("background-color", MAIN_COLOR);
				new FlexSection<SelectorModel>(this, FlexDirection.COLUMN) {
					{
						select(StringExtractor.SIMPLE_CLASS_EXTRACTOR, gs -> gs[0], SelectorModel::new);
						new FlexTable(this).select(StringExtractor.MANAGEMENT, Car.class, InputGenericModel::new);
						new FlexEditor(this, FlexDirection.COLUMN).select_(SelectorModel::getSelection);
						new FlexTable(this).select(StringExtractor.MANAGEMENT, Color.class, InputGenericModel::new);
					}
				};
				new SaveCancelFlexRow(this).addStyle("background-color", MAIN_COLOR);
			}
		};
	}

	void runScript(AbstractRoot engine) {
		Generic car = engine.find(Car.class);
		Generic power = engine.find(Power.class);
		Generic carColor = engine.find(CarColor.class);
		Generic color = engine.find(Color.class);
		Generic red = color.setInstance("Red");
		Generic black = color.setInstance("Black");
		Generic green = color.setInstance("Green");
		color.setInstance("Blue");
		color.setInstance("Orange");
		color.setInstance("White");
		color.setInstance("Yellow");
		Generic audiS4 = car.setInstance("Audi S4");
		audiS4.setHolder(power, 333);
		audiS4.setLink(carColor, "Audi S4 Green", green);
		Generic bmwM3 = car.setInstance("BMW M3");
		bmwM3.setHolder(power, 450);
		bmwM3.setLink(carColor, "BMW M3 Red", red);
		Generic ferrariF40 = car.setInstance("Ferrari F40");
		ferrariF40.setHolder(power, 478);
		ferrariF40.setLink(carColor, "Ferrari F40 red", red);
		Generic miniCooper = car.setInstance("Mini Cooper");
		miniCooper.setHolder(power, 175);
		miniCooper.setLink(carColor, "Mini Cooper", black);
		car.setInstance("Audi A4 3.0 TDI").setHolder(power, 233);
		car.setInstance("Peugeot 106 GTI").setHolder(power, 120);
		car.setInstance("Peugeot 206 S16").setHolder(power, 136);
		power.enableRequiredConstraint(ApiStatics.BASE_POSITION);
		engine.getCurrentCache().flush();
	}

}
@@ -30,16 +30,60 @@
 color;%0A%0A
+import io.vertx.core.http.ServerWebSocket;%0A%0A
 import o
@@ -112,32 +112,32 @@
 ore.ApiStatics;%0A
-
 import org.gener
@@ -1040,66 +1040,8 @@
 or;%0A
-import org.genericsystem.reactor.model.InputGenericModel;%0A
 impo
@@ -1095,52 +1095,8 @@
 l;%0A%0A
-import io.vertx.core.http.ServerWebSocket;%0A%0A
 publ
@@ -1386,17 +1386,21 @@
 args%5B0%5D)
- 
+%0A%09%09%09%09
 : Static
@@ -1559,17 +1559,21 @@
 r.class,
- 
+%0A%09%09%09%09
 Power.cl
@@ -2200,37 +2200,32 @@
 ENT, Car.class, 
-Input
 GenericModel::ne
@@ -2315,16 +2315,16 @@
 ction);%0A
+
 %09%09%09%09%09%09ne
@@ -2393,13 +2393,8 @@
 ss, 
-Input
 Gene

fix compilation error