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

import DeviceMap from '../collections/devicemap.js';

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

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

Meteor.methods({
    'createClient' (item) {
        this.unblock();
        if (this.userId) {
            try {
                Meteor.settings.enableDebug ? log.debug(`createClient() => start create client`, item, Meteor.user().username) : '';
                let options = {
                    data: item,
                    headers: {
                        'content-type': 'application/json',
                        'Accept': 'application/json'
                    }
                }
                Meteor.settings.enableDebug ? log.debug(`createClient() => call create client method`, item, Meteor.user().username) : '';
                const result = HTTP.call("POST", `${Meteor.settings.databusUrl}/clients/create`, options);
                Meteor.settings.enableDebug ? log.debug(`createClient() => createClient have been called`, result, Meteor.user().username) : '';
                return true
            } catch (e) {
                log.error(`createClient() => error in create client`, e, Meteor.user().username)
                throw new Meteor.Error(500, "error");
            }
        }
    },
    "editClient" (item) {
        this.unblock();
        if (this.userId) {
            try {
                Meteor.settings.enableDebug ? log.debug(`editClient() => start edit client`, item, Meteor.user().username) : '';
                let options = {
                    data: item,
                    headers: {
                        'content-type': 'application/json',
                        'Accept': 'application/json'
                    }
                }
                Meteor.settings.enableDebug ? log.debug(`editClient() => call edit client method`, item, Meteor.user().username) : '';
                const result = HTTP.call("POST", `${Meteor.settings.databusUrl}/clients/update`, options);
                Meteor.settings.enableDebug ? log.debug(`editClient() => editClient have been called`, result, Meteor.user().username) : '';
                return true
            } catch (e) {
                log.error(`editClient() => error in edit client`, e, Meteor.user().username)
                throw new Meteor.Error(500, "error");
            }
        }
    },
    "deleteClient" (item) {
        this.unblock();

        Meteor.settings.enableDebug ? log.debug(`deleteClient() => start delete client`, item, Meteor.user().username) : '';

        let relationShipCount = DeviceMap.find({ ClientId: item._id }).count();

        Meteor.settings.enableDebug ? log.debug(`deleteClient() => get relationshipCount of deleted item`, relationShipCount, Meteor.user().username) : '';

        if (relationShipCount > 0) {
            Meteor.settings.enableDebug ? log.debug(`deleteClient() => relationShipCount is greater than zero and selected client can not be deleted`, { relationShipCount, ...item }, Meteor.user().username) : '';
            throw new Meteor.Error(500, "relationShipError");
        }

        if (this.userId) {
            try {
                let options = {
                    data: item,
                    headers: {
                        'content-type': 'application/json',
                        'Accept': 'application/json'
                    }
                }
                Meteor.settings.enableDebug ? log.debug(`deleteClient() => call delete client method`, item, Meteor.user().username) : '';
                const result = HTTP.call("POST", `${Meteor.settings.databusUrl}/clients/delete`, options);
                Meteor.settings.enableDebug ? log.debug(`deleteClient() => deleteClient have been called`, result, Meteor.user().username) : '';
                return true
            } catch (e) {
                log.error(`deleteClient() => error in delete client`, e, Meteor.user().username)
                throw new Meteor.Error(500, "error");
            }
        }
    },
});
Meteor.settings.enableDebug ? log.debug(`endof clients`) : '';