/*
 * 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.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.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 SIPBye
{ 
  private Request byeRequest;
   private static final Logger lgBye= OhmLogger.getLogger();
  //Wichtig Unterscheidung Wer das Gespräch begonnen hat -> Unterschiedliche Header!!!
  public SIPBye(String calluser, String proxyAddress, String myName, String myIPAddress, 
                String myTag,String proxyTag, Integer myPort, String branch, CallIdHeader callID ,
                Long cSequenz, MessageFactory messageFactory, ContactHeader contactHeader,
                AddressFactory addressFactory, HeaderFactory headerFactory )
  { 
    try
    {
    Address addressLine = addressFactory.createAddress("sip:" + calluser + "@" + proxyAddress);
    URI requestURI = addressLine.getURI();

    Address addressfrom = addressFactory.createAddress("sip:" + myName + "@" + myIPAddress);
    FromHeader from = headerFactory.createFromHeader(addressfrom, myTag);

    Address addressTo = addressFactory.createAddress("sip:" + myName + "@" + proxyAddress);
    ToHeader to = headerFactory.createToHeader(addressTo, proxyTag); // Ergänzung TAG!!

    ArrayList via = new ArrayList(); //Via needs a List as input
    ViaHeader viaheader = headerFactory.createViaHeader(myIPAddress, myPort, "UDP", branch);
    via.add(viaheader);
    CSeqHeader cSeq = headerFactory.createCSeqHeader(cSequenz, "BYE");
    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 
    byeRequest = messageFactory.createRequest(requestURI, "BYE", callID, cSeq, from, to, via, maxForwards);
    byeRequest.addHeader(contactHeader);
    byeRequest.addHeader(allow);
    byeRequest.addHeader(expire);
    }
    catch (Exception ex)
    {
      lgBye.getLogger(SIPmodel.class.getName()).log(Level.SEVERE, null, ex);
    }
  }

  /**
   * @return the byeRequest
   */
  public Request getByeRequest()
  {
    return byeRequest;
  }
}
