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

export const Menus = new Mongo.Collection('menus');

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

Meteor.methods({
    'add.menu' (menuName) {
        check(menuName, String);

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

        return Menus.insert({
            menuName: menuName,
            menuOwner: this.userId,
            menuComplete: false,
        });
    },
    'edit.menu' (menuId, menuName) {
        check(menuId, String);
        check(menuName, String);

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

        return Menus.update({ _id: menuId }, {
            $set: {
                menuName: menuName,
            }
        });
    },
    'delete.menu' (menuId) {
        check(menuId, String);

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

        return Menus.remove({ _id: menuId });
    },
    'markMenu.complete' (menuId) {
        check(menuId, String);

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

        return Menus.update({ _id: menuId }, {
            $set: {
                menuComplete: true,
            }
        });
    },
    'markMenu.notComplete' (menuId) {
        check(menuId, String);

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

        return Menus.update({ _id: menuId }, {
            $set: {
                menuComplete: false,
            }
        });
    }
});