import { Meteor } from 'meteor/meteor';
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({
    'createHisLink' (item) {
        this.unblock();
        if (this.userId) {
            try {
                Meteor.settings.enableDebug ? log.debug(`createHisLink() => start create hislink`, item, Meteor.user().username) : '';
                let options = {
                    data: item,
                    headers: {
                        'content-type': 'application/json',
                        'Accept': 'application/json'
                    }
                }
                Meteor.settings.enableDebug ? log.debug(`createHisLink() => call create hislink method`, item, Meteor.user().username) : '';
                const result = HTTP.call("POST", `${Meteor.settings.databusUrl}/hislink/create`, options);
                Meteor.settings.enableDebug ? log.debug(`createHisLink() => createHisLink have been called`, result, Meteor.user().username) : '';
                return true
            } catch (e) {
                log.error(`createHisLink() => error in create HisLink`, e, Meteor.user().username);
                throw new Meteor.Error(500, "error");
            }
        }
    },
    "editHisLink" (item) {
        this.unblock();
        if (this.userId) {
            try {
                Meteor.settings.enableDebug ? log.debug(`editHisLink() => start edit hislink`, item, Meteor.user().username) : '';
                let options = {
                    data: item,
                    headers: {
                        'content-type': 'application/json',
                        'Accept': 'application/json'
                    }
                }
                Meteor.settings.enableDebug ? log.debug(`editHisLink() => call edit hislink method`, item, Meteor.user().username) : '';
                const result = HTTP.call("POST", `${Meteor.settings.databusUrl}/hislink/update`, options);
                Meteor.settings.enableDebug ? log.debug(`editHisLink() => editHisLink have been called`, result, Meteor.user().username) : '';
                return true
            } catch (e) {
                log.error(`editHisLink() => error in edit HisLink`, e, Meteor.user().username);
                throw new Meteor.Error(500, "error");
            }
        }
    },
    "deleteHisLink" (item) {
        this.unblock();
        if (this.userId) {
            Meteor.settings.enableDebug ? log.debug(`deleteHisLink() => start delete hislink`, item, Meteor.user().username) : '';
            let relationShipCount = DeviceMap.find({ HisLinkId: item._id }).count();
            Meteor.settings.enableDebug ? log.debug(`deleteHisLink() => get relationshipCount of deleted item`, relationShipCount, Meteor.user().username) : '';
            if (relationShipCount > 0) {
                Meteor.settings.enableDebug ? log.debug(`deleteHisLink() => relationShipCount is greater than zero and selected hislink can not be deleted`, { relationShipCount, ...item }, Meteor.user().username) : '';
                throw new Meteor.Error(500, "relationShipError");
            }
            try {
                let options = {
                    data: item,
                    headers: {
                        'content-type': 'application/json',
                        'Accept': 'application/json'
                    }
                }
                Meteor.settings.enableDebug ? log.debug(`deleteHisLink() => call delete hislink method`, item, Meteor.user().username) : '';
                const result = HTTP.call("POST", `${Meteor.settings.databusUrl}/hislink/delete`, options);
                Meteor.settings.enableDebug ? log.debug(`deleteHisLink() => deleteHisLink have been called`, result, Meteor.user().username) : '';
                return true
            } catch (e) {
                log.error(`deleteHisLink() => error in delete hislink`, e, Meteor.user().username)
                throw new Meteor.Error(500, "error");
            }
        }
    },
    "activeHisLink" (item) {
        this.unblock();
        if (this.userId) {
            try {
                Meteor.settings.enableDebug ? log.debug(`activeHisLink() => start active hislink`, item, Meteor.user().username) : '';
                let options = {
                    data: item,
                    headers: {
                        'content-type': 'application/json',
                        'Accept': 'application/json'
                    }
                }
                Meteor.settings.enableDebug ? log.debug(`activeHisLink() => call active hislink method`, item, Meteor.user().username) : '';
                const result = HTTP.call("POST", `${Meteor.settings.databusUrl}/hislink/active`, options);
                Meteor.settings.enableDebug ? log.debug(`activeHisLink() => activeHisLink have been called`, result, Meteor.user().username) : '';
                return true
            } catch (e) {
                log.error(`activeHisLink() => error in active hislink`, e, Meteor.user().username)
                throw new Meteor.Error(500, "error");
            }
        }
    },
    "deactiveHisLink" (item) {
        this.unblock();
        if (this.userId) {
            try {
                Meteor.settings.enableDebug ? log.debug(`deactiveHisLink() => start deactive hislink`, item, Meteor.user().username) : '';
                let options = {
                    data: item,
                    headers: {
                        'content-type': 'application/json',
                        'Accept': 'application/json'
                    }
                }
                Meteor.settings.enableDebug ? log.debug(`deactiveHisLink() => call active hislink method`, item, Meteor.user().username) : '';
                const result = HTTP.call("POST", `${Meteor.settings.databusUrl}/hislink/deactive`, options);
                Meteor.settings.enableDebug ? log.debug(`deactiveHisLink() => deactiveHisLink have been called`, result, Meteor.user().username) : '';
                return true
            } catch (e) {
                log.error(`deactiveHisLink() => error in deactive hislink`, e, Meteor.user().username)
                throw new Meteor.Error(500, "error");
            }
        }
    },
    "authenticateHisLink" (item) {
        this.unblock();
        if (this.userId) {
            try {
                Meteor.settings.enableDebug ? log.debug(`authenticateHisLink() => start authenticate hislink`, item, Meteor.user().username) : '';
                let options = {
                    data: item,
                    headers: {
                        'content-type': 'application/json',
                        'Accept': 'application/json'
                    }
                }
                Meteor.settings.enableDebug ? log.debug(`authenticateHisLink() => call active hislink method`, item, Meteor.user().username) : '';
                const result = HTTP.call("POST", `${Meteor.settings.databusUrl}/hislink/authenticate`, options);
                Meteor.settings.enableDebug ? log.debug(`authenticateHisLink() => authenticateHisLink have been called`, result, Meteor.user().username) : '';
                return result;
            } catch (e) {
                log.error(`authenticateHisLink() => error in authenticate hislink`, e, Meteor.user().username)
                throw new Meteor.Error(500, "error");
            }
        }
    }
});
Meteor.settings.enableDebug ? log.debug(`endof hislinks`) : '';