import { Watch } from 'vue-property-decorator';
import { WorkflowSettings } from 'dasf-web/lib/model/UserSetting';
import DateRange from 'dasf-web/lib/dateSelection/daterangepicker/DateRange';
import { EsgfDataset } from 'dasf-web/lib/dataDiscovery/EsgfDataArchive'
import { HighPrecipEventTimeseries } from './HighPrecipEvent';

import Geometry from 'ol/geom/Geometry';
import WKT from 'ol/format/WKT';
import { DataArchive, QueryProperty, HasDataArchive, Dataset, DatasetGroup, Ensemble, EnsembleItem } from 'dasf-web/lib/dataDiscovery/DataArchive';
import _ from 'lodash';

export class ChangeWorkflowSettings extends WorkflowSettings implements HasDataArchive {

	public static readonly DATA_ARCHIVE_UPDATED: string = "data_archive_updated";
	public static readonly DATA_ARCHIVE_QUERY_PROPS_UPDATED: string = "data_archive_query_props_updated";
	public static readonly REFERENCE_DATASET_GROUP_UPDATED: string = "references_dataset_group_updated";
	public static readonly COMPARE_DATASET_GROUP_UPDATED: string = "compare_dataset_group_updated";
	public static readonly ENSEMBLES_UPDATED: string = "ensembles_updated";
	public static readonly ROI_UPDATED: string = "roi_updated";
	public static readonly EVENT_TIMESERIES_UPDATED: string = "event_ts_updated";

	private _dateRange: DateRange = new DateRange();
	/** region of interest [groupName, layerName, geometry] */
	// roiMapProj: Geometry;
	public roiWgs84: Geometry = undefined;

	private _eventTimeseries!: HighPrecipEventTimeseries;

	private _dataArchive!: DataArchive;
	private _dataArchiveQueryProps: string[] = [];
	private _referenceDatasets!: DatasetGroup;
	private _compareDatasets!: DatasetGroup;

	// [0]=> reference/historical and [1] => compare/rcp
	private datasetPresets: Map<string, EsgfDataset[]> = new Map();
	public _ensembles: Ensemble[] = [];

	public constructor() {
		super();

		this.init();
	}

