/* jshint node: true */
'use strict';

var RSVP = require('rsvp');
var fs = require('fs');
var path = require('path');
var merge = require('lodash/object/merge');
var template = require('lodash/string/template');
var minimatch = require('minimatch');
var FormData = require('form-data');

var BasePlugin = require('ember-cli-deploy-plugin');

module.exports = {
  name: 'ember-cli-deploy-rollbar',

  createDeployPlugin: function(options) {
    var DeployPlugin = BasePlugin.extend({
      name: options.name,

      defaultConfig: {
        projectName: function(context) {
          return context.project.pkg.name;
        },
        revisionKey: function(context) {
          return context.revisionData && context.revisionData.revisionKey;
        },
        distFiles: function(context) {
          return context.distFiles;
        },
        distDir: function(context) {
          return context.distDir;
        },
        rollbarConfig: {
          enabled: true,
          environment: 'production',
          captureUncaught: true
        },
        integrateRollbar: true
      },
      requiredConfig: ['accessToken', 'accessServerToken', 'minifedPrependUrl'],
    });

    return new DeployPlugin();
  },

  contentFor: function(type) {
    if (type === 'head') {
      return '<meta name="rollbar"/>';
    }
  }
};

@@ -1167,16 +1167,1598 @@
 ndUrl'%5D,
+%0A%0A      willUpload: function(context) %7B%0A        if(this.readConfig('integrateRollbar')) %7B%0A          // setup rollbarConfig%0A          var rollbarConfig = %7B%0A            accessToken: this.readConfig('accessToken'),%0A            enabled: this.readConfig('rollbarConfig').enabled,%0A            captureUncaught: this.readConfig('rollbarConfig').captureUncaught,%0A            environment: this.readConfig('rollbarConfig').environment,%0A            payload: %7B%0A              client: %7B%0A                javascript: %7B%0A                  source_map_enabled: true,%0A                  code_version: this.readConfig('revisionKey'),%0A                  guess_uncaught_frames: true%0A                %7D%0A              %7D%0A            %7D%0A          %7D;%0A%0A          // render rollbar snippet with fulfilled config%0A          var htmlSnippetPath = path.join(__dirname, 'addon', 'rollbar.html');%0A          var htmlContent = fs.readFileSync(htmlSnippetPath, 'utf-8');%0A          var snippetPath = path.join(__dirname, 'addon', 'snippet.js');%0A          var snippetContent = fs.readFileSync(snippetPath, 'utf-8');%0A%0A          var rollbarSnippet = template(htmlContent)(%7B%0A            rollbarConfig: JSON.stringify(rollbarConfig),%0A            rollbarSnippet: snippetContent%0A          %7D);%0A%0A          // replace rollbar metatag with rollbar snippet in index.html%0A          var indexPath = path.join(context.distDir, %22index.html%22);%0A          var index = fs.readFileSync(indexPath, 'utf8');%0A          var index = index.replace('%3Cmeta name=%22rollbar%22/%3E', rollbarSnippet);%0A          fs.writeFileSync(indexPath, index);%0A        %7D%0A      %7D,
 %0A    %7D);

integrate rollbar to index.html on upload