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

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

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) {


            let relationShipCount = DeviceMap.find({ HisLinkId: item._id }).count();
            if (relationShipCount > 0) {
                throw new Meteor.Error(500, "relationShipError");

            }

            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 e.reason;
            }
        }
    },
    "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");
            }
        }
    }
});