/*
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.io.File;

import server.Player;

public class ChooseUsernameStep {

    private final LoginHandler handler;
    
    public ChooseUsernameStep(LoginHandler handler) {
        this.handler = handler;
    }
    
    public String run() throws Exception {
        while (true){
            handler.println(false, "");
            handler.println(false, "What username would you like to use ?");
            String username = handler.readLine().trim().toLowerCase();
            String errMsg = Player.checkUsername(username);
            if (errMsg != null) {
                handler.println(false, errMsg);
                continue;
            }
            File dir = new File("inactive");
            if (Player.findByUsername(username) != null || new File(dir, username).exists()) {
                handler.println(false, "Sorry, that username is already taken. Please try a different one.");
                continue;
            }
            return username;
        }
    }
    
}