package sophena.rcp.editors.results.single;

import java.io.File;
import java.io.InputStream;
import java.nio.file.Files;
import java.util.ArrayList;
import java.util.List;

import org.eclipse.swt.SWT;
import org.eclipse.swt.browser.Browser;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.ui.forms.IManagedForm;
import org.eclipse.ui.forms.editor.FormPage;
import org.eclipse.ui.forms.widgets.FormToolkit;
import org.eclipse.ui.forms.widgets.ScrolledForm;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.google.gson.Gson;

import sophena.model.Consumer;
import sophena.model.Location;
import sophena.rcp.M;
import sophena.rcp.Workspace;
import sophena.rcp.utils.UI;

public class LocationResultPage extends FormPage {

	private ResultEditor editor;
	private FormToolkit toolkit;

	public LocationResultPage(ResultEditor editor) {
		super(editor, "sophena.LocationResultPage.html", M.Locations);
		this.editor = editor;
	}

	@Override
	protected void createFormContent(IManagedForm mform) {
		ScrolledForm form = UI.formHeader(mform, M.Locations);
		toolkit = mform.getToolkit();
		Composite body = UI.formBody(form, toolkit);
		body.setLayout(new FillLayout());
		var browser = new Browser(body, SWT.NONE);
		UI.onLoaded(browser, getUrl(), () -> {
			String json = createModel();
			browser.execute("setData(" + json + ")");
		});
		form.reflow(true);
	}

	private String getUrl() {
		String pageName = "LocationResultPage.html";
		File f = new File(Workspace.dir(), pageName);
		try {
			if (!f.exists()) {
				InputStream is = getClass().getResourceAsStream(pageName);
				Files.copy(is, f.toPath());
			}
			return f.toURI().toURL().toString();
		} catch (Exception e) {
			Logger log = LoggerFactory.getLogger(getClass());
			log.error("Could not get URL to location page", e);
			return "";
		}
	}

	private String createModel() {
		if (editor.project == null)
			return "{}";
		MapData data = new MapData();
		int points = 0;
		double latSum = 0;
		double lngSum = 0;
		for (Consumer c : editor.project.consumers) {
			Location loc = c.location;
			if (loc == null || loc.latitude == null || loc.longitude == null)
				continue;
			points++;
			latSum += loc.latitude;
			lngSum += loc.longitude;
			Marker marker = new Marker();
			marker.title = c.name;
			marker.latlng = new LatLng(loc.latitude, loc.longitude);
			data.markers.add(marker);
		}
		if (points == 0)
			data.center = new LatLng(48.884, 12.583);
		else
			data.center = new LatLng(latSum / points, lngSum / points);
		return new Gson().toJson(data);
	}

	@SuppressWarnings("unused")
	private class MapData {
		LatLng center;
		List<Marker> markers = new ArrayList<>();
	}

	@SuppressWarnings("unused")
	private class Marker {
		String title;
		LatLng latlng;
	}

	@SuppressWarnings("unused")
	private class LatLng {
		double lat;
		double lng;

		LatLng(double lat, double lng) {
			this.lat = lat;
			this.lng = lng;
		}
	}

}
@@ -42,85 +42,8 @@
 e;%0A%0A
-import java.io.File;%0Aimport java.io.InputStream;%0Aimport java.nio.file.Files;%0A
 impo
@@ -204,50 +204,8 @@
 ut;%0A
-import org.eclipse.swt.widgets.Composite;%0A
 impo
@@ -290,164 +290,8 @@
 age;
-%0Aimport org.eclipse.ui.forms.widgets.FormToolkit;%0Aimport org.eclipse.ui.forms.widgets.ScrolledForm;%0Aimport org.slf4j.Logger;%0Aimport org.slf4j.LoggerFactory;
 %0A%0Aim
@@ -547,38 +547,8 @@
 tor;
-%0A%09private FormToolkit toolkit;
 %0A%0A%09p
@@ -761,20 +761,11 @@
 %7B%0A%09%09
-ScrolledForm
+var
  for
@@ -805,23 +805,22 @@
 ons);%0A%09%09
-toolkit
+var tk
  = mform
@@ -840,17 +840,11 @@
 ;%0A%09%09
-Composite
+var
  bod
@@ -866,22 +866,17 @@
 (form, t
-oolkit
+k
 );%0A%09%09bod
@@ -957,598 +957,271 @@
 ;%0A%09%09
-UI.onLoaded(browser, getUrl(), () -%3E %7B%0A%09%09%09String json = createModel();%0A%09%09%09browser.execute(%22setData(%22 + json + %22)%22);%0A%09%09%7D);%0A%09%09form.reflow(true);%0A%09%7D%0A%0A%09private String getUrl() %7B%0A%09%09String pageName = %22LocationResultPage.html%22;%0A%09%09File f = new File(Workspace.dir(), pageName);%0A%09%09try %7B%0A%09%09%09if (!f.exists()) %7B%0A%09%09%09%09InputStream is = getClass().getResourceAsStream(pageName);%0A%09%09%09%09Files.copy(is, f.toPath());%0A%09%09%09%7D%0A%09%09%09return f.toURI().toURL().toString();%0A%09%09%7D catch (Exception e) %7B%0A%09%09%09Logger log = LoggerFactory.getLogger(getClass
+var url = Workspace.html(%22LocationResultPage%22,%0A%09%09%09%09() -%3E getClass().getResourceAsStream(%0A%09%09%09%09%09%09%22LocationResultPage.html%22));%0A%09%09UI.onLoaded(browser, url, () -%3E %7B%0A%09%09%09String json = createModel
 ()
-)
 ;%0A%09%09%09
-log.error(%22Could not get URL to location page%22, e);%0A%09%09%09return %22%22;%0A%09%09%7D
+browser.execute(%22setData(%22 + json + %22)%22);%0A%09%09%7D);%0A%09%09form.reflow(true);
 %0A%09%7D%0A

use Workspace.html