const io = require('socket.io-client');
const qs = require('querystring');
const _ = require('lodash');

const socket = io.connect('/');

const defaultFetchOpts = {
  method: 'GET', headers: {
    'Content-type': 'application/json',
    'Accept': 'application/json'
  }
};

const ServerAPI = {
  getGraphAndLayout(uri, version) {
    return fetch(`/api/get-graph-and-layout?${qs.stringify({uri, version})}`, defaultFetchOpts).then(res => res.json());
  },

  pcQuery(method, params){
    return fetch(`/pc-client/${method}?${qs.stringify(params)}`, defaultFetchOpts);
  },

  datasources(){
    return fetch('/pc-client/datasources', defaultFetchOpts).then(res => res.json());
  },

  querySearch(query){
    const queryClone=_.assign({},query);
    if (/^((uniprot|hgnc):\w+|ncbi:[0-9]+)$/i.test(queryClone.q)) {
      queryClone.q=queryClone.q.replace(/^(uniprot|ncbi|hgnc):/i,"");
    }
    return fetch(`/pc-client/querySearch?${qs.stringify(queryClone)}`, defaultFetchOpts).then(res => res.json());
  },

  enrich(query, type){
    if(query.genes.length>=1){
      return fetch(`/api/${type}`, {
        method:'POST',
        headers: {
          'Accept': 'application/json',
          'Content-Type': 'application/json'
        },
        body:JSON.stringify(query)
      }).then(res => res.json());
    }else{
      return Promise.resolve({geneInfo:[],unrecognized:[]});
    }
  },

  geneQuery(query){
    return this.enrich(query, "validation");
  },

  getGeneInformation(ids){
    return fetch(`https://eutils.ncbi.nlm.nih.gov/entrez/eutils/esummary.fcgi?retmode=json&db=gene&id=${ids.join(',')}`, {method: 'GET'}).then(res => res.json());
  },

  getUniprotnformation(ids){
    return fetch(`https://www.ebi.ac.uk/proteins/api/proteins?offset=0&accession=${ids.join(',')}`, defaultFetchOpts).then(res => res.json());
  },

  // Send a diff in a node to the backend. The backend will deal with merging these diffs into
  // a layout
  submitNodeChange(uri, version, nodeId, bbox) {
    socket.emit('submitDiff', {
      uri: uri,
      version: version.toString(),
      diff: {
        nodeID: nodeId,
        bbox: bbox
      }
    });
  },

  submitLayoutChange(uri, version, layout) {
    socket.emit('submitLayout', {
      uri: uri,
      version: version,
      layout: layout
    });
  },

  initReceiveLayoutChange(callback) {
    socket.on('layoutChange', layoutJSON => {
      callback(layoutJSON);
    });
  },

  initReceiveNodeChange(callback) {
    socket.on('nodeChange', nodeDiff => {
      callback(nodeDiff);
    });
  },

};

module.exports = ServerAPI;
@@ -1030,41 +1030,8 @@
 e)%7B%0A
-    if(query.genes.length%3E=1)%7B%0A  
     
@@ -1067,18 +1067,16 @@
 %7B%0A      
-  
 method:'
@@ -1088,18 +1088,16 @@
 ,%0A      
- 
- 
 headers:
@@ -1095,26 +1095,24 @@
  headers: %7B%0A
-  
         'Acc
@@ -1145,18 +1145,16 @@
         
- 
- 
 'Content
@@ -1186,18 +1186,16 @@
 '%0A      
-  
 %7D,%0A     
@@ -1195,18 +1195,16 @@
 ,%0A      
- 
- 
 body:JSO
@@ -1222,18 +1222,16 @@
 (query)%0A
-  
     %7D).t
@@ -1258,86 +1258,8 @@
 ));%0A
-    %7Delse%7B%0A      return Promise.resolve(%7BgeneInfo:%5B%5D,unrecognized:%5B%5D%7D);%0A    %7D%0A
   %7D,

remove if/else case