/*
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 server.Config;
import server.GameServer;
import server.Gender;
import server.Klass;
import server.Player;
import server.Race;
import server.XRunnable;

public class AccountCreationStep {

    private final LoginHandler handler;
    
    public AccountCreationStep(LoginHandler handler) {
        this.handler = handler;
    }
    
    public String run() throws Exception {
        LoginHandler.setStatus("Account creation: Initial confirmation.");
        if (!new ConfirmCreationStep(handler).run()) {
            return null; // didn't want to create an account
        }
        LoginHandler.setStatus("Account creation: Choose username.");
        handler.println(false, "");
        handler.println(false, "Please choose a username for your character. The " +
                "username you choose will never be seen by anyone " +
                "except the game administrators. It does not need " +
                "to be the same as your character name.");
        String username = new ChooseUsernameStep(handler).run();
        LoginHandler.setStatus("Account creation: Choose password.");
        final String password = new ChoosePasswordStep(handler).run();
        LoginHandler.setStatus("Account creation: Choose race.");
        final int raceId = new ChooseRaceStep(handler).run();
        LoginHandler.setStatus("Account creation: Choose gender.");
        final Gender gender = new ChooseGenderStep(handler, raceId).run();
        LoginHandler.setStatus("Account creation: Choose race type.");
        final String raceType = new ChooseRaceTypeStep(handler).run(raceId, gender);
        LoginHandler.setStatus("Account creation: Choose class.");
        final int klassId = new ChooseKlassStep(handler).run();
        // only thing that can go wrong now is that the username was taken
        // WHILE we were signing up.
        while (true) {
            final String fusername = username;
            boolean success = (Boolean) GameServer.invokeAndWait(new XRunnable() {
                public Object run() throws Exception {
                    if (Player.findByUsername(fusername) != null) {
                        return false; // can't use this username after all
                    }
                    Race race = Config.getRace(raceId);
                    if (race == null) {
                        race = Config.getUnknownRace();
                    }
                    Klass klass = Config.getKlass(klassId);
                    if (klass == null) {
                        klass = Config.getUnknownKlass();
                    }
                    new Player(fusername, password, race, klass, gender, raceType);
                    return true;
                }
            });
            if (success) {
                break;
            }
            handler.println(false, "Sorry, that username appears to be taken now. We're going to have to ask you to choose a different one.");
            LoginHandler.setStatus("Account creation: Username taken, choosing another.");
            username = new ChooseUsernameStep(handler).run();
        }

        handler.println(true, "Congratulations! Your account has been created!");
        handler.println(false, "");
        handler.println(false, "Before entering the game world for the first " +
                "time, we highly suggest that you take a quick tutorial " +
                "that will introduce you to some of our unique features. " +
                "Without taking this tutorial, the land of Aelfengard can " +
                "be a bit confusing.");
        
        LoginHandler.setStatus("Account creation: Suggesting tutorial.");
        if (new ChooseYesNoStep(handler).run("Would you like to take the tutorial (recommended)")) {
            new TutorialStep(handler).run();
            handler.println(false, "You will now enter the game. You can take the " +
                    "tutorial again at any time, by choosing " +
                    "(@10AT@ZZZ)@10Futorial@ZZZ from the login menu.");
            handler.println(false, "");
            handler.println(false, "[PRESS ENTER TO CONTINUE]");
            LoginHandler.setStatus("Account creation: Tutorial complete, final confirmation.");
            handler.readLine();
        }
        else {
            handler.println(false, "You will now enter the game. You can take the " +
                    "tutorial at any time, by choosing " +
                    "(@10AT@ZZZ)@10Futorial@ZZZ from the login menu.");
            handler.println(false, "");
            handler.println(false, "[PRESS ENTER TO CONTINUE]");
            LoginHandler.setStatus("Account creation: Tutorial declined, final confirmation.");
            handler.readLine();
        }
        
        return username;
        
    }

}