/*
 * 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.logging.Level;
import java.util.logging.Logger;
import javax.sdp.SessionDescription;
import javax.sip.RequestEvent;
import javax.sip.header.AllowEventsHeader;
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.FromHeader;
import javax.sip.header.HeaderFactory;
import javax.sip.header.SupportedHeader;
import javax.sip.header.ToHeader;
import javax.sip.header.ViaHeader;
import javax.sip.message.MessageFactory;
import javax.sip.message.Response;
import logger.OhmLogger;

/**
 *
 * @author Jan
 */
public class SIP200Ok
{
  private Response response;
  private static final Logger lg200Ok = OhmLogger.getLogger();
  private RequestEvent requestEvent;
  private HeaderFactory headerFactory;

  public SIP200Ok(MessageFactory messageFactory, RequestEvent requestEvent,
                  ContactHeader contactHeader, HeaderFactory headerFactory)
  {
    this.requestEvent = requestEvent;
    this.headerFactory = headerFactory;
    try
    {
      response = messageFactory.createResponse(Response.OK, this.requestEvent.getRequest());
      response.addHeader((ViaHeader) this.requestEvent.getRequest().getHeader("Via"));
      response.addHeader((FromHeader) this.requestEvent.getRequest().getHeader("From"));
      response.addHeader((ToHeader) this.requestEvent.getRequest().getHeader("To"));
      response.addHeader((CallIdHeader) this.requestEvent.getRequest().getHeader("Call-ID"));
      response.addHeader((CSeqHeader) this.requestEvent.getRequest().getHeader("Cseq"));
      response.addHeader(contactHeader);
      AllowHeader allow = this.headerFactory.createAllowHeader("NOTIFY,INVITE,ACK,CANCEL,BYE,REFER,INFO,OPTIONS,MESSAGE");
      AllowEventsHeader allowevents = this.headerFactory.createAllowEventsHeader("talk,hold,refer,call-info");
      response.addHeader((CallIdHeader) this.requestEvent.getRequest().getHeader("Call-ID"));
      response.addHeader(allow);
      response.addHeader(allowevents);
      lg200Ok.info("200-OK Header erstellt");
    }
    catch (Exception ex)
    {
      lg200Ok.getLogger(SIPmodel.class.getName()).log(Level.SEVERE, null, ex);
    }
  }

  public void addSDP(SessionDescription sdp, String myTag)
  {
    try
    {
      response.removeHeader("To");
      ToHeader temptoring = (ToHeader) requestEvent.getRequest().getHeader("To");
      temptoring.setTag(myTag); //evtl. besseren Tag
      response.addHeader(temptoring); //evtl noch Ergänzungen
      SupportedHeader supp = this.headerFactory.createSupportedHeader("replace,path,timer");//nachschauen für was es gebraucht wird
      response.addHeader(supp);
      
      ContentTypeHeader content = headerFactory.createContentTypeHeader("application", "sdp");
      response.addHeader(content);
      response.setContent(sdp, content);
    }
    catch (Exception ex)
    {
      lg200Ok.getLogger(SIPmodel.class.getName()).log(Level.SEVERE, null, ex);
    }
  }
 
  /**
   * @return the response
   */
  public Response getResponse()
  {
    return response;
  }
}
