import fetch from 'isomorphic-fetch'
import credentials from '../../credentials'
const { username, password } = credentials
const authorization = 'Basic ' + btoa(`${username}:${password}`)

const urlClassifications = 
  'https://unece-stardog.ichec.ie/annex/classifications/sparql/query'

const makeQueryGetClassificationItems = uri =>  `
  PREFIX skos:<http://www.w3.org/2004/02/skos/core#>

  SELECT ?poste  WHERE {
  	<${uri}> skos:hasTopConcept ?poste
  }
`

const queryGetClassificationList = `
  select ?classification WHERE {   ?classification rdf:type skos:ConceptScheme . }
`

const bodyFromSparql = query => 
  encodeURIComponent('query') + '=' + 
  encodeURIComponent(query)


/**
 * Classification List
 */
export const remoteGetClassificationsList = () =>
  fetch(urlClassifications, {
    method: 'POST',
    headers: {
      'Authorization': authorization, 
      'Accept': 'application/sparql-results+json',
      'Content-Type': 'application/x-www-form-urlencoded'
    },
    body: bodyFromSparql(queryGetClassificationList)
  })
  .then(res => res.json())

/**
 * Classification items
 */
export const remoteGetClassificationItems = uri =>
  fetch(urlClassifications, {
    method: 'POST',
    headers: {
      'Authorization': authorization,
      'Accept': 'application/sparql-results+json',
      'Content-Type': 'application/x-www-form-urlencoded'
    },
    body: bodyFromSparql(makeQueryGetClassificationItems(uri))
  })
  .then(res => res.json())

@@ -1,12 +1,79 @@
+//Stardog HTTP API documentation: http://docs.stardog.apiary.io/#%0A%0A
 import fetch
@@ -753,45 +753,227 @@
 %0A%0A%0A/
-**%0A * Classification List%0A */%0Aexport 
+/ We need to pass some %60x-www-form-urlencoded%60 data. %60multipart/form-data%60%0A// created with %60new FormData()%60 does not work.%0A/**%0A * Retrieve all the classification uri%0A * @return %7BPromise%7D Resolves to json if success%0A */%0A
 cons
@@ -972,34 +972,35 @@
  */%0Aconst remote
-Ge
+Pos
 tClassifications
@@ -1323,33 +1323,601 @@
 )%0A%0A/
-**%0A * Classification item
+/ We can also use a 'GET' verb if the query is not too long%0A/**%0A * Retrieve all the classifications uri%0A * @return %7BPromise%7D Resolves to json if success%0A */%0Aconst remoteGetClassificationsList = () =%3E%0A  fetch(urlClassifications + '?' + bodyFromSparql(queryGetClassificationList), %7B%0A    method: 'GET',%0A    headers: %7B%0A      'Authorization': authorization, %0A      'Accept': 'application/sparql-results+json',%0A    %7D%0A  %7D)%0A  .then(res =%3E res.json())%0A%0A/**%0A * Retrieve all the items for a classification%0A * @param  %7BString%7D uri uri of a classification%0A * @return %7BPromise%7D    Resolves to json if succes
 s%0A *

Add support for 'GET' request to stardog