	private init(): void {
		this.datasetPresets.set('mini', [
			// historical
			new EsgfDataset("cordex.output.EUR-11.MPI-CSC.MPI-M-MPI-ESM-LR.historical.r1i1p1.REMO2009.v1.day.pr", "http://esgf-data.dkrz.de/esg-search/search?type=File&limit=1000&format=application%2Fsolr%2Bjson&dataset_id=cordex.output.EUR-11.MPI-CSC.MPI-M-MPI-ESM-LR.historical.r1i1p1.REMO2009.v1.day.pr.v20160419|esgf1.dkrz.de"),
			new EsgfDataset("cordex-reklies.output.EUR-11.GERICS.ICHEC-EC-EARTH.historical.r12i1p1.REMO2015.v1.day.pr", "http://esgf-data.dkrz.de/esg-search/search?type=File&limit=1000&format=application%2Fsolr%2Bjson&dataset_id=cordex-reklies.output.EUR-11.GERICS.ICHEC-EC-EARTH.historical.r12i1p1.REMO2015.v1.day.pr.v20170410|esgf1.dkrz.de"),
			// rcp26
			new EsgfDataset("cordex.output.EUR-11.MPI-CSC.MPI-M-MPI-ESM-LR.rcp26.r1i1p1.REMO2009.v1.day.pr", "http://esgf-data.dkrz.de/esg-search/search?type=File&limit=1000&format=application%2Fsolr%2Bjson&dataset_id=cordex.output.EUR-11.MPI-CSC.MPI-M-MPI-ESM-LR.rcp26.r1i1p1.REMO2009.v1.day.pr.v20160525|esgf1.dkrz.de"),
			new EsgfDataset("cordex.output.EUR-11.GERICS.ICHEC-EC-EARTH.rcp26.r12i1p1.REMO2015.v1.day.pr", "http://esgf-data.dkrz.de/esg-search/search?type=File&limit=1000&format=application%2Fsolr%2Bjson&dataset_id=cordex.output.EUR-11.GERICS.ICHEC-EC-EARTH.rcp26.r12i1p1.REMO2015.v1.day.pr.v20180710|esgf1.dkrz.de"),
			// rcp45
			new EsgfDataset("cordex.output.EUR-11.MPI-CSC.MPI-M-MPI-ESM-LR.rcp45.r1i1p1.REMO2009.v1.day.pr", "http://esgf-data.dkrz.de/esg-search/search?type=File&limit=1000&format=application%2Fsolr%2Bjson&dataset_id=cordex.output.EUR-11.MPI-CSC.MPI-M-MPI-ESM-LR.rcp45.r1i1p1.REMO2009.v1.day.pr.v20160525|esgf1.dkrz.de"),
			new EsgfDataset("cordex.output.EUR-11.GERICS.ICHEC-EC-EARTH.rcp45.r12i1p1.REMO2015.v1.day.pr", "http://esgf-data.dkrz.de/esg-search/search?type=File&limit=1000&format=application%2Fsolr%2Bjson&dataset_id=cordex.output.EUR-11.GERICS.ICHEC-EC-EARTH.rcp45.r12i1p1.REMO2015.v1.day.pr.v20210128|esgf1.dkrz.de"),
			// rcp85
			new EsgfDataset("cordex.output.EUR-11.MPI-CSC.MPI-M-MPI-ESM-LR.rcp85.r1i1p1.REMO2009.v1.day.pr", "http://esgf-data.dkrz.de/esg-search/search?type=File&limit=1000&format=application%2Fsolr%2Bjson&dataset_id=cordex.output.EUR-11.MPI-CSC.MPI-M-MPI-ESM-LR.rcp85.r1i1p1.REMO2009.v1.day.pr.v20160525|esgf1.dkrz.de"),
			new EsgfDataset("cordex-reklies.output.EUR-11.GERICS.ICHEC-EC-EARTH.rcp85.r12i1p1.REMO2015.v1.day.pr", "http://esgf-data.dkrz.de/esg-search/search?type=File&limit=1000&format=application%2Fsolr%2Bjson&dataset_id=cordex-reklies.output.EUR-11.GERICS.ICHEC-EC-EARTH.rcp85.r12i1p1.REMO2015.v1.day.pr.v20170410|esgf1.dkrz.de"),
		]);

		this.datasetPresets.set('default', [
			// historical
			new EsgfDataset("cordex.output.EUR-11.MPI-CSC.MPI-M-MPI-ESM-LR.historical.r1i1p1.REMO2009.v1.day.pr", "http://esgf-data.dkrz.de/esg-search/search?type=File&limit=1000&format=application%2Fsolr%2Bjson&dataset_id=cordex.output.EUR-11.MPI-CSC.MPI-M-MPI-ESM-LR.historical.r1i1p1.REMO2009.v1.day.pr.v20160419|esgf1.dkrz.de"),
			new EsgfDataset("cordex-reklies.output.EUR-11.GERICS.ICHEC-EC-EARTH.historical.r12i1p1.REMO2015.v1.day.pr", "http://esgf-data.dkrz.de/esg-search/search?type=File&limit=1000&format=application%2Fsolr%2Bjson&dataset_id=cordex-reklies.output.EUR-11.GERICS.ICHEC-EC-EARTH.historical.r12i1p1.REMO2015.v1.day.pr.v20170410|esgf1.dkrz.de"),
			new EsgfDataset("cordex.output.EUR-11.KNMI.ICHEC-EC-EARTH.historical.r12i1p1.RACMO22E.v1.day.pr", "http://esgf-data.dkrz.de/esg-search/search?type=File&limit=1000&format=application%2Fsolr%2Bjson&dataset_id=cordex.output.EUR-11.KNMI.ICHEC-EC-EARTH.historical.r12i1p1.RACMO22E.v1.day.pr.v20170208|esgf1.dkrz.de"),
			new EsgfDataset("cordex.output.EUR-11.CNRM.CNRM-CERFACS-CNRM-CM5.historical.r1i1p1.ALADIN63.v2.day.pr", "http://esgf-data.dkrz.de/esg-search/search?type=File&limit=1000&format=application%2Fsolr%2Bjson&dataset_id=cordex.output.EUR-11.CNRM.CNRM-CERFACS-CNRM-CM5.historical.r1i1p1.ALADIN63.v2.day.pr.v20190510|esg1.umr-cnrm.fr"),
			new EsgfDataset("cordex.output.EUR-11.CLMcom.MPI-M-MPI-ESM-LR.historical.r1i1p1.CCLM4-8-17.v1.day.pr", "http://esgf-data.dkrz.de/esg-search/search?type=File&limit=1000&format=application%2Fsolr%2Bjson&dataset_id=cordex.output.EUR-11.CLMcom.MPI-M-MPI-ESM-LR.historical.r1i1p1.CCLM4-8-17.v1.day.pr.v20140515|esgf1.dkrz.de"),
			// rcp26
			new EsgfDataset("cordex.output.EUR-11.MPI-CSC.MPI-M-MPI-ESM-LR.rcp26.r1i1p1.REMO2009.v1.day.pr", "http://esgf-data.dkrz.de/esg-search/search?type=File&limit=1000&format=application%2Fsolr%2Bjson&dataset_id=cordex.output.EUR-11.MPI-CSC.MPI-M-MPI-ESM-LR.rcp26.r1i1p1.REMO2009.v1.day.pr.v20160525|esgf1.dkrz.de"),
			new EsgfDataset("cordex.output.EUR-11.GERICS.ICHEC-EC-EARTH.rcp26.r12i1p1.REMO2015.v1.day.pr", "http://esgf-data.dkrz.de/esg-search/search?type=File&limit=1000&format=application%2Fsolr%2Bjson&dataset_id=cordex.output.EUR-11.GERICS.ICHEC-EC-EARTH.rcp26.r12i1p1.REMO2015.v1.day.pr.v20180710|esgf1.dkrz.de"),
			new EsgfDataset("cordex.output.EUR-11.KNMI.ICHEC-EC-EARTH.rcp26.r12i1p1.RACMO22E.v1.day.pr", "http://esgf-data.dkrz.de/esg-search/search?type=File&limit=1000&format=application%2Fsolr%2Bjson&dataset_id=cordex.output.EUR-11.KNMI.ICHEC-EC-EARTH.rcp26.r12i1p1.RACMO22E.v1.day.pr.v20170206|esgf1.dkrz.de"),
			new EsgfDataset("cordex.output.EUR-11.CNRM.CNRM-CERFACS-CNRM-CM5.rcp26.r1i1p1.ALADIN63.v2.day.pr", "http://esgf-data.dkrz.de/esg-search/search?type=File&limit=1000&format=application%2Fsolr%2Bjson&dataset_id=cordex.output.EUR-11.CNRM.CNRM-CERFACS-CNRM-CM5.rcp26.r1i1p1.ALADIN63.v2.day.pr.v20190702|esg1.umr-cnrm.fr"),
			new EsgfDataset("cordex-reklies.output.EUR-11.CLMcom-BTU.MPI-M-MPI-ESM-LR.rcp26.r1i1p1.CCLM4-8-17.v1.day.pr", "http://esgf-data.dkrz.de/esg-search/search?type=File&limit=1000&format=application%2Fsolr%2Bjson&dataset_id=cordex-reklies.output.EUR-11.CLMcom-BTU.MPI-M-MPI-ESM-LR.rcp26.r1i1p1.CCLM4-8-17.v1.day.pr.v20180212|esgf1.dkrz.de"),
			// rcp45
			new EsgfDataset("cordex.output.EUR-11.MPI-CSC.MPI-M-MPI-ESM-LR.rcp45.r1i1p1.REMO2009.v1.day.pr", "http://esgf-data.dkrz.de/esg-search/search?type=File&limit=1000&format=application%2Fsolr%2Bjson&dataset_id=cordex.output.EUR-11.MPI-CSC.MPI-M-MPI-ESM-LR.rcp45.r1i1p1.REMO2009.v1.day.pr.v20160525|esgf1.dkrz.de"),
			new EsgfDataset("cordex.output.EUR-11.GERICS.ICHEC-EC-EARTH.rcp45.r12i1p1.REMO2015.v1.day.pr", "http://esgf-data.dkrz.de/esg-search/search?type=File&limit=1000&format=application%2Fsolr%2Bjson&dataset_id=cordex.output.EUR-11.GERICS.ICHEC-EC-EARTH.rcp45.r12i1p1.REMO2015.v1.day.pr.v20210128|esgf1.dkrz.de"),
			new EsgfDataset("cordex.output.EUR-11.KNMI.ICHEC-EC-EARTH.rcp45.r12i1p1.RACMO22E.v1.day.pr", "http://esgf-data.dkrz.de/esg-search/search?type=File&limit=1000&format=application%2Fsolr%2Bjson&dataset_id=cordex.output.EUR-11.KNMI.ICHEC-EC-EARTH.rcp45.r12i1p1.RACMO22E.v1.day.pr.v20170713|esgf1.dkrz.de"),
			new EsgfDataset("cordex.output.EUR-11.CNRM.CNRM-CERFACS-CNRM-CM5.rcp45.r1i1p1.ALADIN63.v2.day.pr", "http://esgf-data.dkrz.de/esg-search/search?type=File&limit=1000&format=application%2Fsolr%2Bjson&dataset_id=cordex.output.EUR-11.CNRM.CNRM-CERFACS-CNRM-CM5.rcp45.r1i1p1.ALADIN63.v2.day.pr.v20190828|esg1.umr-cnrm.fr"),
			new EsgfDataset("cordex.output.EUR-11.CLMcom.MPI-M-MPI-ESM-LR.rcp45.r1i1p1.CCLM4-8-17.v1.day.pr", "http://esgf-data.dkrz.de/esg-search/search?type=File&limit=1000&format=application%2Fsolr%2Bjson&dataset_id=cordex.output.EUR-11.CLMcom.MPI-M-MPI-ESM-LR.rcp45.r1i1p1.CCLM4-8-17.v1.day.pr.v20140515|esgf1.dkrz.de"),
			// rcp85
			new EsgfDataset("cordex.output.EUR-11.MPI-CSC.MPI-M-MPI-ESM-LR.rcp85.r1i1p1.REMO2009.v1.day.pr", "http://esgf-data.dkrz.de/esg-search/search?type=File&limit=1000&format=application%2Fsolr%2Bjson&dataset_id=cordex.output.EUR-11.MPI-CSC.MPI-M-MPI-ESM-LR.rcp85.r1i1p1.REMO2009.v1.day.pr.v20160525|esgf1.dkrz.de"),
			new EsgfDataset("cordex-reklies.output.EUR-11.GERICS.ICHEC-EC-EARTH.rcp85.r12i1p1.REMO2015.v1.day.pr", "http://esgf-data.dkrz.de/esg-search/search?type=File&limit=1000&format=application%2Fsolr%2Bjson&dataset_id=cordex-reklies.output.EUR-11.GERICS.ICHEC-EC-EARTH.rcp85.r12i1p1.REMO2015.v1.day.pr.v20170410|esgf1.dkrz.de"),
			new EsgfDataset("cordex.output.EUR-11.KNMI.ICHEC-EC-EARTH.rcp85.r12i1p1.RACMO22E.v1.day.pr", "http://esgf-data.dkrz.de/esg-search/search?type=File&limit=1000&format=application%2Fsolr%2Bjson&dataset_id=cordex.output.EUR-11.KNMI.ICHEC-EC-EARTH.rcp85.r12i1p1.RACMO22E.v1.day.pr.v20170606|esgf1.dkrz.de"),
			new EsgfDataset("cordex.output.EUR-11.CNRM.CNRM-CERFACS-CNRM-CM5.rcp85.r1i1p1.ALADIN63.v2.day.pr", "http://esgf-data.dkrz.de/esg-search/search?type=File&limit=1000&format=application%2Fsolr%2Bjson&dataset_id=cordex.output.EUR-11.CNRM.CNRM-CERFACS-CNRM-CM5.rcp85.r1i1p1.ALADIN63.v2.day.pr.v20190419|esg1.umr-cnrm.fr"),
			new EsgfDataset("cordex.output.EUR-11.CLMcom.MPI-M-MPI-ESM-LR.rcp85.r1i1p1.CCLM4-8-17.v1.day.pr", "http://esgf-data.dkrz.de/esg-search/search?type=File&limit=1000&format=application%2Fsolr%2Bjson&dataset_id=cordex.output.EUR-11.CLMcom.MPI-M-MPI-ESM-LR.rcp85.r1i1p1.CCLM4-8-17.v1.day.pr.v20140515|esgf1.dkrz.de"),
		]);


		this.datasetPresets.set('big', [
			// historical
			new EsgfDataset("cordex.output.EUR-11.MPI-CSC.MPI-M-MPI-ESM-LR.historical.r1i1p1.REMO2009.v1.day.pr", "http://esgf-data.dkrz.de/esg-search/search?type=File&limit=1000&format=application%2Fsolr%2Bjson&dataset_id=cordex.output.EUR-11.MPI-CSC.MPI-M-MPI-ESM-LR.historical.r1i1p1.REMO2009.v1.day.pr.v20160419|esgf1.dkrz.de"),
			new EsgfDataset("cordex-reklies.output.EUR-11.GERICS.ICHEC-EC-EARTH.historical.r12i1p1.REMO2015.v1.day.pr", "http://esgf-data.dkrz.de/esg-search/search?type=File&limit=1000&format=application%2Fsolr%2Bjson&dataset_id=cordex-reklies.output.EUR-11.GERICS.ICHEC-EC-EARTH.historical.r12i1p1.REMO2015.v1.day.pr.v20170410|esgf1.dkrz.de"),
			new EsgfDataset("cordex.output.EUR-11.KNMI.ICHEC-EC-EARTH.historical.r12i1p1.RACMO22E.v1.day.pr", "http://esgf-data.dkrz.de/esg-search/search?type=File&limit=1000&format=application%2Fsolr%2Bjson&dataset_id=cordex.output.EUR-11.KNMI.ICHEC-EC-EARTH.historical.r12i1p1.RACMO22E.v1.day.pr.v20170208|esgf1.dkrz.de"),
			new EsgfDataset("cordex.output.EUR-11.CNRM.CNRM-CERFACS-CNRM-CM5.historical.r1i1p1.ALADIN63.v2.day.pr", "http://esgf-data.dkrz.de/esg-search/search?type=File&limit=1000&format=application%2Fsolr%2Bjson&dataset_id=cordex.output.EUR-11.CNRM.CNRM-CERFACS-CNRM-CM5.historical.r1i1p1.ALADIN63.v2.day.pr.v20190510|esg1.umr-cnrm.fr"),
			new EsgfDataset("cordex.output.EUR-11.CLMcom.MPI-M-MPI-ESM-LR.historical.r1i1p1.CCLM4-8-17.v1.day.pr", "http://esgf-data.dkrz.de/esg-search/search?type=File&limit=1000&format=application%2Fsolr%2Bjson&dataset_id=cordex.output.EUR-11.CLMcom.MPI-M-MPI-ESM-LR.historical.r1i1p1.CCLM4-8-17.v1.day.pr.v20140515|esgf1.dkrz.de"),
			new EsgfDataset("cordex-reklies.output.EUR-11.GERICS.MOHC-HadGEM2-ES.historical.r1i1p1.REMO2015.v1.day.pr", "http://esgf-data.dkrz.de/esg-search/search?type=File&limit=1000&format=application%2Fsolr%2Bjson&dataset_id=cordex-reklies.output.EUR-11.GERICS.MOHC-HadGEM2-ES.historical.r1i1p1.REMO2015.v1.day.pr.v20170412|esgf1.dkrz.de"),
			new EsgfDataset("cordex.output.EUR-11.KNMI.MOHC-HadGEM2-ES.historical.r1i1p1.RACMO22E.v2.day.pr", "http://esgf-data.dkrz.de/esg-search/search?type=File&limit=1000&format=application%2Fsolr%2Bjson&dataset_id=cordex.output.EUR-11.KNMI.MOHC-HadGEM2-ES.historical.r1i1p1.RACMO22E.v2.day.pr.v20160620|esgf1.dkrz.de"),
			// rcp26
			new EsgfDataset("cordex.output.EUR-11.MPI-CSC.MPI-M-MPI-ESM-LR.rcp26.r1i1p1.REMO2009.v1.day.pr", "http://esgf-data.dkrz.de/esg-search/search?type=File&limit=1000&format=application%2Fsolr%2Bjson&dataset_id=cordex.output.EUR-11.MPI-CSC.MPI-M-MPI-ESM-LR.rcp26.r1i1p1.REMO2009.v1.day.pr.v20160525|esgf1.dkrz.de"),
			new EsgfDataset("cordex.output.EUR-11.GERICS.ICHEC-EC-EARTH.rcp26.r12i1p1.REMO2015.v1.day.pr", "http://esgf-data.dkrz.de/esg-search/search?type=File&limit=1000&format=application%2Fsolr%2Bjson&dataset_id=cordex.output.EUR-11.GERICS.ICHEC-EC-EARTH.rcp26.r12i1p1.REMO2015.v1.day.pr.v20180710|esgf1.dkrz.de"),
			new EsgfDataset("cordex.output.EUR-11.KNMI.ICHEC-EC-EARTH.rcp26.r12i1p1.RACMO22E.v1.day.pr", "http://esgf-data.dkrz.de/esg-search/search?type=File&limit=1000&format=application%2Fsolr%2Bjson&dataset_id=cordex.output.EUR-11.KNMI.ICHEC-EC-EARTH.rcp26.r12i1p1.RACMO22E.v1.day.pr.v20170206|esgf1.dkrz.de"),
			new EsgfDataset("cordex.output.EUR-11.CNRM.CNRM-CERFACS-CNRM-CM5.rcp26.r1i1p1.ALADIN63.v2.day.pr", "http://esgf-data.dkrz.de/esg-search/search?type=File&limit=1000&format=application%2Fsolr%2Bjson&dataset_id=cordex.output.EUR-11.CNRM.CNRM-CERFACS-CNRM-CM5.rcp26.r1i1p1.ALADIN63.v2.day.pr.v20190702|esg1.umr-cnrm.fr"),
			new EsgfDataset("cordex-reklies.output.EUR-11.CLMcom-BTU.MPI-M-MPI-ESM-LR.rcp26.r1i1p1.CCLM4-8-17.v1.day.pr", "http://esgf-data.dkrz.de/esg-search/search?type=File&limit=1000&format=application%2Fsolr%2Bjson&dataset_id=cordex-reklies.output.EUR-11.CLMcom-BTU.MPI-M-MPI-ESM-LR.rcp26.r1i1p1.CCLM4-8-17.v1.day.pr.v20180212|esgf1.dkrz.de"),
			new EsgfDataset("cordex.output.EUR-11.GERICS.MOHC-HadGEM2-ES.rcp26.r1i1p1.REMO2015.v1.day.pr", "http://esgf-data.dkrz.de/esg-search/search?type=File&limit=1000&format=application%2Fsolr%2Bjson&dataset_id=cordex.output.EUR-11.GERICS.MOHC-HadGEM2-ES.rcp26.r1i1p1.REMO2015.v1.day.pr.v20180710|esgf1.dkrz.de"),
			new EsgfDataset("cordex.output.EUR-11.KNMI.MOHC-HadGEM2-ES.rcp26.r1i1p1.RACMO22E.v2.day.pr", "http://esgf-data.dkrz.de/esg-search/search?type=File&limit=1000&format=application%2Fsolr%2Bjson&dataset_id=cordex.output.EUR-11.KNMI.MOHC-HadGEM2-ES.rcp26.r1i1p1.RACMO22E.v2.day.pr.v20160830|esgf1.dkrz.de"),
			// rcp45
			new EsgfDataset("cordex.output.EUR-11.MPI-CSC.MPI-M-MPI-ESM-LR.rcp45.r1i1p1.REMO2009.v1.day.pr", "http://esgf-data.dkrz.de/esg-search/search?type=File&limit=1000&format=application%2Fsolr%2Bjson&dataset_id=cordex.output.EUR-11.MPI-CSC.MPI-M-MPI-ESM-LR.rcp45.r1i1p1.REMO2009.v1.day.pr.v20160525|esgf1.dkrz.de"),
			new EsgfDataset("cordex.output.EUR-11.GERICS.ICHEC-EC-EARTH.rcp45.r12i1p1.REMO2015.v1.day.pr", "http://esgf-data.dkrz.de/esg-search/search?type=File&limit=1000&format=application%2Fsolr%2Bjson&dataset_id=cordex.output.EUR-11.GERICS.ICHEC-EC-EARTH.rcp45.r12i1p1.REMO2015.v1.day.pr.v20210128|esgf1.dkrz.de"),
			new EsgfDataset("cordex.output.EUR-11.KNMI.ICHEC-EC-EARTH.rcp45.r12i1p1.RACMO22E.v1.day.pr", "http://esgf-data.dkrz.de/esg-search/search?type=File&limit=1000&format=application%2Fsolr%2Bjson&dataset_id=cordex.output.EUR-11.KNMI.ICHEC-EC-EARTH.rcp45.r12i1p1.RACMO22E.v1.day.pr.v20170713|esgf1.dkrz.de"),
			new EsgfDataset("cordex.output.EUR-11.CNRM.CNRM-CERFACS-CNRM-CM5.rcp45.r1i1p1.ALADIN63.v2.day.pr", "http://esgf-data.dkrz.de/esg-search/search?type=File&limit=1000&format=application%2Fsolr%2Bjson&dataset_id=cordex.output.EUR-11.CNRM.CNRM-CERFACS-CNRM-CM5.rcp45.r1i1p1.ALADIN63.v2.day.pr.v20190828|esg1.umr-cnrm.fr"),
			new EsgfDataset("cordex.output.EUR-11.CLMcom.MPI-M-MPI-ESM-LR.rcp45.r1i1p1.CCLM4-8-17.v1.day.pr", "http://esgf-data.dkrz.de/esg-search/search?type=File&limit=1000&format=application%2Fsolr%2Bjson&dataset_id=cordex.output.EUR-11.CLMcom.MPI-M-MPI-ESM-LR.rcp45.r1i1p1.CCLM4-8-17.v1.day.pr.v20140515|esgf1.dkrz.de"),
			new EsgfDataset("cordex.output.EUR-11.GERICS.MOHC-HadGEM2-ES.rcp45.r1i1p1.REMO2015.v1.day.pr", "http://esgf-data.dkrz.de/esg-search/search?type=File&limit=1000&format=application%2Fsolr%2Bjson&dataset_id=cordex.output.EUR-11.GERICS.MOHC-HadGEM2-ES.rcp45.r1i1p1.REMO2015.v1.day.pr.v20200702|esgf1.dkrz.de"),
			new EsgfDataset("cordex.output.EUR-11.KNMI.MOHC-HadGEM2-ES.rcp45.r1i1p1.RACMO22E.v2.day.pr", "http://esgf-data.dkrz.de/esg-search/search?type=File&limit=1000&format=application%2Fsolr%2Bjson&dataset_id=cordex.output.EUR-11.KNMI.MOHC-HadGEM2-ES.rcp45.r1i1p1.RACMO22E.v2.day.pr.v20160705|esgf1.dkrz.de"),
			// rcp85
			new EsgfDataset("cordex.output.EUR-11.MPI-CSC.MPI-M-MPI-ESM-LR.rcp85.r1i1p1.REMO2009.v1.day.pr", "http://esgf-data.dkrz.de/esg-search/search?type=File&limit=1000&format=application%2Fsolr%2Bjson&dataset_id=cordex.output.EUR-11.MPI-CSC.MPI-M-MPI-ESM-LR.rcp85.r1i1p1.REMO2009.v1.day.pr.v20160525|esgf1.dkrz.de"),
			new EsgfDataset("cordex-reklies.output.EUR-11.GERICS.ICHEC-EC-EARTH.rcp85.r12i1p1.REMO2015.v1.day.pr", "http://esgf-data.dkrz.de/esg-search/search?type=File&limit=1000&format=application%2Fsolr%2Bjson&dataset_id=cordex-reklies.output.EUR-11.GERICS.ICHEC-EC-EARTH.rcp85.r12i1p1.REMO2015.v1.day.pr.v20170410|esgf1.dkrz.de"),
			new EsgfDataset("cordex.output.EUR-11.KNMI.ICHEC-EC-EARTH.rcp85.r12i1p1.RACMO22E.v1.day.pr", "http://esgf-data.dkrz.de/esg-search/search?type=File&limit=1000&format=application%2Fsolr%2Bjson&dataset_id=cordex.output.EUR-11.KNMI.ICHEC-EC-EARTH.rcp85.r12i1p1.RACMO22E.v1.day.pr.v20170606|esgf1.dkrz.de"),
			new EsgfDataset("cordex.output.EUR-11.CNRM.CNRM-CERFACS-CNRM-CM5.rcp85.r1i1p1.ALADIN63.v2.day.pr", "http://esgf-data.dkrz.de/esg-search/search?type=File&limit=1000&format=application%2Fsolr%2Bjson&dataset_id=cordex.output.EUR-11.CNRM.CNRM-CERFACS-CNRM-CM5.rcp85.r1i1p1.ALADIN63.v2.day.pr.v20190419|esg1.umr-cnrm.fr"),
			new EsgfDataset("cordex.output.EUR-11.CLMcom.MPI-M-MPI-ESM-LR.rcp85.r1i1p1.CCLM4-8-17.v1.day.pr", "http://esgf-data.dkrz.de/esg-search/search?type=File&limit=1000&format=application%2Fsolr%2Bjson&dataset_id=cordex.output.EUR-11.CLMcom.MPI-M-MPI-ESM-LR.rcp85.r1i1p1.CCLM4-8-17.v1.day.pr.v20140515|esgf1.dkrz.de"),
			new EsgfDataset("cordex-reklies.output.EUR-11.GERICS.MOHC-HadGEM2-ES.rcp85.r1i1p1.REMO2015.v1.day.pr", "http://esgf-data.dkrz.de/esg-search/search?type=File&limit=1000&format=application%2Fsolr%2Bjson&dataset_id=cordex-reklies.output.EUR-11.GERICS.MOHC-HadGEM2-ES.rcp85.r1i1p1.REMO2015.v1.day.pr.v20170412|esgf1.dkrz.de"),
			new EsgfDataset("cordex.output.EUR-11.KNMI.MOHC-HadGEM2-ES.rcp85.r1i1p1.RACMO22E.v2.day.pr", "http://esgf-data.dkrz.de/esg-search/search?type=File&limit=1000&format=application%2Fsolr%2Bjson&dataset_id=cordex.output.EUR-11.KNMI.MOHC-HadGEM2-ES.rcp85.r1i1p1.RACMO22E.v2.day.pr.v20160705|esgf1.dkrz.de"),
		]);

		this.datasetPresets.set('debug', [
			// historical
			new EsgfDataset("cordex-reklies.output.EUR-11.GERICS.MOHC-HadGEM2-ES.historical.r1i1p1.REMO2015.v1.day.pr", "http://esgf-data.dkrz.de/esg-search/search?type=File&limit=1000&format=application%2Fsolr%2Bjson&dataset_id=cordex-reklies.output.EUR-11.GERICS.MOHC-HadGEM2-ES.historical.r1i1p1.REMO2015.v1.day.pr.v20170412|esgf1.dkrz.de"),
			// rcp26
			new EsgfDataset("cordex.output.EUR-11.GERICS.MOHC-HadGEM2-ES.rcp26.r1i1p1.REMO2015.v1.day.pr", "http://esgf-data.dkrz.de/esg-search/search?type=File&limit=1000&format=application%2Fsolr%2Bjson&dataset_id=cordex.output.EUR-11.GERICS.MOHC-HadGEM2-ES.rcp26.r1i1p1.REMO2015.v1.day.pr.v20180710|esgf1.dkrz.de"),
			// rcp45
			new EsgfDataset("cordex.output.EUR-11.GERICS.MOHC-HadGEM2-ES.rcp45.r1i1p1.REMO2015.v1.day.pr", "http://esgf-data.dkrz.de/esg-search/search?type=File&limit=1000&format=application%2Fsolr%2Bjson&dataset_id=cordex.output.EUR-11.GERICS.MOHC-HadGEM2-ES.rcp45.r1i1p1.REMO2015.v1.day.pr.v20200702|esgf1.dkrz.de"),
			// rcp85
			new EsgfDataset("cordex-reklies.output.EUR-11.GERICS.MOHC-HadGEM2-ES.rcp85.r1i1p1.REMO2015.v1.day.pr", "http://esgf-data.dkrz.de/esg-search/search?type=File&limit=1000&format=application%2Fsolr%2Bjson&dataset_id=cordex-reklies.output.EUR-11.GERICS.MOHC-HadGEM2-ES.rcp85.r1i1p1.REMO2015.v1.day.pr.v20170412|esgf1.dkrz.de"),
		]);

		this.createDatasetGroups()
		// this.createFlatUniqueDatasetList()
		// this.transformDatasetPresetsToEnsembleList()
	}

