/*
 * 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.sip.ResponseEvent;
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.FromHeader;
import javax.sip.header.HeaderFactory;
import javax.sip.header.MaxForwardsHeader;
import javax.sip.header.SupportedHeader;
import javax.sip.header.ToHeader;
import javax.sip.header.ViaHeader;
import javax.sip.message.MessageFactory;
import javax.sip.message.Request;
import logger.OhmLogger;

/**
 * Erstellt den Trying Header, wird als erstes gesendet sobald man einen Anruf bekommt
 * @author Jan
 */
public class SIPACK
{
  private Request ackRequest;
  private static final Logger lgTry = OhmLogger.getLogger();

  public SIPACK(MessageFactory messageFactory, ResponseEvent responseEvent, AddressFactory addressFactory, String sipaddresse, String proxyaddresse, String myname,
                      HeaderFactory headerFactory, long cSeq )
  {
    try
    { //
      ArrayList via = new ArrayList(); 
      ViaHeader viaheader = (ViaHeader) responseEvent.getResponse().getHeader("Via");
      via.add(viaheader);
      FromHeader from = (FromHeader) responseEvent.getResponse().getHeader("From");
      ToHeader to = (ToHeader) responseEvent.getResponse().getHeader("To");
      CallIdHeader callID = (CallIdHeader) responseEvent.getResponse().getHeader("Call-ID");
      MaxForwardsHeader maxForwardsHeader = headerFactory.createMaxForwardsHeader(70);
      
      Address addressto = addressFactory.createAddress("sip:" + myname+ "@" + proxyaddresse);
      URI requestURI = addressto.getURI();
      CSeqHeader cSeqHeader = headerFactory.createCSeqHeader(cSeq, "ACK");
      
      SupportedHeader supp = headerFactory.createSupportedHeader("replace,path,timer"); //nachschauen
      AllowHeader allow = headerFactory.createAllowHeader("NOTIFY,INVITE,ACK,CANCEL,BYE,REFER,INFO,OPTIONS,MESSAGE");
      //ackRequest.addHeader(supp);
      //ackRequest.addHeader(allow);
      
      ackRequest = messageFactory.createRequest(requestURI, "ACK", callID, cSeqHeader, from, to, via, maxForwardsHeader);
      lgTry.info("100-Trying Header erstellt");
    }
    catch (Exception ex)
    {
      lgTry.getLogger(SIPmodel.class.getName()).log(Level.SEVERE, null, ex);
    }

  }

  /**
   * @return the ackRequest
   */
  public Request getAckRequest()
  {
    return ackRequest;
  }

  /**
   * @return the ackRequest
   */
 
}
