package ocms;

import oracle.adf.view.faces.component.core.output.CoreOutputText;

import java.net.URI;

import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;

import org.csapi.schema.parlayx.common.v2_0.*;
import org.csapi.schema.parlayx.presence.v2_0.*;
import org.csapi.wsdl.parlayx.presence.consumer.v2_0.interface_.*;

public class PresenceBean
{
  private static final String consumerEndpoint =
    "/aggregationproxy/presenceconsumerws/presenceconsumer";
    
  private CoreOutputText userNameText;
  private FacesContext ctx;
  
  private String server;
  private String port;
  private String realm;
  private String user;
  private String password;

  private static PresenceConsumerClient consumer = null;
  private static Exception exception = null;
  
  public void setServer(String server)     { this.server    = server; }
  public void setPort(String port)         { this.port     = port; }
  public void setRealm(String realm)       { this.realm    = realm; }
  public void setUser(String user)         { this.user     = user; }
  public void setPassword(String password) { this.password = password; }
   
  public void setUserNameText(CoreOutputText userNameText)
  {
    this.userNameText = userNameText;
  }

  public CoreOutputText getUserNameText()
  {
    return userNameText;
  }
  
  private boolean init()
  {
    if (consumer  != null) return true;
    if (exception != null) return false;
    try
    {
      consumer = new PresenceConsumerClient();
      consumer.setEndpoint("http://" + server + ":" + port + consumerEndpoint);
  
      consumer.setMaintainSession(true);
      consumer.setUsername(user);
      consumer.setPassword(password);
      return true;
    }
    catch (Exception e)
    {
      exception = e;
      consumer = null;
      
      ctx = FacesContext.getCurrentInstance();
      FacesMessage message =
        new FacesMessage("Error initializing presence connection", e.getMessage());
      message.setSeverity(FacesMessage.SEVERITY_FATAL);
      ctx.addMessage(null, message);
      return false;
    }
  }
  
  public String getPresence()
  {
    if (!init()) return "Exception";
    try
    {
      String userName = (String) userNameText.getValue();
      URI user =  new URI("sip:" + userName + "@" + realm);
  
      // Currently not used, but required    
      SimpleReference sr = new SimpleReference();
      sr.setCorrelator("unused_correlator");
      sr.setInterfaceName("unused_interfacename");
      sr.setEndpoint(new URI("http://unused.com"));
      consumer.subscribePresence(user, null, "unused", sr);
    
      PresenceAttribute[] pa = consumer.getUserPresence(user, null);
      AttributeTypeAndValue v = pa[0].getTypeAndValue();
      if (v.getUnionElement() == PresenceAttributeType.Activity)
        return v.getActivity().getValue();
      else
        return "Wrong presence info";
    }
    catch (PolicyException e)
    {
       return "N/A";
    }
    catch (Exception e)
    {
      exception = e;
      consumer = null;
      
      ctx = FacesContext.getCurrentInstance();
      FacesMessage message =
        new FacesMessage("Error getting presence", e.getMessage());
      message.setSeverity(FacesMessage.SEVERITY_FATAL);
      ctx.addMessage(null, message);
      
      return "Exception";
    }
  }
}