"""Endpoint for the manipulation of datasets
"""

import hashlib

from flask import Response
from flask_restx import Namespace, Resource, abort

from app.common import client
from app.common import datasets as datasets_fcts
from app.common import path

api = Namespace("datasets", description="Datasets related endpoints")


@api.route("/")
class Datasets(Resource):
    def get(self):
        """Return a list of all datasets known by the platform"""
        datasets = client.get_dataset_list()
        if len(datasets) == 0:
            abort(404)

        add_openaire_links(datasets)

        return datasets


@api.route("/full/")
class DatasetsFull(Resource):
    def get(self):
        """Return a list of all datasets known by the platform, along with their
        variables and time periods"""
        datasets = client.get_dataset_list()
        if len(datasets) == 0:
            abort(404)

        for dataset in datasets:
            dataset["info"] = client.get_parameters(dataset["ds_id"])
            if dataset["info"] is None:
                abort(404)

            datasets_fcts.process_parameters(
                dataset["info"],
                dataset_id=dataset["ds_id"],
                is_raster=dataset["is_raster"],
            )

        add_openaire_links(datasets)

        return datasets


@api.route("/<int:id>/parameters/")
class DatasetParameters(Resource):
    def get(self, id):
        """Return the variables and time periods available in a dataset"""
        parameters = client.get_parameters(id)
        if parameters is None:
            abort(404)

        datasets_fcts.process_parameters(parameters)

        return parameters


@api.route(
    "/layer_name/vector/<int:id>/", defaults={"variable": None, "time_period": None}
)
@api.route("/layer_name/vector/<int:id>/<string:variable>/<string:time_period>/")
@api.route(
    "/layer_name/vector/<int:id>/<string:variable>/", defaults={"time_period": None}
)
@api.route(
    "/layer_name/vector/<int:id>/-/<string:time_period>/",
    defaults={"variable": None},
)
class VectorLayerName(Resource):
    def get(self, id, variable=None, time_period=None):
        """Return an unique layer name"""
        if variable is not None:
            variable = variable.replace("__SLASH__", "/")

        layer_name = path.make_unique_layer_name(
            path.VECTOR, id, variable=variable, time_period=time_period
        )

        return Response(layer_name, mimetype="text/plain")


@api.route(
    "/layer_name/raster/<int:id>/", defaults={"variable": None, "time_period": None}
)
@api.route("/layer_name/raster/<int:id>/<string:variable>/<string:time_period>/")
@api.route(
    "/layer_name/raster/<int:id>/<string:variable>/", defaults={"time_period": None}
)
@api.route(
    "/layer_name/raster/<int:id>/-/<string:time_period>/",
    defaults={"variable": None},
)
class RasterLayerName(Resource):
    def get(self, id, variable=None, time_period=None):
        """Return an unique layer name"""
        if variable is not None:
            variable = variable.replace("__SLASH__", "/")

        layer_name = path.make_unique_layer_name(
            path.RASTER, id, variable=variable, time_period=time_period
        )

        return Response(layer_name, mimetype="text/plain")


@api.route("/legend/<path:layer_name>/")
class Legend(Resource):
    def get(self, layer_name):
        """Return the legend of the layer"""
        legend = client.get_legend(layer_name)
        if legend is None:
            abort(404)

        return legend


@api.route("/geojson/<path:layer_name>/")
class GeoJSON(Resource):
    def get(self, layer_name):
        """Return the GeoJSON file corresponding to the layer"""
        geojson = client.get_geojson(layer_name, ignore_intersecting=True)
        if geojson is None:
            abort(404)

        return geojson


@api.route("/areas/")
class Areas(Resource):
    def get(self):
        """Return a list of all areas known by the platform"""
        areas = client.get_areas()
        if len(areas) == 0:
            abort(404)

        return areas


def add_openaire_links(datasets):
    for dataset in datasets:
        shared_id = dataset.get("shared_id")
        if not shared_id:
            dataset["openaireLink"] = "https://enermaps.openaire.eu/"
        else:
            shared_id_hash = hashlib.md5(shared_id.encode())  # nosec
            dataset["openaireLink"] = (
                "https://enermaps.openaire.eu/search/dataset?datasetId=enermaps____::{}"
                .format(shared_id_hash.hexdigest())
            )

[(13, 'flask_restx.Namespace', 'Namespace', False, 'from flask_restx import Namespace, Resource, abort\n'), (20, 'app.common.client.get_dataset_list', 'client.get_dataset_list', False, 'from app.common import client\n'), (34, 'app.common.client.get_dataset_list', 'client.get_dataset_list', False, 'from app.common import client\n'), (58, 'app.common.client.get_parameters', 'client.get_parameters', False, 'from app.common import client\n'), (62, 'app.common.datasets.process_parameters', 'datasets_fcts.process_parameters', True, 'from app.common import datasets as datasets_fcts\n'), (84, 'app.common.path.make_unique_layer_name', 'path.make_unique_layer_name', False, 'from app.common import path\n'), (88, 'flask.Response', 'Response', False, 'from flask import Response\n'), (108, 'app.common.path.make_unique_layer_name', 'path.make_unique_layer_name', False, 'from app.common import path\n'), (112, 'flask.Response', 'Response', False, 'from flask import Response\n'), (119, 'app.common.client.get_legend', 'client.get_legend', False, 'from app.common import client\n'), (130, 'app.common.client.get_geojson', 'client.get_geojson', False, 'from app.common import client\n'), (141, 'app.common.client.get_areas', 'client.get_areas', False, 'from app.common import client\n'), (22, 'flask_restx.abort', 'abort', False, 'from flask_restx import Namespace, Resource, abort\n'), (36, 'flask_restx.abort', 'abort', False, 'from flask_restx import Namespace, Resource, abort\n'), (39, 'app.common.client.get_parameters', 'client.get_parameters', False, 'from app.common import client\n'), (43, 'app.common.datasets.process_parameters', 'datasets_fcts.process_parameters', True, 'from app.common import datasets as datasets_fcts\n'), (60, 'flask_restx.abort', 'abort', False, 'from flask_restx import Namespace, Resource, abort\n'), (121, 'flask_restx.abort', 'abort', False, 'from flask_restx import Namespace, Resource, abort\n'), (132, 'flask_restx.abort', 'abort', False, 'from flask_restx import Namespace, Resource, abort\n'), (143, 'flask_restx.abort', 'abort', False, 'from flask_restx import Namespace, Resource, abort\n'), (41, 'flask_restx.abort', 'abort', False, 'from flask_restx import Namespace, Resource, abort\n')]