import axios from 'axios';

const API_URL = `/api/v1`;

class WhatsOptApi {
  constructor(csrfToken, apiKey, relativeUrlRoot) {
    axios.defaults.headers.common['X-CSRF-Token'] = csrfToken;
    axios.defaults.headers.common['Accept'] = 'application/json';
    axios.defaults.headers.common['Authorization'] = 'Token '+apiKey;
    this.relativeUrlRoot = relativeUrlRoot;
  }

  url(path) {
    return `${this.relativeUrlRoot}${path}`;
  };

  apiUrl(path) {
    return `${API_URL}${this.url(path)}`;
  };

  openmdaoChecking(mdaId, callback) {
    const path = `/analyses/${mdaId}/openmdao_checking`;
    axios.post(this.apiUrl(path))
        .then(callback)
        .catch((error) => console.log(error));
  };

  getMemberCandidates(mdaId, callback) {
    const path = `/user_roles?query[analysis_id]=${mdaId}&query[select]=member_candidates`;
    axios.get(this.apiUrl(path))
        .then(callback)
        .catch((error) => console.log(error));
  }

  addMember(userId, mdaId, callback) {
    const path = `/user_roles/${userId}`;
    axios.put(this.apiUrl(path), {user: {analysis_id: mdaId, role: 'member'}})
        .then(callback)
        .catch((error) => console.log(error));
  }

  removeMember(userId, mdaId, callback) {
    const path = `/user_roles/${userId}`;
    axios.put(this.apiUrl(path), {user: {analysis_id: mdaId, role: 'none'}})
        .then(callback)
        .catch((error) => console.log(error));
  }

  updateUserSettings(userId, settings, callback) {
    const path = `/users/${userId}`;
    axios.put(this.apiUrl(path), {user: {settings: settings}})
        .then(callback)
        .catch((error) => console.log(error));
  }

  getAnalysis(mdaId, xdsmFormat, callback) {
    let path = `/analyses/${mdaId}`;
    if (xdsmFormat) {
      path += '.xdsm';
    }
    axios.get(this.apiUrl(path))
        .then(callback)
        .catch((error) => console.log(error));
  };

  updateAnalysis(mdaId, mdaAttrs, callback, onError) {
    const path = `/analyses/${mdaId}`;
    axios.put(this.apiUrl(path), {analysis: mdaAttrs})
        .then(callback)
        .catch(onError);
  }

  createDiscipline(mdaId, disciplineAttributes, callback) {
    const path = `/analyses/${mdaId}/disciplines`;
    axios.post(this.apiUrl(path), {discipline: disciplineAttributes})
        .then(callback)
        .catch((error) => console.log(error));
  }

  updateDiscipline(discId, disciplineAttributes, callback) {
    const path = `/disciplines/${discId}`;
    axios.put(this.apiUrl(path), {discipline: disciplineAttributes})
        .then(callback)
        .catch((error) => console.log(error));
  }

  deleteDiscipline(discId, callback) {
    const path = `/disciplines/${discId}`;
    axios.delete(this.apiUrl(path))
        .then(callback)
        .catch((error) => console.log(error));
  }

  getSubAnalysisCandidates(callback) {
    const path = `/analyses`;
    axios.get(this.apiUrl(path))
        .then(callback)
        .catch((error) => console.log(error));
  }
  
  createSubAnalysisDiscipline(discId, subMdaId, callback) {
    const path = `/disciplines/${discId}/analysis_discipline`;
    axios.post(this.apiUrl(path), {analysis_discipline: {analysis_id: subMdaId}})
        .then(callback)
        .catch((error) => console.log(error));    
  }
  
  createConnection(mdaId, connectionAttributes, callback, onError) {
    const path = `/analyses/${mdaId}/connections`;
    axios.post(this.apiUrl(path), {connection: connectionAttributes})
        .then(callback)
        .catch(onError);
  }

  updateConnection(connectionId, connectionAttributes, callback, onError) {
    const path = `/connections/${connectionId}`;
    axios.put(this.apiUrl(path), {connection: connectionAttributes})
        .then(callback)
        .catch(onError);
  }

  deleteConnection(connectionId, callback) {
    const path = `/connections/${connectionId}`;
    axios.delete(this.apiUrl(path))
        .then(callback)
        .catch((error) => console.log(error));
  }

  getOperation(operationId, callback) {
    const path = `/operations/${operationId}`;
    axios.get(this.apiUrl(path))
        .then(callback)
        .catch((error) => console.log(error));
  }

  updateOperation(operationId, opeAttrs, callback, onError) {
    const path = `/operations/${operationId}`;
    const jobpath = `${path}/job`;
    axios.patch(this.apiUrl(path), {operation: opeAttrs})
        .then(() => axios.post(this.apiUrl(jobpath)).then(callback).catch((error) => console.log(error)))
        .catch((error) => console.log(error));
  }

  killOperationJob(operationId) {
    const path = `/operations/${operationId}/job`;
    axios.patch(this.apiUrl(path))
        .then((resp) => console.log(resp))
        .catch((error) => console.log(error));
  }

  pollOperationJob(operationId, check, callback, onError) {
    const path = `/operations/${operationId}/job`;
    this._pollStatus(() => axios.get(this.apiUrl(path)), check, callback, 300000, 2000)
        .then(callback)
        .catch(onError);
  }

  _pollStatus(fn, check, callback, timeout, interval) {
    const endTime = Number(new Date()) + (timeout || 2000);
    interval = interval || 100;
    const checkCondition = function(resolve, reject) {
      var ajax = fn();
      ajax.then( function(response) {
        // If the condition is met, we're done!
        if (check(response.data)) {
          resolve(response.data);
        }
        // If the condition isn't met but the timeout hasn't elapsed, go again
        else if (Number(new Date()) < endTime) {
          callback(response.data);
          setTimeout(checkCondition, interval, resolve, reject);
        }
        // Didn't match and too much time, reject!
        else {
          reject(new Error('timed out for ' + fn));
        }
      });
    };

    return new Promise(checkCondition);
  }
};

export default WhatsOptApi;

@@ -465,16 +465,39 @@
 turn %60$%7B
+this.relativeUrlRoot%7D$%7B
 API_URL%7D
@@ -502,22 +502,12 @@
 L%7D$%7B
-this.url(
 path
-)
 %7D%60;%0A

fix stupid bug