	private createDatasetGroups() {
		// Create Reference DatasetGroup
		this._referenceDatasets = new DatasetGroup("Reference", "darkgreen");
		this._referenceDatasets['averagingIntervals'] = [];
		this._referenceDatasets['averagingIntervals'].push(new DateRange('1960-1-1', '1990-12-31'));
		this._referenceDatasets.dateRange = new DateRange(EsgfDataset.DEFAULT_HIST_DATES[0], EsgfDataset.DEFAULT_HIST_DATES[1]);
		this._referenceDatasets.datasets = []

		// Create Compare DatasetGroup
		this._compareDatasets = new DatasetGroup("Compare", "darkred");
		this._compareDatasets['averagingIntervals'] = [];
		this._compareDatasets['averagingIntervals'].push(new DateRange('2036-1-1', '2065-12-31'));
		this._compareDatasets['averagingIntervals'].push(new DateRange('2070-1-1', '2099-12-31'));
		this._compareDatasets.dateRange = new DateRange(EsgfDataset.DEFAULT_RCP_DATES[0], EsgfDataset.DEFAULT_RCP_DATES[1]);
		this._compareDatasets.datasets = []
	}

	public get refDatasetGroup() {
		return this._referenceDatasets;
	}

	public get compDatasetGroup() {
		return this._compareDatasets;
	}

