CmdUtils.CreateCommand({
  name: "mantis",
  author: { name: "Jonathan Fretin", email: "jfretin@gmail.com"},
  license: "MIT",
  icon: "http://www.mantisbt.org/favicon.ico",
  description: "Inserts a bug in Mantis Bug Tracker",
  help: "Inserts a bug in Mantis. Use it for example with a copy/paste from an email, with the mantis command.",
  takes: {"the bug": noun_arb_text},

_prepare_xml: function(the_bug) {
    summary = the_bug.summary;
    the_bug = the_bug.text;
    xml = '<soapenv:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:man="http://futureware.biz/mantisconnect" xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/">\
   <soapenv:Header/>\
   <soapenv:Body>\
      <man:mc_issue_add soapenv:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">\
         <username xsi:type="xsd:string">jfretin</username>\
         <password xsi:type="xsd:string">XXXXXXX</password>\
         <issue xsi:type="man:IssueData">\
            <view_state xsi:type="man:ObjectRef">\
               <id xsi:type="xsd:integer">10</id>\
            </view_state>\
            <project xsi:type="man:ObjectRef">\
               <id xsi:type="xsd:integer">3</id>\
            </project>\
            <category xsi:type="xsd:string">Graph</category>\
            <priority xsi:type="man:ObjectRef">\
               <id xsi:type="xsd:integer">30</id>\
            </priority>\
            <severity xsi:type="man:ObjectRef">\
               <id xsi:type="xsd:integer">60</id>\
            </severity>\
            <status xsi:type="man:ObjectRef">\
               <id xsi:type="xsd:integer">10</id>\
            </status>\
            <reporter xsi:type="man:AccountData">\
               <id xsi:type="xsd:integer">2</id>\
            </reporter>\
            <summary xsi:type="xsd:string">' + summary + '</summary>\
            <reproducibility xsi:type="man:ObjectRef">\
               <id xsi:type="xsd:integer">10</id>\
            </reproducibility>\
            <handler xsi:type="man:AccountData">\
               <id xsi:type="xsd:integer">0</id>\
            </handler>\
            <projection xsi:type="man:ObjectRef">\
               <id xsi:type="xsd:integer">10</id>\
            </projection>\
            <eta xsi:type="man:ObjectRef">\
               <id xsi:type="xsd:integer">20</id>\
            </eta>\
            <resolution xsi:type="man:ObjectRef">\
               <id xsi:type="xsd:integer">10</id>\
            </resolution>\
            <description xsi:type="xsd:string"><![CDATA[\
' + the_bug + '\
]]></description>\
         </issue>\
      </man:mc_issue_add>\
   </soapenv:Body>\
</soapenv:Envelope>';
    return xml;
  },
  

  execute: function(the_bug) {
    var _mantis_connect_uri = "http://[MANTIS_SITE]/api/soap/mantisconnect.php";
    
    jQuery.ajax( { type: "POST",
     url: _mantis_connect_uri,
     dataType: "xml",
     contentType: "text/xml;charset=UTF-8",
     data: this._prepare_xml(the_bug),
     processData: false,
     success: function(mantis_response) {
       var issue_number = jQuery("return", mantis_response).text();
       displayMessage( "Bug added in Mantis with issue number #" + issue_number + ".");
     }
    });
  }
})
