var debug = require('debug')('drachtio:sip') ;
var merge = require('merge') ;
var only = require('only') ;
var assert = require('assert');
var parser = require('./parser') ;

exports = module.exports = SipMessage ;

function SipMessage(msg) {
  if (!(this instanceof SipMessage)) return new SipMessage(msg);

 var self = this ;

  this.headers = {} ;
  
  Object.defineProperty( this, 'type', {
    get: function() {
      if( this.method ) return 'request' ;
      else if( this.status ) return 'response' ;
    }
  }) ;

  Object.defineProperty( this, 'canFormDialog', {
    get: function() {
      return ('INVITE' === this.method || 'SUBSCRIBE' === this.method) && !this.get('to').tag ;
    }
  }) ;

  if( msg ) {
    if( msg instanceof SipMessage ) {
      merge( this, msg ) ;
    }
    else if( typeof msg === 'object' ) {
      merge( this, only( msg, 'body method version headers status reason uri') ) ;
    }
    else {
      var sm = parser.parseSipMessage( msg, true ) ;
      merge( this, only( sm, 'body method version headers status reason uri')) ;
      this.raw = msg ;
    }
   }
}

/* 
  get and set headers
*/
SipMessage.prototype.set = function( hdr, value ) {
  var self = this ;

  var hdrs = {} ;
  if( typeof hdr === 'string') hdrs[hdr] = value ;
  else merge( hdrs, hdr ) ;

  Object.keys(hdrs).forEach(function(key) {
    var name = parser.getHeaderName( key ) ;
    var newValue = hdrs[key] ;
    var v = '' ;
    if( name in self.headers ) {
      v += self.headers[name] ;
      v += ',' ;
    }
    v += newValue ;
    self.headers[name]= v;
  });

  return this ;
} ;

SipMessage.prototype.get = function( hdr ) {
  if( this.has( hdr ) ) { return this.headers[parser.getHeaderName( hdr )] ; }
} ;

SipMessage.prototype.getParsedHeader = function( hdr ) {
  var name = parser.getHeaderName( hdr ) ;
  var v =  this.headers[name] ;
  var fn = parser.getParser( hdr.toLowerCase() ) ;
  return fn( {s:v, i:0} ) ;
} ;

SipMessage.prototype.has = function( hdr ) {
  var name = parser.getHeaderName( hdr ) ;
  return name in this.headers ;
} ;

/*
SipMessage.prototype.getParsed = function( hdr ) {
  return this.headers[hdr].parsed ;
} ;
*/
SipMessage.prototype.toString = function() {
  return parser.stringifySipMessage(this) ;
} ;


@@ -510,24 +510,638 @@
     %7D%0A  %7D) ;
+%0A  Object.defineProperty( this, 'calledNumber', %7B%0A    get: function() %7B%0A      var user = this.uri.match(/sip:(.*?)@/) ;%0A      if( user && user.length %3E 1 ) %7B%0A        return user%5B1%5D.split(';')%5B0%5D ;%0A      %7D%0A      return '' ;      %0A    %7D%0A  %7D) ;%0A  Object.defineProperty( this, 'callingNumber', %7B%0A    get: function() %7B%0A      var callingNumber ;%0A      var header = this.has('p-preferred-identity') ? this.get('p-preferred-identity') : this.get('from') ;%0A      var user  = header.match(/sip:(.*?)@/) ;%0A      if( user && user.length %3E 1 ) %7B %0A        return user%5B1%5D.split(';')%5B0%5D ;%0A      %7D%0A      return '' ;   %0A    %7D%0A  %7D) ;
 %0A%0A  Object.d

add getters for callingNumber and calledNumber