# Path: odpw/core/dataset_converter.py
# DCT = Namespace("http://purl.org/dc/terms/")
# DCAT = Namespace("http://www.w3.org/ns/dcat#")
# ADMS = Namespace("http://www.w3.org/ns/adms#")
# VCARD = Namespace("http://www.w3.org/2006/vcard/ns#")
# FOAF = Namespace("http://xmlns.com/foaf/0.1/")
# SCHEMA = Namespace('http://schema.org/')
# TIME = Namespace('http://www.w3.org/2006/time')
# LOCN = Namespace('http://www.w3.org/ns/locn#')
# GSP = Namespace('http://www.opengis.net/ont/geosparql#')
# OWL = Namespace('http://www.w3.org/2002/07/owl#')
# ADEQUATE = Namespace('https://www.adequate.at/ns#')
# GEOJSON_IMT = 'https://www.iana.org/assignments/media-types/application/vnd.geo+json'
# VALID_URL = re.compile(
#         r'^(?:http|ftp)s?://' # http:// or https://
#         r'(?:(?:[A-Z0-9](?:[A-Z0-9-]{0,61}[A-Z0-9])?\.)+(?:[A-Z]{2,6}\.?|[A-Z0-9-]{2,}\.?)|' #domain...
#         r'localhost|' #localhost...
#         r'\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})' # ...or ip
#         r'(?::\d+)?' # optional port
#         r'(?:/?|[/?]\S+)$', re.IGNORECASE)
# URI = get_compiled_pattern('^%(URI)s$')
# def add_dcat_to_graph(dataset_dict, portal, graph, portal_uri):
# def dict_to_dcat(dataset_dict, portal, graph=None, format='json-ld', portal_uri=None):
# def fix_socrata_graph(g, dataset_dict, portal_url):
# def is_valid_url(references):
# def is_valid_uri(references):
# def graph_from_opendatasoft(g, dataset_dict, portal_url):
# def graph_from_data_gouv_fr(g, dataset_dict, portal_url):
#     def __init__(self, graph, portal_base_url):
#     def graph_from_ckan(self, dataset_dict):
#     def _get_dataset_value(self, dataset_dict, key, default=None):
#     def publisher_uri_from_dataset_dict(self, dataset_dict):
#     def catalog_uri(self):
#     def resource_uri(self, resource_dict, dataset_id):
#     def dataset_uri(self, dataset_dict):
# def _add_triples_from_dict(graph, _dict, subject, items,
#                            list_value=False,
#                            date_value=False):
# def _add_date_triples_from_dict(graph, _dict, subject, items):
# def _add_list_triples_from_dict(graph, _dict, subject, items):
# def _add_triple_from_dict(graph, _dict, subject, predicate, key,
#                           fallbacks=None,
#                           list_value=False,
#                           date_value=False):
# def _get_dict_value(_dict, key, default=None):
# def _add_list_triple(graph, subject, predicate, value):
# def _add_date_triple(graph, subject, predicate, value):
# class CKANConverter:


import rdflib
import json
from rdflib import URIRef, BNode, Literal
from rdflib.namespace import Namespace, RDF, RDFS, DCTERMS, XSD
from odpw.core import dataset_converter

# https://www.w3.org/2015/spatial/wiki/ISO_19115_-_DCAT_-_Schema.org_mapping

def resp_party(g, doc, orga):
    if isinstance(orga, URIRef) and '@id' not in doc:
        doc['@id'] = str(orga)
    for p, o in g.predicate_objects(orga):
        if 'name' not in doc:
            if p == FOAF.name:
                doc['name'] = str(o)
            elif p == VCARD.fn:
                doc['name'] = str(o)
            elif p == VCARD['organization-name']:
                doc['name'] = str(o)
        if 'email' not in doc:
            if p == FOAF.mbox:
                doc['email'] = str(o)
            elif p == VCARD.hasEmail:
                doc['email'] = str(o)
        if 'url' not in doc:
            if p == FOAF.homepage:
                doc['url'] = str(o)
            elif p == VCARD.hasURL:
                doc['url'] = str(o)
    return doc


def convert(portal, data):
    g = rdflib.Graph()
    # write dcat dataset into graph

