package apz.pirichat.server;
import java.io.IOException;
import apz.pirichat.shared.ChatMessage;
import apz.pirichat.shared.PiriException;

public class PiriServerData
{
    private Users mUsers;
//    private Channels mChannels;

    public void PiriServerData()
    {
        mUsers = new Users();
        mChannels = new Channels();

        mUsers.createUser("alanp", "Password");


    }

    public void dataRecieved(Connection pConnection, ChatMessage pChatMessage) throws PiriException, IOException
    {

        Command pCommand = (ChatMessage)pChatMessage;

        // See what type of message this is by looking at destination field
        // If destination begins with ! - it's a command
        // If destination begins with # - it's a channel message
        // If destination begins with % it's to a user
        String destination = pChatMessage.getDestination();
        if (destination.startsWith("!"))
            processCommand(pClientThread, pCommand);
        if (destination.startsWith("#"))
            sendChannelMessage(pClientThread, pChatMessage);
        if (destination.startsWith("%"))
            sendUserMessage(pClientThread, pChatMessage);
    }

    private void processCommand(Connection pConnection, Command pCommand) throws PiriException, IOException
    {
        // See which command to run
        String command = pCommand.getCommand();
        if (command.equals("init"))
            cmdInitialise(pClientThread, pCommand);
        if (command.equals("login"))
            cmdLogin(pClientThread, pCommand);
        if (command.equals("status"))
            cmdStatus(pClientThread, pCommand);
        if (command.equals("listUsers"))
            cmdStatus(pClientThread, pCommand);
    }

    private void sendReply(ClientThread pClientThread, Command pCommand)
    {
        pClientThread.getSocket().sendMessage(reply);
    }

    private void sendError(ClientThread pClientThread, Command pCommand, String errorCode, String pErrorMsg)
    {

    }

    private void sendUserMessage()
    {

    }

    private void sendChannelMessage()
    {

    }

    private void cmdDisconnect()
    {

    }

    private void cmdInitialise(ClientThread pClientThread) throws PiriException, IOException
    {
        // This is called by the client to get basic server information
        Command reply = new Command("init", null);
        reply.setParameter("welcome", "Welcome to PiriChat v1.0");
        reply.setParameter("serverName", "Alan's Server");
        reply.setParameter("admin", "alan@alancode.net");
        sendReply(pClientThread, reply);
    }

    private void cmdLogin(ClientThread pClientThread, Command pCommand)
    {
        // Called by client to request login
        // This command requires attributes:
        //  *   userName - Username of client
        //  *   password - Password protecting account
        if (pCommand.doesParameterExist("userName") == false)
            sendError(pClientThread, "Missing attribute: userName");
        if (pCommand.doesParameterExist("password") == false)
            sendError(pClientThread, "Missing attribute: userName");
        String userName = pCommand.getParameter("userName");
        String password = pCommand.getParameter("password");
        // Check username exists
        if (mUsers.doesUserExist(userName) == false)
            sendError(pClientThread, pCommand, "userNameNotFound", "User name not found");
        // User exists ok.. next check password
        if (getUser(userName).checkPassword(password) == false)
            sendError(pClientThread, pCommand, "passwordIncorrect", "Incorrect password");
        // User logged in
        pClientThread.setUserName(userName);
        pClientThread.setStatus(STATUS_ONLINE);
        // Send success message
        // Command to send is 'login'
        sendReply(pClientThread, new Command("loginSuccess", null));
    }

    private void listChannels(ClientThread pClientThread)
    {

    }

    private void cmdListUsers(ClientThread pClientThread, Command pCommand)
    {
        // This command returns a list of users
        User[] users = mUsers.getUsers();
    }

    private void cmdGetUserProperties(ClientThread pClientThread, Command pCommand)
    {
        // This command retrieves properties about a particular user


        User user = mUsers.getUser(userName);

        Command reply;

        reply.setParameter("displayName", user.getDisplayName());

    }



    private void cmdJoinChannel(ClientThread pClientThread, ChatMessage pChatMessage)
    {

    }


    private void sendError()
    {

    }

    private boolean isUserOnline(String pUserName)
    {

    }

    private boolean isUserModerator(String pUserName)
    {

    }

    private boolean isUserAdministrator(String pUserName)
    {

    }


}
