package ru.ifmo.neerc.task;

/**
 * @author Evgeny Mandrikov
 */
public class TaskActions {
    public static final String TYPE_TODO = "todo";
    public static final String TYPE_CONFIRM = "confirm";
    public static final String TYPE_REASON = "okfail";
    public static final String TYPE_QUESTION = "question";

    public static final String STATUS_NEW = "none";
    public static final String STATUS_ACK = "acknowledged";
    public static final String STATUS_RUNNING = "running";
    public static final String STATUS_SUCCESS = "success";
    public static final String STATUS_FAIL = "fail";

    public static final int ACTION_START = 0;
    public static final int ACTION_DONE = 1;
    public static final int ACTION_FAIL = 2;

    public static boolean isActionSupported(Task task, String user, int action) {
        if (task == null) {
            return false;
        }
        String type = task.getType();
        TaskStatus status = task.getStatuses().get(user);
        if (status == null) {
            return false;
        }
        if (TYPE_TODO.equals(type)) {
            if (STATUS_RUNNING.equals(status.getType())) {
                return action == ACTION_DONE;
            } else {
                return action == ACTION_START;
            }
        } else if (TYPE_CONFIRM.equals(type)) {
            return action == ACTION_DONE;
        } else if (TYPE_REASON.equals(type)) {
            return action == ACTION_DONE || action == ACTION_FAIL;
        } else if (TYPE_QUESTION.equals(type)) {
            return action == ACTION_DONE;
        }
        return false;
    }

    public static String getNewStatus(Task task, String user, int action) {
        String type = task.getType();
        String status = task.getStatuses().get(user).getType();
        if (action == ACTION_START) {
            return STATUS_RUNNING;
        }
        if (action == ACTION_DONE) {
            if (type.equals(TYPE_QUESTION)) {
                return STATUS_SUCCESS;
            }
            return STATUS_SUCCESS.equals(status) ? STATUS_ACK : STATUS_SUCCESS;
        }
        if (action == ACTION_FAIL) {
            return STATUS_FAIL;
        }
        throw new IllegalArgumentException();
    }
    
    public static String getTypeByAlias(String type) {
        if ("task".equals(type)) return TYPE_TODO;
        if ("ok".equals(type)) return TYPE_CONFIRM;
        if ("reason".equals(type)) return TYPE_REASON;
        if ("q".equals(type)) return TYPE_QUESTION;
        return type;
    }
}

@@ -306,16 +306,75 @@
 estion%22;
+%0A    public static final String TYPE_TODOFAIL = %22todofail%22;
 %0A%0A    pu
@@ -1608,32 +1608,317 @@
 == ACTION_DONE;%0A
+        %7D else if (TYPE_TODOFAIL.equals(type)) %7B%0A            if (action == ACTION_FAIL)%0A                return true;%0A%0A            if (STATUS_RUNNING.equals(status.getType()))%0A                return action == ACTION_DONE;%0A            else%0A                return action == ACTION_START;%0A
         %7D%0A      

Add TODOFAIL task type