/*
 * Copyright 2013 Charles University in Prague
 * Copyright 2013 Vojtech Horky
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package cz.cuni.mff.d3s.spl.demo.imagequality.html;

import java.io.BufferedOutputStream;
import java.io.IOException;
import java.io.OutputStream;

import com.sun.net.httpserver.HttpExchange;
import com.sun.net.httpserver.HttpHandler;

import cz.cuni.mff.d3s.spl.core.Formula;
import cz.cuni.mff.d3s.spl.core.Result;
import cz.cuni.mff.d3s.spl.demo.imagequality.DataToVisualize;
import cz.cuni.mff.d3s.spl.demo.imagequality.Graph;
import cz.cuni.mff.d3s.spl.formula.SimpleFormulas;
import cz.cuni.mff.d3s.spl.stock.SystemLoad;
import de.erichseifert.gral.data.DataTable;

public class GraphHandler implements HttpHandler {
	private Formula machineIdle;
	private Formula smallLoad;
	
	public GraphHandler() {
		cz.cuni.mff.d3s.spl.core.Data load = SystemLoad.INSTANCE;
		machineIdle = SimpleFormulas.createSmallerThanConst("load", 0.2);
		machineIdle.bind("load", load);
		smallLoad = SimpleFormulas.createSmallerThanConst("load", 0.6);
		smallLoad.bind("load", load);
	}
	
	@Override
	public void handle(HttpExchange exchange) throws IOException {
		DataTable values = null;
		if (machineIdle.evaluate() == Result.TRUE) {
			values = DataToVisualize.getHourly();
		} else if (smallLoad.evaluate() == Result.TRUE) {
			values = DataToVisualize.getDaily();
		} else {
			values = DataToVisualize.getWeekly();
		}
		
		Graph graph = new Graph(values);
		graph.render();
		
		exchange.sendResponseHeaders(200, 0);
		
		OutputStream outputStream = new BufferedOutputStream(exchange.getResponseBody());
		graph.writeAsPng(outputStream, 800, 600);
		
		exchange.close();
	}

}

@@ -1261,16 +1261,61 @@
 ndler %7B%0A
+%09private static boolean beAdaptive = true;%0A%09%0A
 %09private
@@ -1758,16 +1758,37 @@
 = null;%0A
+%09%09if (beAdaptive) %7B%0A%09
 %09%09if (ma
@@ -1821,32 +1821,33 @@
 sult.TRUE) %7B%0A%09%09%09
+%09
 values = DataToV
@@ -1864,24 +1864,25 @@
 etHourly();%0A
+%09
 %09%09%7D else if 
@@ -1921,16 +1921,17 @@
 TRUE) %7B%0A
+%09
 %09%09%09value
@@ -1964,16 +1964,17 @@
 ly();%0A%09%09
+%09
 %7D else %7B
@@ -1974,16 +1974,17 @@
  else %7B%0A
+%09
 %09%09%09value
@@ -2016,16 +2016,73 @@
 ekly();%0A
+%09%09%09%7D%0A%09%09%7D else %7B%0A%09%09%09values = DataToVisualize.getHourly();%0A
 %09%09%7D%0A%09%09%0A%09

allow switching the adaptivity off