/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package model;

import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.sdp.SessionDescription;
import javax.sip.address.Address;
import javax.sip.address.AddressFactory;
import javax.sip.address.URI;
import javax.sip.header.AllowHeader;
import javax.sip.header.CSeqHeader;
import javax.sip.header.CallIdHeader;
import javax.sip.header.ContactHeader;
import javax.sip.header.ContentTypeHeader;
import javax.sip.header.ExpiresHeader;
import javax.sip.header.FromHeader;
import javax.sip.header.HeaderFactory;
import javax.sip.header.MaxForwardsHeader;
import javax.sip.header.ToHeader;
import javax.sip.header.ViaHeader;
import javax.sip.message.MessageFactory;
import javax.sip.message.Request;
import logger.OhmLogger;

/**
 *
 * @author Jan
 */
public class SIPInvite
{
  private Request inviterequest;
  private static final Logger lgInvite = OhmLogger.getLogger();

  public SIPInvite(String sipaddresse, String proxyaddresse, String myIPAddress,
                   int myPort,
                   String myTag, int branch, CallIdHeader callID, long cSequenz,
                   AddressFactory addressFactory, SessionDescription sessionDescription,
                   HeaderFactory headerFactory, MessageFactory messageFactory, ContactHeader contactHeader)
  {
    try
    {
      Address addressto = addressFactory.createAddress("sip:" + sipaddresse + "@" + proxyaddresse);
      ToHeader to = headerFactory.createToHeader(addressto, null); // Integer.toString(myTag)  address + myTag
      URI requestURI = addressto.getURI();
      FromHeader from = headerFactory.createFromHeader(addressto, String.valueOf(myTag)); //this.contactAddress
      ArrayList via = new ArrayList(); //Via needs a List as input
      ViaHeader viaheader = headerFactory.createViaHeader(myIPAddress, myPort, "UDP", String.valueOf(branch));
      via.add(viaheader);
      CSeqHeader cSeq = headerFactory.createCSeqHeader(cSequenz, "INVITE");
      MaxForwardsHeader maxForwards = headerFactory.createMaxForwardsHeader(70);
      AllowHeader allow = headerFactory.createAllowHeader("NOTIFY,INVITE,ACK,CANCEL,BYE,REFER,INFO,OPTIONS,MESSAGE");
      ExpiresHeader expire = headerFactory.createExpiresHeader(3600); //int value 
      inviterequest = messageFactory.createRequest(requestURI, "INVITE", callID, cSeq, from, to, via, maxForwards);
      inviterequest.addHeader(contactHeader);
      inviterequest.addHeader(allow);
      inviterequest.addHeader(expire);
      ContentTypeHeader content = headerFactory.createContentTypeHeader("application", "sdp");
      inviterequest.addHeader(content);
      inviterequest.setContent(sessionDescription, content);
      lgInvite.info("Invitation Header erstellt");
    }
    catch (Exception ex)
    {
      lgInvite.getLogger(SIPmodel.class.getName()).log(Level.SEVERE, null, ex);
    }

  }

  /**
   * @return the inviterequest
   */
  public Request getInviterequest()
  {
    return inviterequest;
  }
}
