package basics.application.user;

import java.util.Map;
import basics.collections.TypedValueMap;
import basics.utl.Empty;
import basics.utl.SUtl;

/**
 * This is a user abstraction for authentification.
 * @author ks
 */
public class AppUser implements User
{
public static final String ID   = "Id";
public static final String NAME = "Name";
public static final String PWD  = "Pwd";
public static final String ROLE = "Role";
private TypedValueMap      map  = new TypedValueMap();

/**
 * sets properties, using a map as input
 */
@SuppressWarnings("unchecked")
public void addProperties(Map properties)
{
   this.map.putAll(properties);
}

/**
 * gets all properties as a map
 */
@SuppressWarnings("unchecked")
public Map getProperties()
{
   return this.map.getAsMap();
}

public TypedValueMap getTypedMap()
{
   return map;
}

/**
 * checks the password
 */
public boolean hasPwd(String test)
{
   if(Empty.is(test))
      return false;
   if(Empty.is(map.get(PWD, "")))
      return false;
   return map.get(PWD, "").equals(test);
}

/**
 * returns the id of the user
 */
public String getId()
{
   return map.get(ID, "");
}

/**
 * 
 */
public void setId(String id)
{
   map.put(ID, id);
}

public String getName()
{
   return map.get(NAME, "Unknown");
}

public void setName(String name)
{
   map.put(NAME, name);
}

public String getPwd()
{
   return map.get(PWD, "");
}

/*
 * (non-Javadoc)
 * @see utl.misc.AbstractUser#setPwd(java.lang.String)
 */
public void setPwd(String pwd)
{
   map.put(PWD, pwd);
}

public String getRole()
{
   return map.get(PWD, "");
}

public void setRole(String role)
{
   map.put(ROLE, role);
}

public String toString()
{
   return SUtl.toString(map.getAsMap(), PWD, true);
}

public static User create(String id, String name, String pwd, String role)
{
   User u = new AppUser();
   u.setId(id);
   u.setName(name);
   u.setPwd(pwd);
   u.setRole(role);
   return u;
}

@SuppressWarnings("unchecked")
public static User create(String id, String name, String pwd, String role, Map map)
{
   User u = new AppUser();
   u.setId(id);
   u.setName(name);
   u.setPwd(pwd);
   u.setRole(role);
   u.addProperties(map);
   return u;
}
}
