/*******************************************************************************
 * Copyright (c) 2012-2014 Codenvy, S.A.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *   Codenvy, S.A. - initial API and implementation
 *******************************************************************************/
package com.codenvy.cli.command.builtin;

import com.codenvy.cli.command.builtin.model.UserBuilderStatus;
import com.codenvy.cli.command.builtin.model.UserProject;
import com.codenvy.cli.command.builtin.model.UserRunnerStatus;
import com.codenvy.cli.command.builtin.util.ascii.AsciiArray;

import org.apache.karaf.shell.commands.Command;
import org.fusesource.jansi.Ansi;

import java.util.ArrayList;
import java.util.List;

/**
 * List command.
 * This command will list all projects used in Codenvy.
 * @author Florent Benoit
 */
@Command(scope = "codenvy", name = "list", description = "List workspaces, projects and processes")
public class ListCommand extends AbsCommand {

    /**
     * Prints the current projects per workspace
     */
    protected Object doExecute() {
        init();

        // not logged in
        if (!checkifEnabledRemotes()) {
            return null;
        }

        Ansi buffer = Ansi.ansi();


        List<UserProject> projects = getMultiRemoteCodenvy().getProjects();
        if (projects.isEmpty()) {
            buffer.a("No projects");
            System.out.println(buffer.toString());
            return null;
        }

        // Titles
        List<String> ids = new ArrayList<>();
        List<String> remotes = new ArrayList<>();
        List<String> workspaces = new ArrayList<>();
        List<String> projectNames = new ArrayList<>();
        List<String> runnerIDs = new ArrayList<>();
        List<String> builderIDs = new ArrayList<>();
        List<String> types = new ArrayList<>();
        List<String> privacies = new ArrayList<>();

        for (UserProject project : projects) {

            // get all runners and builders for this project
            List<UserRunnerStatus> runners = getMultiRemoteCodenvy().getRunners(project);
            List<UserBuilderStatus> builders = getMultiRemoteCodenvy().getBuilders(project);

            // ok, now we need to know how many lines we need
            int lines = 1;
            if (runners.size() > lines) {
                lines = runners.size();
            }
            if (builders.size() > lines) {
                lines = builders.size();
            }

            // loop on items
            for (int i = 1; i <= lines; i++) {
                // we add basic stuff only on the first line
                if (i == 1) {
                    ids.add(project.shortId());
                    remotes.add(project.getWorkspace().getRemote());
                    workspaces.add(project.getWorkspace().name());
                    projectNames.add(project.name());
                    types.add(project.getInnerProject().projectTypeId());
                    privacies.add(project.getInnerProject().visibility());
                } else {
                    // blank data
                    ids.add("");
                    remotes.add("");
                    workspaces.add("");
                    projectNames.add("");
                    types.add("");
                    privacies.add("");
                }

                // runners ?
                if (runners.isEmpty() && i == 1) {
                    runnerIDs.add("none");
                } else if (i <= runners.size()) {
                    // print runner
                    runnerIDs.add(prettyPrint(runners.get(i - 1)));
                } else {
                    runnerIDs.add("");
                }

                // builders ?
                if (builders.isEmpty() && i == 1) {
                    builderIDs.add("none");
                } else if (i <= builders.size()) {
                    // print builder
                    builderIDs.add(prettyPrint(builders.get(i - 1)));
                } else {
                    builderIDs.add("");
                }
            }

        }

        // Ascii array
        AsciiArray asciiArray = buildAsciiArray().withColumns(ids, remotes, workspaces, projectNames, types, privacies, builderIDs, runnerIDs).withTitle("ID", "Remote", "Workspace", "Project", "Type", "Privacy", "Builders", "Runners");
        System.out.println(asciiArray.toAscii());

        return null;
    }



    protected String prettyPrint(UserRunnerStatus runnerStatus) {
        StringBuilder sb = new StringBuilder(runnerStatus.shortId());
        sb.append("[");
        switch (runnerStatus.getInnerStatus().status()) {
            case RUNNING:
                sb.append("RUN");
                break;
            case CANCELLED:
                sb.append("CANCEL");
                break;
            case NEW:
                sb.append("WAIT");
                break;
            case FAILED:
                sb.append("FAIL");
                break;
            case STOPPED:
                sb.append("STOP");
                break;
        }
        sb.append("]");
        return sb.toString();
    }

    protected String prettyPrint(UserBuilderStatus builderStatus) {
        StringBuilder sb = new StringBuilder(builderStatus.shortId());
        sb.append("[");
        switch (builderStatus.getInnerStatus().status()) {
            case IN_QUEUE:
                sb.append("WAIT");
                break;
            case IN_PROGRESS:
                sb.append("RUN");
                break;
            case FAILED:
                sb.append("FAIL");
                break;
            case CANCELLED:
                sb.append("CANCEL");
                break;
            case SUCCESSFUL:
                sb.append("OK");
                break;
        }
        sb.append("]");
        return sb.toString();
    }

}

@@ -557,16 +557,53 @@
 iltin;%0A%0A
+import jline.console.ConsoleReader;%0A%0A
 import c
@@ -650,32 +650,32 @@
 rBuilderStatus;%0A
-
 import com.coden
@@ -925,16 +925,44 @@
 .Ansi;%0A%0A
+import java.io.IOException;%0A
 import j
@@ -1005,16 +1005,56 @@
 .List;%0A%0A
+import static java.lang.String.format;%0A%0A
 /**%0A * L
@@ -1396,16 +1396,35 @@
 xecute()
+ throws IOException
  %7B%0A     
@@ -1572,17 +1572,94 @@
 nsi();%0A%0A
+        new ConsoleReader().resetPromptLine(%22Retrieving projects...%22, %22%22, 0);
 %0A
-
         
@@ -1722,24 +1722,80 @@
 Projects();%0A
+        new ConsoleReader().resetPromptLine(%22%22, %22%22, 0);%0A
         if (
@@ -2364,24 +2364,47 @@
 yList%3C%3E();%0A%0A
+        int count = 0;%0A
         for 
@@ -2500,16 +2500,215 @@
 project%0A
+            count++;%0A            String percent = (count * 100) / projects.size() + %22/100%22;%0A            new ConsoleReader().resetPromptLine(%22Collecting projects data...%22, percent, percent.length());%0A
         
@@ -4769,32 +4769,87 @@
    %7D%0A%0A        %7D%0A
+        new ConsoleReader().resetPromptLine(%22%22, %22%22, 0);
 %0A        // Asci

Fix #87 : add triggering on the list command