import { Injectable } from "@angular/core";

@Injectable({
    providedIn: 'root'
})

export class JsonStore {

    /* createCollection(name, data) {

        var collectionName = name;
        var addOptions = {
            // Mark data as dirty (true = yes, false = no), default true.
            markDirty: true
        };

        var JSONStoreCollections = {};
        JSONStoreCollections[collectionName] = {};
        JSONStoreCollections[collectionName].searchFields = { UserId: 'integer' };

        WL.JSONStore.init(JSONStoreCollections)

            .then(function () {
                WL.Logger.debug('Init done');
                console.log('Init done');
                return WL.JSONStore.get(collectionName).add(data, addOptions);

            }).fail(function (err) {
                WL.Logger.error(err);
                console.log(err)
            });

    } */

    getCollection(collectionName) {

        var allOptions = {
            // Returns a maximum of 10 documents, default no limit.
            limit: 10,
            // Skip 0 documents, default no offset.
            offset: 0,
            // Search fields to return, default: ['_id', 'json'].
            filter: ['UserId', 'json'],
            // How to sort the returned values, default no sort.}]
        };

        var JSONStoreCollections = {};
        JSONStoreCollections[collectionName] = {};
        JSONStoreCollections[collectionName].searchFields = { UserId: 'integer' };

      /* WL.JSONStore.init(JSONStoreCollections)
            .then(function () {
                WL.Logger.debug('Find all colletion data');
                console.log('Find all colletion data');


            }).fail(function (err) {
                WL.Logger.error(err);
                console.log("JsonStore getColletion error ",err)
            });

            const data = WL.JSONStore.get(collectionName).findAll(allOptions).then((value) => {
                console.log('Find all colletion data', value);
                return JSON.parse(value);
            });


            return data */

        //return notificationData
    }

    getColletionById(collectionName, value) {

        var query = { UserId: value };

        var allOptions = {
            // Returns a maximum of 10 documents, default no limit.
            limit: 10,
            // Skip 0 documents, default no offset.
            offset: 0,
            // Search fields to return, default: ['_id', 'json'].
            filter: ['UserId', 'json'],
            // How to sort the returned values, default no sort.}]
        };

        var JSONStoreCollections = {};
        JSONStoreCollections[collectionName] = {};
        JSONStoreCollections[collectionName].searchFields = { UserId: 'integer' };

        /* WL.JSONStore.init(JSONStoreCollections)
            .then(function () {
                WL.Logger.debug('Find colletion data by id');
                console.log('Find colletion data by id');

                return WL.JSONStore.get(collectionName).find(query, allOptions).then((value) => {
                    console.log('Find colletion data by id', value);
                });
            }).fail(function (err) {
                WL.Logger.error(err);
                console.log(err)
            }); */

    }

    replaceDocument(collectionName: string, document: any) {
        /* var document = { Example of how identify the document to replace
            _id: 1, json: {name: 'chevy', age: 23}
          }; 
        var options = {};

        /* WL.JSONStore.get(collectionName).replace(document, options).then(function (numberOfDocsReplaced) {
            console.log("JsonStore replace document sucess: ", numberOfDocsReplaced)
        }).fail(function (error) {
            console.log("JsonStore replace document error: ", error)
        }); */
    }

    removeDocument(collectionName: any,query: any,) {
        /* var query = { _id: 1 };  Exemple of query
        var options = { exact: true };
        /* WL.JSONStore.get(collectionName).remove(query, options).then(function (numberOfDocsRemoved) {
            console.log("JsonStore remove document sucess: ", numberOfDocsRemoved)
        }).fail(function (error) {
            console.log("JsonStore remove document erro: ", error)
        }); */
    }
}
