/*
 * 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.Vector;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.sdp.SdpFactory;
import javax.sdp.SessionDescription;
import javax.sip.RequestEvent;
import logger.OhmLogger;

/**
 *
 * @author Jan
 */
public class SIPSessionDescription
{
  private SdpFactory sdpFactory;
  private SessionDescription sessionDescription;
  private Vector mediavec;
  private String codsRTP[];     //[RTP-Notation]
  private String codecs[]; //[Codec/SampelRate]
  private static final Logger lgSessionDescription = OhmLogger.getLogger();
  
  
  public SIPSessionDescription(String myName, String myIPAddress, int myRTPPort,
                               RequestEvent requestEvent, Boolean anfrage)
  {
    //Hier alle unterstützen Codecs eintragen [RTP-Notation][Codec/SampelRate]
    codsRTP = new String[4];      
    codsRTP[0]= "0";
    codsRTP[1] ="4";
    codsRTP[2]= "8";
    codsRTP[3]= "18";
    
    codecs = new String[4]; 
    codecs[0] = "PCMU/8000";
    codecs[1] = "G723/8000";
    codecs[2]= "PCMA/8000";
    codecs[3]= "G729/8000";
  
    mediavec = new Vector();
    try
    {
      sdpFactory = SdpFactory.getInstance();
      sessionDescription = sdpFactory.createSessionDescription();
      sessionDescription.setOrigin(sdpFactory.createOrigin(myName, 8000, 8000, "IN", "IP4", myIPAddress));
      sessionDescription.setSessionName(sdpFactory.createSessionName("SIP Call"));
      sessionDescription.setConnection(sdpFactory.createConnection("IN", "IP4", myIPAddress));

      //Media Bod
      if (anfrage == true)
      {
        mediavec.add(sdpFactory.createMediaDescription("audio", myRTPPort, 1, "RTP/AVP", codsRTP)); //(Übertragungstyp, Port, anzahl der Ports, Verbindungstyp,..)
        mediavec.add(sdpFactory.createAttribute("sendrecv", null));
        for (int i = 0; i < codsRTP.length; i++)
        {
          mediavec.add(sdpFactory.createAttribute("rtpmap", codsRTP[i] + " " + codecs[i]));
        }
      }
      else//Vergleich von eigenen Codecs mit Codecs der anruft -> Rückgabe entsprechend wählen
      {
        String[] reqbody = (requestEvent.getRequest()).toString().split("\\s|" + System.getProperty("line.seperator"));
        ArrayList<String> reqSDP = new ArrayList<>();
        for (String req : reqbody)
        {
          if (req.startsWith("a=rtpmap"))
          {
            reqSDP.add(req.replace("a=rtpmap:", ""));
          }
        }

        int position = 0;
        ArrayList<String> tempList = new ArrayList<>();
        for (String stcodec : codsRTP)
        {
          if (reqSDP.contains(stcodec))
          { 
            tempList.add(stcodec);
            mediavec.add(sdpFactory.createAttribute("rtpmap", codsRTP[position] + " " + codecs[position]));
          }
          position++;
        }
        String[] tempArray = tempList.toArray(new String[tempList.size()]);
        mediavec.add(sdpFactory.createMediaDescription("audio", myRTPPort, 1, "RTP/AVP", tempArray)); //(Übertragungstyp, Port, anzahl der Ports, Verbindungstyp,..)
        mediavec.add(sdpFactory.createAttribute("sendrecv", null));
      }
      sessionDescription.setAttributes(mediavec);
      lgSessionDescription.info("SDP Header erstellt");
    }
    catch (Exception ex)
    {
      lgSessionDescription.getLogger(SIPmodel.class.getName()).log(Level.SEVERE, null, ex);
    }

  }
  /**
   * @return the sessionDescription
   */
  public SessionDescription getSessionDescription()
  {
    return sessionDescription;
  }
}