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

export const TaskItems = new Mongo.Collection('taskitems');

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

Meteor.methods({
    'add.task' (taskName, assignedTo, assignedToId, taskDate, actDate) {
        check(taskName, String);
        check(assignedTo, String);
        check(taskDate, String);
        check(assignedToId, String);
        check(actDate, Date);

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

        let username;

        if (assignedTo == "self") {
            let userInfo = Meteor.users.findOne({ _id: this.userId });
            username = userInfo.profile.fullname;
            assignedToId = this.userId;
        } else {
            username = assignedTo;
        }

        return TaskItems.insert({
            taskName: taskName,
            taskDate: taskDate,
            actualDate: actDate,
            assignedTo: username,
            assignedToId: assignedToId,
            isComplete: false,
            completedOn: null,
            assignedOn: new Date(),
            assignedBy: this.userId,
        });
    },
    'edit.task' (taskId, taskName, assignedTo, taskDate) {
        check(taskId, String);
        check(taskName, String);
        check(assignedTo, String);
        check(taskDate, String);

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

        return TaskItems.update({ _id: taskId }, {
            $set: {
                taskName: taskName,
                taskDate: taskDate,
                assignedTo: assignedTo,
                changedOn: new Date(),
                changedBy: this.userId,
            }
        });
    },
    'delete.task' (taskId) {
        check(taskId, String);

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

        return TaskItems.remove({ _id: taskId });
    },
    'markTask.complete' (taskId) {
        check(taskId, String);

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

        return TaskItems.update({ _id: taskId }, {
            $set: {
                isComplete: true,
                completedOn: new Date(),
                completedBy: this.userId,
            }
        });
    },
    'markTask.notComplete' (taskId) {
        check(taskId, String);

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

        return TaskItems.update({ _id: taskId }, {
            $set: {
                isComplete: false,
                markedUncomplteOn: new Date(),
                markedUncompleteBy: this.userId,
            }
        });
    },
});