import { Meteor } from 'meteor/meteor';
import { check } from 'meteor/check';
import { HTTP } from 'meteor/http';

import { Logger } from 'meteor/ostrio:logger';
import { LoggerFile } from 'meteor/ostrio:loggerfile';

const log = new Logger();
(new LoggerFile(log)).enable();

Meteor.methods({
    'createDevicemap' (item) {
        this.unblock();
        if (this.userId) {
            try {
                Meteor.settings.enableDebug ? log.debug(`createDeviceMap() => start create devicemap`, item, Meteor.user().username) : '';
                let options = {
                    data: item,
                    headers: {
                        'content-type': 'application/json',
                        'Accept': 'application/json'
                    }
                }
                Meteor.settings.enableDebug ? log.debug(`createDevicemap() => call create devicemap method`, item, Meteor.user().username) : '';
                const result = HTTP.call("POST", `${Meteor.settings.databusUrl}/devicemap/create`, options);
                Meteor.settings.enableDebug ? log.debug(`createDeviceMap() => createDevicemap have been called`, result, Meteor.user().username) : '';
                return true
            } catch (e) {
                log.error(`createDeviceMap() => error in create DeviceMap`, e, Meteor.user().username)
                return false;
            }
        }
    },
    "editDevicemap" (item) {
        this.unblock();
        if (this.userId) {

            try {
                Meteor.settings.enableDebug ? log.debug(`editDevicemap() => start edit Devicemap`, item, Meteor.user().username) : '';
                let options = {
                    data: item,
                    headers: {
                        'content-type': 'application/json',
                        'Accept': 'application/json'
                    }
                }
                Meteor.settings.enableDebug ? log.debug(`editDevicemap() => call edit devicemap method`, item, Meteor.user().username) : '';
                const result = HTTP.call("POST", `${Meteor.settings.databusUrl}/devicemap/update`, options);
                Meteor.settings.enableDebug ? log.debug(`editDevicemap() => editDeviceMap have been called`, result, Meteor.user().username) : '';
                return true
            } catch (e) {
                log.error(`editDevicemap() => error in edit devicemap`, e, Meteor.user().username);
                return false;
            }
        }
    },
    "deleteDevicemap" (item) {
        this.unblock();
        if (this.userId) {
            try {
                Meteor.settings.enableDebug ? log.debug(`deleteDevicemap() => start delete Devicemap`, item, Meteor.user().username) : '';
                let options = {
                    data: item,
                    headers: {
                        'content-type': 'application/json',
                        'Accept': 'application/json'
                    }
                }
                Meteor.settings.enableDebug ? log.debug(`deleteDevicemap() => call delete devicemap method`, item, Meteor.user().username) : '';
                const result = HTTP.call("POST", `${Meteor.settings.databusUrl}/devicemap/delete`, options);
                Meteor.settings.enableDebug ? log.debug(`deleteDevicemap() => deleteDevicemap have been called`, result, Meteor.user().username) : '';
                return true
            } catch (e) {
                log.error(`deleteDevicemap() => error in delete devicemap`, e, Meteor.user().username);
                return false;
            }
        }
    },
});
Meteor.settings.enableDebug ? log.debug(`endof devicemap`) : '';