package sfs2x.client.examples;

import sfs2x.client.SmartFox;
import sfs2x.client.core.BaseEvent;
import sfs2x.client.core.SFSEvent;
import sfs2x.client.entities.Room;
import sfs2x.client.requests.JoinRoomRequest;
import sfs2x.client.requests.LoginRequest;
import sfs2x.client.util.ConfigData;


public class SFS2XConnector
{
    SmartFox sfs;
    ConfigData cfg;

    public SFS2XConnector()
    {
        // Configure client connection settings
        cfg = new ConfigData();
        cfg.setHost("localhost");
        cfg.setPort(9933);
        cfg.setZone("BasicExamples");
        cfg.setDebug(false);

        // Set up event handlers
        sfs = new SmartFox();
        sfs.addEventListener(SFSEvent.CONNECTION, this::onConnection);
        sfs.addEventListener(SFSEvent.CONNECTION_LOST, this::onConnectionLost);
        sfs.addEventListener(SFSEvent.LOGIN, this::onLogin);
        sfs.addEventListener(SFSEvent.LOGIN_ERROR, this::onLoginError);
        sfs.addEventListener(SFSEvent.ROOM_JOIN, this::onRoomJoin);

        // Connect to server
        sfs.connect(cfg);
    }

    // ----------------------------------------------------------------------
    // Event Handlers
    // ----------------------------------------------------------------------

    private void onConnection(BaseEvent evt)
    {
        boolean success = (boolean) evt.getArguments().get("success");

        if (success)
        {
            System.out.println("Connection success");
            sfs.send(new LoginRequest(""));
        }
        else
            System.out.println("Connection Failed. Is the server running?");

    }

    private void onConnectionLost(BaseEvent evt)
    {
        System.out.println("-- Connection lost --");
    }

    private void onLogin(BaseEvent evt)
    {
        System.out.println("Logged in as: " + sfs.getMySelf().getName());

        sfs.send(new JoinRoomRequest("The Lobby"));
    }

    private void onLoginError(BaseEvent evt)
    {
        String message = (String) evt.getArguments().get("errorMessage");
        System.out.println("Login failed. Cause: " + message);
    }

    private void onRoomJoin(BaseEvent evt)
    {
        Room room = (Room) evt.getArguments().get("room");
        System.out.println("Joined Room: " + room.getName());
    }

    // ----------------------------------------------------------------------

    public static void main(String[] args)
    {
        new SFS2XConnector();
    }
}
