package electrosphere.net.client.protocol;

import electrosphere.engine.Globals;
import electrosphere.logger.LoggerInterface;
import electrosphere.net.parser.net.message.AuthMessage;
import electrosphere.net.parser.net.message.CharacterMessage;
import electrosphere.net.parser.net.message.LoreMessage;
import electrosphere.net.template.ClientProtocolTemplate;

/**
 * The client protocol for handling auth messages
 */
public class AuthProtocol implements ClientProtocolTemplate<AuthMessage> {

    @Override
    public AuthMessage handleAsyncMessage(AuthMessage message) {
        switch(message.getMessageSubtype()){
            case AUTHREQUEST:
                //Try login
                //TODO: actually get user/pass
                Globals.clientConnection.queueOutgoingMessage(AuthMessage.constructAuthDetailsMessage(Globals.clientUsername,Globals.clientPassword));
                break;
            case AUTHSUCCESS:
                //clean password hash from memory
                Globals.clientPassword = "";
                //request playable races
                Globals.clientConnection.queueOutgoingMessage(LoreMessage.constructRequestRacesMessage());
                //request characters available to this player
                Globals.clientConnection.queueOutgoingMessage(CharacterMessage.constructRequestCharacterListMessage());
                //log that we succeeded
                LoggerInterface.loggerAuth.INFO("Successfully logged in");
                break;
            case AUTHFAILURE:
                //TODO: handle better
                LoggerInterface.loggerAuth.ERROR("Auth failure",new Exception("Auth failure"));
                break;
            //ignore stack
            case AUTHDETAILS:
                //silently ignore
                break;
        }
        return null;
    }

    @Override
    public void handleSyncMessage(AuthMessage message) {
        switch(message.getMessageSubtype()){
            case AUTHREQUEST:
            case AUTHSUCCESS:
            case AUTHFAILURE:
            case AUTHDETAILS:
                break;
        }
    }

}
