/*
Copyright (C) 2005 David Green <green@couchpotato.net>
All Rights Reserved.

This file is part of Aelfengard.

Aelfengard is proprietary software. You may not redistribute it without
prior written permission from the copyright holder.
*/

package server.login;

import java.util.List;
import java.util.Set;
import java.util.TreeSet;

import server.Config;
import server.GameServer;
import server.Gender;
import server.Race;
import server.XRunnable;

import common.CommonUtils;
import common.gameevent.TableGameEvent;


public class ChooseRaceTypeStep {

    private final LoginHandler handler;
    
    public ChooseRaceTypeStep(LoginHandler handler) {
        this.handler = handler;
    }
    
    public String run(final int raceId, final Gender gender) throws Exception {
        List<TreeSet<String>> columns = CommonUtils.uncheckedCast(
            GameServer.invokeAndWait(new XRunnable() {
                public Object run() throws Exception {
                    Race race = Config.getRace(raceId);
                    if (race == null) {
                        race = Config.getUnknownRace();
                    }
                    return race.getColumns(gender);
                }
            })
        );
        if (columns.size() == 0) {
            return "a stranger";
        }
        String[][] col = new String[columns.size()][];
        int columnNumber = 0;
        int rows = 0;
        for (Set<String> column : columns) {
            col[columnNumber] = new String[column.size()];
            rows = Math.max(rows, column.size()); 
            int rowNumber = 0;
            for (String cell : column) {
                col[columnNumber][rowNumber++] = cell;
            }
            columnNumber++;
        }

        String[][] data = new String[rows][col.length];

        for (int i = 0; i < rows; i++) {
            for (int j = 0; j < col.length; j++) {
                if (i >= col[j].length) {
                    data[i][j] = "";
                }
                else {
                    data[i][j] = (i + 1) + ") " + col[j][i];
                }
            }
        }
        
        String[] headers = new String[col.length];
        for (int i = 0; i < col.length; i++) {
            headers[i] = "Column " + (i + 1);
        }
        while (true) {
            handler.send(new TableGameEvent(data, headers, null));
            StringBuffer desc = new StringBuffer();
            for (int i = 0; i < col.length; i++) {
                while (true) {
                    if (col[i].length == 1) {
                        if (desc.length() > 0) {
                            desc.append(' ');
                        }
                        desc.append(col[i][0]);
                        break;
                    }
                    handler.println(false, "");
                    handler.println(false, "Please make a selection @10Ffrom column " + (i + 1) + "@ZZZ by entering a number from the list?");
                    try {
                        int id = Integer.parseInt(handler.readLine());
                        if (id < 1 || id > col[i].length) {
                            throw new NumberFormatException();
                        }
                        if (desc.length() > 0) {
                            desc.append(' ');
                        }
                        desc.append(col[i][id - 1]);
                        break;
                    }
                    catch (NumberFormatException ex) {
                        handler.println(false, "@30FPlease enter a number from the list.");
                    }
                }
            }
            if (new ChooseYesNoStep(handler).run("You have chosen @10F" + desc.toString() + "@ZZZ. Is this correct")) {
                return desc.toString();
            }
        }
    }
    
}