import { CatalogItem, CatalogItemStatus, getCatalogItemId } from "dasf-web/lib/map/legend/DemapCatalogList";
import Layer from "ol/layer/Layer";
import VectorSource from "ol/source/Vector";
import GeoJSON from 'ol/format/GeoJSON.js';
import { GaugeDataAcquisition } from "~/components/similarity/model/GaugeDataAcquisition";
import Demap from "dasf-web/lib/map/Demap.vue";
import { Gauges } from "~/components/similarity/model/Gauge";
import Feature from "ol/Feature";
import Geometry from "ol/geom/Geometry";

function buildCatalog(): CatalogItem {
    let fs: CatalogItem = {
        id: getCatalogItemId(),
        name: 'Flood Similarity',
        status: CatalogItemStatus.folder,
        description: '(attribution: Stefan Lüdtke, GFZ Potsdam)',
        disabledReason: '',
        disabled: false,
        needsRoi: false,
        children: []
    }

    let gauges: CatalogItem = {
        id: getCatalogItemId(),
        name: 'Gauge Stations Germany',
        status: CatalogItemStatus.none,
        description: 'Collection of gauge station locations',
        disabledReason: '',
        disabled: false,
        needsRoi: false,
    }

    gauges.call = (addLayer: (layer: Layer) => void): void => {
        gauges.status = CatalogItemStatus.loading;

        new GaugeDataAcquisition().loadGauges().then((value: Gauges) => {
            gauges.status = CatalogItemStatus.processing;
            let gaugeSource = new VectorSource({
                features: new GeoJSON().readFeatures(value, {
                    dataProjection: 'EPSG:4326',
                    featureProjection: 'EPSG:3857'
                })
            });

            addLayer(Demap.createVectorLayer('gauges', gaugeSource));
            gauges.status = CatalogItemStatus.finished;
        }).catch((reason) => {
            gauges.status = CatalogItemStatus.error;
            throw new Error('error requesting gauge data: ' + reason);
        });
    }

    fs.children.push(gauges);

    let basins: CatalogItem = {
        id: getCatalogItemId(),
        name: 'Catchment Basins',
        status: CatalogItemStatus.none,
        description: 'Collection of catchment basis associated to gauge station locations',
        disabledReason: '',
        disabled: false,
        needsRoi: false,
    }

    basins.call = (addLayer: (layer: Layer) => void): void => {
        basins.status = CatalogItemStatus.loading;

        new GaugeDataAcquisition().loadBasins().then((value: Feature<Geometry>[]) => {
            basins.status = CatalogItemStatus.processing;
            let basinSource = new VectorSource({
                features: value
            });

            addLayer(Demap.createVectorLayer('basins', basinSource));
            basins.status = CatalogItemStatus.finished;
        }).catch((reason) => {
            basins.status = CatalogItemStatus.error;
            throw new Error('error requesting basins data: ' + reason);
        });
    }

    fs.children.push(basins);

    return fs;
}

const catalog: CatalogItem = buildCatalog();
export default catalog;