import { API_URL } from '../constants/apiUrls';

var apiClass = undefined;

class Api
{

    constructor(url, auth) {
        this.url = url;
        this.auth = auth;
        this.cache = [];
        this.ignoredStores = ['METADATASTORE', 'HISTORYSTORE'];
    }

    getNamespaces() {
        const ignoredStores = this.ignoredStores;

        return fetch(this.url+'/dataStore', this.getHeaders())
            .then(response => this.successOnly(response))
            .then(response => response.json())
            .then(response => {
                return response.filter(function(value) {
                    return ignoredStores.indexOf(value) === -1;
                })
            });
    }

    deleteNamespace(namespace) {
        return fetch(this.url+'/dataStore/'+namespace, Object.assign({}, this.getHeaders(), {
            'method': 'DELETE',
        }))
            .then(response => this.successOnly(response))
            .then(response => response.json());
    }

    getKeys(namespace) {
        return fetch(this.url+'/dataStore/'+namespace, this.getHeaders())
            .then(response => this.successOnly(response))
            .then(response => response.json());
    }

    getValue(namespace, key) {
        const k = this.buildId(namespace, key);
        var cache = this.cache;

        //if (!cache[k]) {
            return this.getMetaData(namespace, key)
                .then(result => {
                    cache[k] = result;
                    return JSON.parse(result.value);
                });
      //  }

        return new Promise(function (resolve, reject) {
            console.log('cache resolve');
            resolve(JSON.parse(cache[k].value));
        });
    }

    getMetaData(namespace, key) {
        return fetch(this.url+'/dataStore/'+namespace+'/'+key+'/metaData', this.getHeaders())
            .then(response => this.successOnly(response))
            .then(response => response.json());
    }

    createValue(namespace, key, value, log = true) {
        return fetch(this.url+'/dataStore/'+namespace+'/'+key, Object.assign({}, this.getHeaders(), {
            'method': 'POST',
            'body': JSON.stringify(value),
        }))
            .then(response => this.successOnly(response))
            .then(response => response.json())
            .then(response => {
                log && this.updateHistory(namespace, key, value);
                return response;
            });
    }

    updateValue(namespace, key, value, log = true) {
        return fetch(this.url+'/dataStore/'+namespace+'/'+key, Object.assign({}, this.getHeaders(), {
            'method': 'PUT',
            'body': JSON.stringify(value)
        }))
            .then(response => this.successOnly(response))
            .then(response => response.json())
            .then(response => {
                log && this.updateHistory(namespace, key, value);
                return response;
            });
    }

    deleteValue(namespace, key) {
        return fetch(this.url+'/dataStore/'+namespace+'/'+key, Object.assign({}, this.getHeaders(), {
            'method': 'DELETE',
        }))
            .then(response => this.successOnly(response))
            .then(response => response.json())
            .then(response => {
                this.updateHistory(namespace, key, 'DELETED');
                return response;
            });
    }


    getHistory(namespace, key) {
        const id = this.buildId(namespace, key);

        return fetch(this.url+'/dataStore/HISTORYSTORE/'+id, this.getHeaders());
    }

    updateHistory(namespace, key, newValue) {
        const id = this.buildId(namespace, key);
        const historyRecord = {
            'date': new Date(),
            'value': newValue
        };

        return this.getHistory(namespace, key)
            .then(response => {
                if (response.status === 404) {
                    this.createValue('HISTORYSTORE', id, [historyRecord], false);
                    return null;
                } else {
                    return response.json();
                }
            }).then(history => {
                if (history !== null) {
                    history.unshift(historyRecord);
                    this.updateValue('HISTORYSTORE', id, history, false);
                }
            });
    }


    successOnly(response) {
        if (response.status >= 200 && response.status < 300) {
            return Promise.resolve(response);
        }
        return Promise.reject(response);
    }

    buildId(namespace, key) {
        return encodeURIComponent(namespace+':'+key);
    }

    getHeaders() {
        return {
            'method': 'GET',
            'mode': 'cors',
            'headers': {
                'Content-Type': 'application/json',
                'Authorization': this.auth
            }
        };
    }
}

export default (function getApi() {
    if (typeof apiClass === 'undefined') {
        apiClass = new Api(API_URL, `Basic ${btoa('admin:district')}`);
    }

    return apiClass;
})();

@@ -1309,18 +1309,16 @@
         
-//
 if (!cac
@@ -1322,24 +1322,24 @@
 cache%5Bk%5D) %7B%0A
+
             
@@ -1408,24 +1408,82 @@
 result =%3E %7B%0A
+                    const val = JSON.parse(result.value);%0A
             
@@ -1501,22 +1501,19 @@
 he%5Bk%5D = 
-result
+val
 ;%0A      
@@ -1537,32 +1537,11 @@
 urn 
-JSON.parse(result.value)
+val
 ;%0A  
@@ -1568,10 +1568,8 @@
     
-//
   %7D%0A
@@ -1691,27 +1691,16 @@
 lve(
-JSON.parse(
 cache%5Bk%5D
 .val
@@ -1695,23 +1695,16 @@
 cache%5Bk%5D
-.value)
 );%0A     
@@ -2824,32 +2824,97 @@
 n(response =%3E %7B%0A
+                this.cache%5Bthis.buildId(namespace,key)%5D = value;%0A
                 

Fix caching when updating values