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

Meteor.methods({
    'createHisLink' (item) {

        this.unblock();
        if (this.userId) {
            try {

                let options = {
                    data: item,
                    headers: {
                        'content-type': 'application/json',
                        'Accept': 'application/json'
                    }
                }

                const result = HTTP.call("POST", `${Meteor.settings.databusUrl}/hislink/create`, options);

                return true
            } catch (e) {
                // Got a network error, timeout, or HTTP error in the 400 or 500 range.
                console.log(e);
                return false;
            }
        }
    },
    "editHisLink" (item) {
        this.unblock();
        if (this.userId) {
            try {

                let options = {
                    data: item,
                    headers: {
                        'content-type': 'application/json',
                        'Accept': 'application/json'
                    }
                }

                const result = HTTP.call("POST", `${Meteor.settings.databusUrl}/hislink/update`, options);

                return true
            } catch (e) {
                // Got a network error, timeout, or HTTP error in the 400 or 500 range.
                console.log(e);
                return false;
            }
        }
    },
    "deleteHisLink" (item) {
        this.unblock();
        if (this.userId) {
            try {
                let options = {
                    data: item,
                    headers: {
                        'content-type': 'application/json',
                        'Accept': 'application/json'
                    }
                }

                const result = HTTP.call("POST", `${Meteor.settings.databusUrl}/hislink/delete`, options);

                return true
            } catch (e) {
                // Got a network error, timeout, or HTTP error in the 400 or 500 range.
                console.log(e);
                return false;
            }
        }
    },
    "activeHisLink" (item) {
        this.unblock();
        if (this.userId) {
            try {
                let options = {
                    data: item,
                    headers: {
                        'content-type': 'application/json',
                        'Accept': 'application/json'
                    }
                }

                const result = HTTP.call("POST", `${Meteor.settings.databusUrl}/hislink/active`, options);

                return true
            } catch (e) {
                // Got a network error, timeout, or HTTP error in the 400 or 500 range.
                console.log(e);
                return false;
            }
        }
    },
    "deactiveHisLink" (item) {
        this.unblock();
        if (this.userId) {
            try {
                let options = {
                    data: item,
                    headers: {
                        'content-type': 'application/json',
                        'Accept': 'application/json'
                    }
                }

                const result = HTTP.call("POST", `${Meteor.settings.databusUrl}/hislink/deactive`, options);

                return true
            } catch (e) {
                // Got a network error, timeout, or HTTP error in the 400 or 500 range.
                console.log(e);
                return false;
            }
        }
    },
    "authenticateHisLink" (item) {
        this.unblock();
        if (this.userId) {
            try {
                let options = {
                    data: item,
                    headers: {
                        'content-type': 'application/json',
                        'Accept': 'application/json'
                    }
                }

                const result = HTTP.call("POST", `${Meteor.settings.databusUrl}/hislink/authenticate`, options);

                return result;
            } catch (e) {
                // Got a network error, timeout, or HTTP error in the 400 or 500 range.
                console.log(e);
                throw new Meteor.Error(500, "error");
            }
        }
    }
});