	public getSelectedDatasetCount(): number {
		if (this._ensembles.length == 0) {
			return 0;
		}

		return (this._ensembles.map((ensemble: Ensemble) => {
			return (ensemble.isActive && ensemble.hasSelectedItem()) ? 1 : 0;
		}) as number[]).reduce((accumulator, currentValue) => accumulator + currentValue, 0);
	}

	private buildEnsemble(items: EnsembleItem[]): Ensemble {
		// generate name reference based on all grouped props
		const props: string[][] = [];
		props.push(Object.keys(_.groupBy(items, 'project')))
		props.push(Object.keys(_.groupBy(items, 'product')))
		props.push(Object.keys(_.groupBy(items, 'domain')))
		props.push(Object.keys(_.groupBy(items, 'institute')))
		props.push(Object.keys(_.groupBy(items, 'drivingModel')))
		props.push(Object.keys(_.groupBy(items, 'ensemble')))
		props.push(Object.keys(_.groupBy(items, 'regionalModel')))
		props.push(Object.keys(_.groupBy(items, 'downscaling')))
		props.push(Object.keys(_.groupBy(items, 'timeFrequency')))
		props.push(Object.keys(_.groupBy(items, 'variable')))
		// join them to derive a proper name
		const name = props.flatMap((v: string[]) => v.join('|')).join('.');

		return new Ensemble(name, items.sort((a: EnsembleItem, b: EnsembleItem) =>
			a.getLabel().localeCompare(b.getLabel())
		));
	}