# Path: odpw/core/dataset_converter.py
# DCT = Namespace("http://purl.org/dc/terms/")
# DCAT = Namespace("http://www.w3.org/ns/dcat#")
# ADMS = Namespace("http://www.w3.org/ns/adms#")
# VCARD = Namespace("http://www.w3.org/2006/vcard/ns#")
# FOAF = Namespace("http://xmlns.com/foaf/0.1/")
# SCHEMA = Namespace('http://schema.org/')
# TIME = Namespace('http://www.w3.org/2006/time')
# LOCN = Namespace('http://www.w3.org/ns/locn#')
# GSP = Namespace('http://www.opengis.net/ont/geosparql#')
# OWL = Namespace('http://www.w3.org/2002/07/owl#')
# ADEQUATE = Namespace('https://www.adequate.at/ns#')
# GEOJSON_IMT = 'https://www.iana.org/assignments/media-types/application/vnd.geo+json'
# VALID_URL = re.compile(
#         r'^(?:http|ftp)s?://' # http:// or https://
#         r'(?:(?:[A-Z0-9](?:[A-Z0-9-]{0,61}[A-Z0-9])?\.)+(?:[A-Z]{2,6}\.?|[A-Z0-9-]{2,}\.?)|' #domain...
#         r'localhost|' #localhost...
#         r'\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})' # ...or ip
#         r'(?::\d+)?' # optional port
#         r'(?:/?|[/?]\S+)$', re.IGNORECASE)
# URI = get_compiled_pattern('^%(URI)s$')
# def add_dcat_to_graph(dataset_dict, portal, graph, portal_uri):
# def dict_to_dcat(dataset_dict, portal, graph=None, format='json-ld', portal_uri=None):
# def fix_socrata_graph(g, dataset_dict, portal_url):
# def is_valid_url(references):
# def is_valid_uri(references):
# def graph_from_opendatasoft(g, dataset_dict, portal_url):
# def graph_from_data_gouv_fr(g, dataset_dict, portal_url):
#     def __init__(self, graph, portal_base_url):
#     def graph_from_ckan(self, dataset_dict):
#     def _get_dataset_value(self, dataset_dict, key, default=None):
#     def publisher_uri_from_dataset_dict(self, dataset_dict):
#     def catalog_uri(self):
#     def resource_uri(self, resource_dict, dataset_id):
#     def dataset_uri(self, dataset_dict):
# def _add_triples_from_dict(graph, _dict, subject, items,
#                            list_value=False,
#                            date_value=False):
# def _add_date_triples_from_dict(graph, _dict, subject, items):
# def _add_list_triples_from_dict(graph, _dict, subject, items):
# def _add_triple_from_dict(graph, _dict, subject, predicate, key,
#                           fallbacks=None,
#                           list_value=False,
#                           date_value=False):
# def _get_dict_value(_dict, key, default=None):
# def _add_list_triple(graph, subject, predicate, value):
# def _add_date_triple(graph, subject, predicate, value):
# class CKANConverter:


# Path: schemadotorg/dcat_to_schemadotorg.py
import rdflib
import json
from rdflib import URIRef, BNode, Literal
from rdflib.namespace import Namespace, RDF, RDFS, DCTERMS, XSD
from odpw.core import dataset_converter


# https://www.w3.org/2015/spatial/wiki/ISO_19115_-_DCAT_-_Schema.org_mapping

def resp_party(g, doc, orga):
    if isinstance(orga, URIRef) and '@id' not in doc:
        doc['@id'] = str(orga)
    for p, o in g.predicate_objects(orga):
        if 'name' not in doc:
            if p == FOAF.name:
                doc['name'] = str(o)
            elif p == VCARD.fn:
                doc['name'] = str(o)
            elif p == VCARD['organization-name']:
                doc['name'] = str(o)
        if 'email' not in doc:
            if p == FOAF.mbox:
                doc['email'] = str(o)
            elif p == VCARD.hasEmail:
                doc['email'] = str(o)
        if 'url' not in doc:
            if p == FOAF.homepage:
                doc['url'] = str(o)
            elif p == VCARD.hasURL:
                doc['url'] = str(o)
    return doc


def convert(portal, data):
    g = rdflib.Graph()
    # write dcat dataset into graph

    dataset_converter.dict_to_dcat(data, portal, graph=g)