import { Meteor } from 'meteor/meteor';
import { Mongo } from 'meteor/mongo';
import { check } from 'meteor/check';

export const MenuItems = new Mongo.Collection('menuitems');

MenuItems.allow({
    insert: function(userId, doc){
        // if use id exists, allow insert
        return !!userId;
    },
});

Meteor.methods({
    'add.menuItem' (itemName, serveDate, menuId) {
        check(itemName, String);
        check(serveDate, String);
        check(menuId, String);

        if (!this.userId) {
            throw new Meteor.Error('You are not allowed to add items. Make sure you are logged in with valid user credentials.');
        }

        serveDateActual = new Date(serveDate);

        return MenuItems.insert({
            itemName: itemName,
            serveDate: serveDate,
            serveDateActual: serveDateActual,
            menuId: menuId,
            addedBy: this.userId,
            itemMade: false,
            dateAddedtoMenu: new Date(),
        });
    },
    'setMade.menuItem' (itemId) {
        check(itemId, String);

        if (!this.userId) {
            throw new Meteor.Error('You are not allowed to set items as made. Make sure you are logged in with valid user credentials.');
        }

        return MenuItems.update({ _id: itemId }, {
            $set: {
                itemMade: true,
                dateMade: new Date(),
            }
        });
    },
    'setAllMade.menuItem' (menuId) {
        check(menuId, String);

        if (!this.userId) {
            throw new Meteor.Error('You are not allowed to set all items as made. Make sure you are logged in with valid user credentials.');
        }

        return MenuItems.update({ menuId: menuId }, {
            $set: {
                itemMade: true,
            }
        }, {
            multi: true
        });
    },
    'setNotMade.menuItem' (itemId) {
        check(itemId, String);

        if (!this.userId) {
            throw new Meteor.Error('You are not allowed to set items as not made. Make sure you are logged in with valid user credentials.');
        }

        return MenuItems.update({ _id: itemId }, {
            $set: {
                itemMade: false,
                dateUnMade: new Date(),
            }
        });
    },
    'edit.madeItem' (itemId, itemName, serveDate) {
        check(itemId, String);
        check(itemName, String);
        check(serveDate, String);

        if (!this.userId) {
            throw new Meteor.Error('You are not allowed to edit menu items. Make sure you are logged in with valid user credentials.');
        }

        return MenuItems.update({ _id: itemId }, {
            $set: {
                itemName: itemName,
                serveDate: serveDate,
            }
        });
    },
    'delete.menuItem' (itemId) {
        check(itemId, String);

        if (!this.userId) {
            throw new Meteor.Error('You are not allowed to delete menu items. Make sure you are logged in with valid user credentials.');
        }

        return MenuItems.remove({ _id: itemId });
    },
    'shiftDate' (itemId, momentAddDay) {
        check(itemId, String);
        check(momentAddDay, String);

        if (!this.userId) {
            throw new Meteor.Error('You are not allowed to shift menu item dates. Make sure you are logged in with valid user credentials.');
        }

        return MenuItems.update({ _id: itemId }, {
            $set: {
                serveDate: momentAddDay,
            }
        });
    }
});