	public groupDatasets(datasets: EsgfDataset[]): Ensemble[] {
		// group based on driving and regional models
		const grouped: _.Dictionary<EsgfDataset[]> = _.groupBy(datasets, (ds: EsgfDataset) => 
		{ return [ds.drivingModel, ds.regionalModel, ds.ensemble, ds.downscaling].join(':') });

		// create an ensemble for each group
		const ensembles: Ensemble[] = [];
		for (const items of Object.values(grouped)) {
			ensembles.push(this.buildEnsemble(items));
		}

		return ensembles;
	}

	public getDatasetGroups(): [DatasetGroup, DatasetGroup] {
		this._referenceDatasets.datasets.length = 0;
		this._compareDatasets.datasets.length = 0;

		const selectedEnsembles = _.filter(this._ensembles, 'isActive')
		selectedEnsembles.forEach((ensemble: Ensemble) => {
			ensemble.items.forEach((item: EnsembleItem) => {
				if (!item.isActive) {
					// skip non active items
					return;
				}

				const dataset: EsgfDataset = item as EsgfDataset;

				if (dataset.experiment == 'historical') {
					this._referenceDatasets.addDataset(dataset);
				} else {
					this._compareDatasets.addDataset(dataset);
				}
			})
		});

		return [this._referenceDatasets, this._compareDatasets];
	}

