/*
 * 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.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.sip.RequestEvent;
import javax.sip.header.CSeqHeader;
import javax.sip.header.CallIdHeader;
import javax.sip.header.ContactHeader;
import javax.sip.header.FromHeader;
import javax.sip.header.HeaderFactory;
import javax.sip.header.ToHeader;
import javax.sip.header.ViaHeader;
import javax.sip.message.MessageFactory;
import javax.sip.message.Response;
import logger.OhmLogger;
/**
 * Erstellt den Ringing Header, wird als zweites gesendet sobald man einen Anruf bekommt
 * Wiederholen bis User das Telefon abnimmt oder nichtmehr angerufen wird
 * 
 * @author Jan
 */
public class SIP180Ringing implements Runnable
{
  private RequestEvent requestEvent;
  private ExecutorService threadring; //für wiederholtes senden des Ringing Headers
  private boolean callIncoming;
  private Response responseRing;
  private MessageFactory messageFactory;
  private HeaderFactory headerFactory;
  private String myTag;
  private String proxyTag;
  
  public static final Logger lgRing = OhmLogger.getLogger();
  //private SubmissionPublisher<Integer> publisherZahl;

  public SIP180Ringing(RequestEvent requestEvent, String myTag, String proxyTag, ContactHeader contact, HeaderFactory headerFactory)
  {
    this.requestEvent = requestEvent;
    this.headerFactory = headerFactory;
    threadring = Executors.newSingleThreadExecutor();  
    try
    {
      responseRing = this.messageFactory.createResponse(180, this.requestEvent.getRequest());
      responseRing.addHeader((ViaHeader) this.requestEvent.getRequest().getHeader("Via"));
      responseRing.addHeader((FromHeader) this.requestEvent.getRequest().getHeader("From"));
      //persönlichen Tag muss beim To-Header ergänzt werden
      ToHeader tempTo = ((ToHeader) this.requestEvent.getRequest().getHeader("To")); 
      tempTo.setTag(myTag);
      responseRing.addHeader(tempTo);
      responseRing.addHeader((CallIdHeader) requestEvent.getRequest().getHeader("Call-ID"));
      responseRing.addHeader((CSeqHeader) this.requestEvent.getRequest().getHeader("Cseq"));
      responseRing.addHeader(contact);
      responseRing.addHeader(headerFactory.createSupportedHeader("replace,path,timer"));
      responseRing.addHeader(this.headerFactory.createAllowHeader("NOTIFY,INVITE,ACK,CANCEL,BYE,REFER,INFO,OPTIONS,MESSAGE"));
      responseRing.addHeader(this.headerFactory.createAllowEventsHeader("talk,hold,refer")); //für was wird er gebraucht
      lgRing.info("180-Ringing Header erstellt");
    }
    catch (Exception ex)
    {
      lgRing.getLogger(SIPmodel.class.getName()).log(Level.SEVERE, null, ex);
    }
  }

  public void start()
  {
    callIncoming = true;
  }

  public void stop()
  {
    callIncoming = false;
  }

  @Override
  public void run()
  {
    while (callIncoming)
    {
    }
  }
  /**
   * @return the responseRing
   */
  public Response getResponseRing()
  {
    return responseRing;
  }
}
