package org.commcare.util.screen;

import org.commcare.modern.session.SessionWrapper;
import org.commcare.session.CommCareSession;
import org.commcare.suite.model.Entry;
import org.commcare.suite.model.Menu;
import org.commcare.suite.model.MenuDisplayable;
import org.commcare.suite.model.MenuLoader;
import org.commcare.util.LoggerInterface;
import org.javarosa.core.services.Logger;

import java.io.PrintStream;

/**
 * Screen to allow users to choose items from session menus.
 *
 * @author ctsims
 */
public class MenuScreen extends Screen {

    private MenuDisplayable[] mChoices;

    private String mTitle;

    class ScreenLogger implements LoggerInterface {

        @Override
        public void logError(String message, Exception cause) {
            Logger.exception(message, cause);
        }

        @Override
        public void logError(String message) {
            Logger.log("exception", message);
        }
    }

    @Override
    public void init(SessionWrapper session) throws CommCareSessionException {
        String root = deriveMenuRoot(session);
        MenuLoader menuLoader = new MenuLoader(session.getPlatform(), session, root, new ScreenLogger(), false);
        this.mChoices = menuLoader.getMenus();
        Exception loadException = menuLoader.getLoadException();
        if (loadException != null) {
            throw new CommCareSessionException(menuLoader.getErrorMessage());
        }
    }

    @Override
    protected String getScreenTitle() {
        return mTitle;
    }

    private String deriveMenuRoot(SessionWrapper session) {
        if (session.getCommand() == null) {
            return "root";
        } else {
            return session.getCommand();
        }
    }

    @Override
    public void prompt(PrintStream out) {
        for (int i = 0; i < mChoices.length; ++i) {
            MenuDisplayable d = mChoices[i];
            out.println(i + ")" + d.getDisplayText());
        }
    }

    @Override
    public String[] getOptions() {
        String[] ret = new String[mChoices.length];
        for (int i = 0; i < mChoices.length; ++i) {
            MenuDisplayable d = mChoices[i];
            ret[i] = d.getDisplayText();
        }
        return ret;
    }

    @Override
    public boolean handleInputAndUpdateSession(CommCareSession session, String input) {
        try {
            int i = Integer.parseInt(input);
            String commandId;
            MenuDisplayable menuDisplayable = mChoices[i];
            if (menuDisplayable instanceof Entry) {
                commandId = ((Entry)menuDisplayable).getCommandId();
            } else {
                commandId = ((Menu)mChoices[i]).getId();
            }
            session.setCommand(commandId);
            return false;
        } catch (NumberFormatException e) {
            //This will result in things just executing again, which is fine.
        }
        return true;
    }
}

@@ -28,16 +28,17 @@
 creen;%0A%0A
+%0A
 import o
@@ -378,16 +378,127 @@
 .Logger;
+%0Aimport org.javarosa.core.services.locale.Localization;%0Aimport org.javarosa.core.util.NoLocalizedTextException;
 %0A%0Aimport
@@ -519,16 +519,42 @@
 Stream;%0A
+import java.util.Arrays;%0A%0A
 %0A/**%0A * 
@@ -722,35 +722,210 @@
 es;%0A
-%0A    private String mTitle;
+    private String%5B%5D badges;%0A%0A    private String mTitle;%0A%0A    public String%5B%5D getBadges() %7B%0A        return badges;%0A    %7D%0A%0A    public void setBadges(String%5B%5D badges) %7B%0A        this.badges = badges;%0A    %7D
 %0A%0A  
@@ -1538,24 +1538,116 @@
 getMenus();%0A
+        this.mTitle = this.getBestTitle();%0A        this.badges = menuLoader.getBadgeText();%0A
         Exce
@@ -1854,16 +1854,13 @@
    p
-rotected
+ublic
  Str
@@ -3309,11 +3309,437 @@
 e;%0A    %7D
+%0A%0A    public MenuDisplayable%5B%5D getMenuDisplayables()%7B%0A        return mChoices;%0A    %7D%0A%0A    private String getBestTitle()%7B%0A        try %7B%0A            return Localization.get(%22app.display.name%22);%0A        %7D catch (NoLocalizedTextException nlte) %7B%0A            return %22CommCare%22;%0A        %7D%0A    %7D%0A%0A    @Override%0A    public String toString() %7B%0A        return %22MenuScreen %22 + mTitle + %22 with choices %22 + Arrays.toString(mChoices);%0A    %7D
 %0A%7D%0A

Add badges, menu title, toString to MenuScreen