	public set dataArchiveQueryProps(props: string[]) {
		this._dataArchiveQueryProps = props;

		this.callObservers(ChangeWorkflowSettings.DATA_ARCHIVE_QUERY_PROPS_UPDATED);
	}

	public get dataArchiveQueryProps(): string[] {
		return this._dataArchiveQueryProps;
	}

	public set dataArchive(dataArchive: DataArchive) {
		this._dataArchive = dataArchive;

		this.callObservers(ChangeWorkflowSettings.DATA_ARCHIVE_UPDATED);
	}

	public get dataArchive(): DataArchive {
		return this._dataArchive;
	}

	public set eventTimeseries(eventTimeseries: HighPrecipEventTimeseries) {
		this._eventTimeseries = eventTimeseries;

		this.callObservers(ChangeWorkflowSettings.EVENT_TIMESERIES_UPDATED);
	}

	public get eventTimeseries(): HighPrecipEventTimeseries {
		return this._eventTimeseries;
	}

	public get dateRange(): DateRange {
		return this._dateRange;
	}

	public set ensembles(ensembles: Ensemble[]) {
		this._ensembles = ensembles;

		this.callObservers(ChangeWorkflowSettings.ENSEMBLES_UPDATED);
	}

	public get ensembles(): Ensemble[] {
		return this._ensembles;
	}

