var request = require('request');
var urlgrey = require('urlgrey');
var getConfiguration = require('./getConfiguration');


// curl example
// curl -X POST -H 'Content-Type: text/lcov' -d 'SF:money.py\nFN:11,(anonymous_1)\nDA:1,1\nDA:2,1\nDA:11,1\nend_of_record' https://codecov.io/upload/v1?token=473c8c5b-10ee-4d83-86c6-bfd72a185a27&commit=743b04806ea677403aa2ff26c6bdeb85005de658&branch=master
//

var sendToCodecov = function(str, cb){
  var withTestTokenUrl = 'https://codecov.io/upload/v1?token=473c8c5b-10ee-4d83-86c6-bfd72a185a27&commit=743b04806ea677403aa2ff26c6bdeb85005de658&branch=master';
  var configuration = getConfiguration();
  console.log("configuration: ", configuration);
  var query = {
    commit : process.env.TRAVIS_COMMIT,
    travis_job_id : process.env.TRAVIS_JOB_ID,
    branch : process.env.TRAVIS_BRANCH
  };
  if (!!process.env.TRAVIS_PULL_REQUEST && process.env.TRAVIS_PULL_REQUEST !== 'false'){
    query.pull_request = process.env.TRAVIS_PULL_REQUEST;
  }

  var url = urlgrey('https://codecov.io/upload/v1').query(query).toString();

  var headers = {
    'content-type' : 'text/lcov'
  };
  var body = str;
  var options = {
    url : url,
    headers : headers,
    body : body
  };
  request.post(options, function(err, response, body){
    if (err){
      return cb(err);
    }
    if (response.statusCode !== 200){
      var error = new Error("non-success response");
      error.detail = {
        statusCode : response.statusCode,
        body : body,
        headers : response.headers
      };
      return cb(error);
    }
    return cb();
  });
};

module.exports = sendToCodecov;

@@ -719,271 +719,200 @@
 t : 
-process.env.TRAVIS_COMMIT,%0A    travis_job_id : process.env.TRAVIS_JOB_ID,%0A    branch : process.env.TRAVIS_BRANCH%0A  %7D;%0A  if (!!process.env.TRAVIS_PULL_REQUEST && process.env.TRAVIS_PULL_REQUEST !== 'false')%7B%0A    query.pull_request = process.env.TRAVIS_PULL_REQUEST
+configuration.commitId,%0A    travis_job_id : configuration.buildId,%0A    branch : configuration.branch%0A  %7D;%0A  if (!!configuration.pullRequest)%7B%0A    query.pull_request = configuration.pullRequest
 ;%0A  

use new configuration detection.