package it.gotoandplay.smartfoxclient.data;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * The User class stores the properties of each user.
 * <p>
 * This class is used internally by the {@link it.gotoandplay.smartfoxclient.SmartFoxClient} class;
 * also, User objects are returned by various methods and events of the SmartFoxServer API.
 * </p>
 * 
 * <p>
 * <b>NOTE</b>: in the provided examples, {@code user} always indicates a User instance.
 * </p>
 * 
 * @since 1.0.0
 * 
 * @author The gotoAndPlay() Team<br>
 *         <a href="http://www.smartfoxserver.com">http://www.smartfoxserver.com</a><br>
 *         <a href="http://www.gotoandplay.it">http://www.gotoandplay.it</a><br>
 */
public class User
{
    private int id;
    private String name;
    private Map<String, SFSVariable> variables;
    private boolean isSpec;
    private boolean isMod;
    private int pId;

    /*
     * User contructor.
     * 
     * @param id the user id.
     * @param name the user name.
     */		
    public User(int id, String name)
    {
        this.id = id;
        this.name = name;
        this.variables = new ConcurrentHashMap<String, SFSVariable>();
        this.isSpec = false;
        this.isMod = false;
    }

    /**
     * Get the id of the user.
     * 
     * @return The user id.
     * 
     * <p>
     * Example
     * </p>
     * <pre>
     * System.out.println("User id:" + user.getId());
     * </pre>
     * 
     * @see #getName
     * 
     * @since SmartFoxServer Basic / Pro v1.x.x
     */
    public int getId()
    {
        return this.id;
    }

    /**
     * Get the name of the user.
     * 
     * @return The user name.
     * 
     * <p>
     * Example
     * </p>
     * <pre>
     * System.out.println("User name:" + user.getName());
     * </pre>
     * 
     * @see #getId
     * 
     * @since SmartFoxServer Basic / Pro v1.x.x
     */
    public String getName()
    {
        return this.name;
    }

    /**
     * Retrieve a User Variable.
     * 
     * @param varName the name of the variable to retrieve.
     * 
     * @return The User Variable.
     * 
     * <p>
     * Example
     * </p>
     * <pre>
     * int age = Integer.parseInt(room.getVariable("age").getValue());
     * </pre>
     * 
     * @see #getVariables
     * @see SFSVariable
     * @see it.gotoandplay.smartfoxclient.SmartFoxClient#setUserVariables
     * 
     * @since SmartFoxServer Basic / Pro v1.x.x
     */
    public SFSVariable getVariable(String varName)
    {
        return this.variables.get(varName);
    }

    /**
     * Retrieve all User Variables.
     * 
     * @return A {@code Map} containing User Variables, where the key is the variable name.
     * 
     * <p>
     * Example
     * </p>
     * <pre>
     * Map&lt;String, SFSVariable&gt; userVars = user.getVariables();
     * 			
     * for(String varName : userVars.keySet())
     * {
     *     System.out.println("Name: " + varName + " | Value: " + userVars.get(varName));
     * }
     * </pre>
     * 
     * @see #getVariable
     * @see it.gotoandplay.smartfoxclient.SmartFoxClient#setUserVariables
     * 
     * @since SmartFoxServer Basic / Pro v1.x.x
     */
    public Map<String, SFSVariable> getVariables()
    {
        return this.variables;
    }

    /*
     * Set the User Variabless.
     * 
     * @param vars a Map containing user variables.
     */
    public void setVariables(Map<String, SFSVariable> vars)
    {
        for(String key : vars.keySet())
        {
            SFSVariable v = vars.get(key);
            if(v != null)
            {
                this.variables.put(key, v);
            }
            else
            {
                this.variables.remove(key);
            }
        } 
    }

    /*
     * Reset User Variabless.
     */	
    public void clearVariables()
    {
            this.variables = new ConcurrentHashMap<String, SFSVariable>();
    }

    /*
     * Set the {@link #isSpectator} property.
     * 
     * @param b {@code true} if the user is a spectator.
     */
    public void setIsSpectator(boolean b)
    {
        this.isSpec = b;
    }

    /**
     * A boolean flag indicating if the user is a spectator in the current room.
     * 
     * @return {@code true} if the user is a spectator.
     * 
     * <p>
     * Example
     * </p>
     * <pre>
     * if (user.isSpectator())
     * {
     *     System.out.println("The user is a spectator");
     * }
     * </pre>
     * 
     * @since SmartFoxServer Basic / Pro v1.x.x
     */	
    public boolean isSpectator()
    {
        return this.isSpec;
    }

    /*
     * Set the {@link #isModerator} property.
     * 
     * @param b {@code true} if the user is a Moderator.
     */	
    public void setModerator(boolean b)
    {
        this.isMod = b;
    }

    /**
     * A boolean flag indicating if the user is a Moderator in the current zone.
     * 
     * @return {@code true} if the user is a Moderator.
     * 
     * <p>
     * Example
     * </p>
     * <pre>
     * if (user.isModerator())
     * {
     *     System.out.println("The user is a Moderator");
     * }
     * </pre>
     * 
     * @since SmartFoxServer Basic / Pro v1.x.x
     */	
    public boolean isModerator()
    {
        return this.isMod;
    }

    /**
     * Retrieve the player id of the user.
     * The user must be a player inside a game room for this method to work properly.
     * This id is 1-based (player 1, player 2, etc.), but if the user is a spectator its value is -1.
     * 
     * @return The current player id.
     * 
     * <p>
     * Example
     * </p>
     * <pre>
     * System.out.println("The user's player id is " + user.getPlayerId());
     * </pre>
     * 
     * @since SmartFoxServer Basic / Pro v1.x.x
     */	
    public int getPlayerId()
    {
        return this.pId;
    }

    /*
     * Set the playerId property.
     * 
     * @param pid the playerId value.
     */
    public void setPlayerId(int pid)
    {
        this.pId = pid;
    }
}