	public getQueryProperties(onQueryPropsClb: (props: QueryProperty[]) => void): void {
		if (this._dataArchive) {
			this._dataArchive.getQueryProperties(this._dataArchiveQueryProps).then(onQueryPropsClb);
		}
	}

	public removeUnselectedEnsembles(): void {
		_.remove(this._ensembles, (ensemble) => {
			return !ensemble.isActive;
		});
	}

	public queryDatasets(props: QueryProperty[], onFinish?: () => void): void {
		if (this._dataArchive) {
			// new search results - remove old ones first
			this.removeUnselectedEnsembles();

			// place query at archive
			this._dataArchive.queryDatasets(props).then((datasets: Dataset[]) => {
				// convert to ensembles and add to ensembles list
				this.ensembles.push(...this.groupDatasets(datasets as EsgfDataset[]));

				if (onFinish) {
					onFinish()
				}
			});
		}
	}

	public getRoiWgs84Wkt(): string {
		if (this.roiWgs84) {
			return new WKT().writeGeometry(this.roiWgs84, { 'decimals': 6 });
		} else {
			return ""
		}
	}

	public getDatasetPresetNames(): string[] {
		return Array.from(this.datasetPresets.keys());
	}

	public isAnalysisConfigurationValid(): boolean {
		return this.ensembles.length > 0 && this.roiWgs84 != undefined;
	}

	public loadPreset(presetName: string): void {
		console.log('loading preset ', presetName);


		// clear ensemble list
		this._ensembles.length = 0;

		if (presetName != undefined && this.datasetPresets.has(presetName)) {
			// get preset data sets
			const presetDatasets = this.datasetPresets.get(presetName)

			// group data sets into ensembles
			const ensembles: Ensemble[] = this.groupDatasets(presetDatasets);

			// set all ensembles to active
			ensembles.forEach((ensemble: Ensemble) => {
				ensemble.isActive = true;

				// set all ensemble items active
				ensemble.items.forEach((item: EnsembleItem) => {
					item.isActive = true;
				});
			});

			// add ensembles to currently selected datasets
			this._ensembles.push(...ensembles);
		}
	}
}
