package com.mysterioustrousers.firehose;



import java.util.ArrayList;
import java.util.List;

import com.android.volley.ParseError;
import com.android.volley.Request;
import com.android.volley.Response.ErrorListener;
import com.android.volley.Response.Listener;
import com.google.gson.annotations.SerializedName;

import org.apache.commons.lang3.StringUtils;
import org.json.JSONObject;



public class Agent extends FHObject {

  private static transient Agent s_loggedInAgent = null;


  @SerializedName("access_token")
  private String _accessToken;

  @SerializedName("browser_token")
  private String _browserToken;

  @SerializedName("email")
  private String _email;

  @SerializedName("first_name")
  private String _firstName;

  @SerializedName("last_name")
  private String _lastName;

  @SerializedName("url_token")
  private String _urlToken;

  @SerializedName("display_name")
  private String _displayName;

  @SerializedName("avatar_url")
  private String _avatarURL;

  @SerializedName("agent_settings")
  private AgentSettings _agentSettings;

  @SerializedName("companies")
  private List<Company> _companies;  // TODO: Change from List to Set

  @SerializedName("devices")
  private List<Device> _devices;  // TODO: Change from List to Set



  public Agent() {
    super();

    this.setAccessToken(null);
    this.setBrowserToken(null);
    this.setEmail(null);
    this.setFirstName(null);
    this.setLastName(null);
    this.setURLToken(null);
    this.setDisplayName(null);
    this.setAvatarURL(null);
    this.setAgentSettings(new AgentSettings());
    this.setCompanies(new ArrayList<Company>());
    this.setDevices(new ArrayList<Device>());
  }



  public static GsonRequest<Agent> login(String email, String password, Listener<Agent> listener, ErrorListener errorListener) {
    String url = String.format("%s/login", EnvironmentManager.getRemoteInstance().getBaseURL(FHApplication.API));

    JSONObject jsonObject = new JSONObject();
    try {
      jsonObject.put("email", email);
      jsonObject.put("password", password);
    } catch (Exception e) {
      errorListener.onErrorResponse(new ParseError(e));
    }

    return new GsonRequest<Agent>(Request.Method.POST, url, Agent.class, jsonObject, listener, errorListener);
  }


  public String getShortName() {
    String lastInitial = (!this.getLastName().isEmpty()) ? String.format(" %s.", this.getLastName().substring(0, 1)) : StringUtils.EMPTY;
    return String.format("%s %s", this.getFirstName(), lastInitial);
  }



  // region Getters & Setters


  public static Agent getLoggedInAgent() {
    return s_loggedInAgent;
  }


  public static void setLoggedInAgent(Agent agent) {
    s_loggedInAgent = agent;
  }


  public String getAccessToken() {
    return _accessToken;
  }


  public void setAccessToken(String accessToken) {
    _accessToken = accessToken;
  }


  public String getBrowserToken() {
    return _browserToken;
  }


  public void setBrowserToken(String browserToken) {
    _browserToken = browserToken;
  }


  public String getEmail() {
    return _email;
  }


  public void setEmail(String email) {
    _email = email;
  }


  public String getFirstName() {
    return _firstName;
  }


  public void setFirstName(String firstName) {
    _firstName = firstName;
  }


  public String getLastName() {
    return _lastName;
  }


  public void setLastName(String lastName) {
    _lastName = lastName;
  }


  public String getURLToken() {
    return _urlToken;
  }


  public void setURLToken(String urlToken) {
    _urlToken = urlToken;
  }


  public String getDisplayName() {
    return _displayName;
  }


  public void setDisplayName(String displayName) {
    _displayName = displayName;
  }


  public String getAvatarURL() {
    return _avatarURL;
  }


  public void setAvatarURL(String avatarURL) {
    _avatarURL = avatarURL;
  }


  public AgentSettings getAgentSettings() {
    return _agentSettings;
  }


  public void setAgentSettings(AgentSettings agentSettings) {
    _agentSettings = agentSettings;
  }


  public List<Company> getCompanies() {
    return _companies;
  }


  public void setCompanies(List<Company> companies) {
    _companies = companies;
  }


  public List<Device> getDevices() {
    return _devices;
  }


  public void setDevices(List<Device> devices) {
    _devices = devices;
  }


  // endregion
}
@@ -61,24 +61,50 @@
 .ArrayList;%0A
+import java.util.HashMap;%0A
 import java.
@@ -2282,32 +2282,548 @@
 istener);%0A  %7D%0A%0A%0A
+  public static GsonRequest%3CAgent%3E loginWithToken(String accessToken, Listener%3CAgent%3E listener, ErrorListener errorListener) %7B%0A    String url = String.format(%22%25s/login%22, EnvironmentManager.getRemoteInstance().getBaseURL(FHApplication.API));%0A%0A    HashMap%3CString, String%3E headers = new HashMap%3CString, String%3E();%0A    headers.put(%22Authorization%22, String.format(%22Token token=%5C%22%25s%5C%22%22, accessToken));%0A%0A    return new GsonRequest%3CAgent%3E(Request.Method.POST, url, Agent.class, headers, null, listener, errorListener);%0A  %7D%0A%0A%0A
   public String 

add accesstoken login