'use strict';

var commands              = require('./lib/commands');
var cordovaPath           = require('./lib/utils/cordova-path');
var getNetworkIp          = require('./lib/utils/get-network-ip');

var chalk                 = require('chalk');
var mergeTrees            = require('broccoli-merge-trees');
var Funnel                = require('broccoli-funnel');
var path                  = require('path');
var copydir               = require('copy-dir');
var fs                    = require('fs');

module.exports = {
  name: 'ember-cordova',

  config: function(/* env, baseConfig */) {
    if (this.project.targetIncludesCordova) {
      var conf = { cordova: {} };

      if (!!this.project.RELOAD_PORT) {
        //If cordova live reload, set the reload url
        var networkAddress = getNetworkIp();
        var deviceServerUrl = 'http://' + networkAddress + ':' + this.project.RELOAD_PORT;

        conf.cordova.reloadUrl = deviceServerUrl;
      }

      return conf;
    }
  },

  contentFor: function(type) {
    if (this.project.targetIncludesCordova && type === 'body') {
      return '<script src="cordova.js"></script>';
    }
  },

  includedCommands: function() {
    return commands;
  },

  treeForPublic: function(tree) {
    if (this.project.targetIncludesCordova) {
      var platform = this.project.CORDOVA_PLATFORM;

      var platformsPath = path.join(cordovaPath(this.project), 'platforms');
      var pluginsPath;

      if (platform === 'ios') {
        pluginsPath = path.join(platformsPath, 'ios', 'www');
      } else if (platform === 'android') {
        pluginsPath = path.join(platformsPath, 'android', 'assets', 'www');
      } else if (platform === 'browser') {
        pluginsPath = path.join(platformsPath, 'browser', 'www');
      }

      var files = ['cordova.js', 'cordova_plugins.js'];

      files.forEach(function (file) {
        var filePath = path.join(pluginsPath, file);
        if (!fs.existsSync(filePath)) {
          var err = new Error('ember-cordova: ' + filePath + ' did not exist. It is required for Device LiveReload to work.');
          err.stack = null;
          throw err;
        }
      });

      if (fs.existsSync(path.join(pluginsPath, 'plugins'))) {
        files.push('plugins/**');
      }

      var pluginsTree = new Funnel(this.treeGenerator(pluginsPath), {
        srcDir:  '/',
        include: files,
        destDir: '/'
      });

      this.ui.writeLine(chalk.green('ember-cordova: Device LiveReload is enabled'));

      return mergeTrees([tree, pluginsTree]);
    }

    return tree;
  },

  postBuild(outputDest) {
    if (this.project.targetIncludesCordova) {
      var cordovaOutputPath = path.join(this.project.root, './ember-cordova/cordova/www');
      var cordovaInputPath = path.join(this.project.root, outputDest);

      copydir.sync(cordovaInputPath, cordovaOutputPath);
    }
  }
};

@@ -408,57 +408,8 @@
 ');%0A
-var copydir               = require('copy-dir');%0A
 var 
@@ -447,16 +447,16 @@
 ('fs');%0A
+
 %0Amodule.
@@ -557,38 +557,32 @@
 .project.targetI
-nclude
 sCordova) %7B%0A    
@@ -611,17 +611,16 @@
 : %7B%7D %7D;%0A
-%0A
       if
@@ -982,38 +982,32 @@
 .project.targetI
-nclude
 sCordova && type
@@ -1198,38 +1198,32 @@
 .project.targetI
-nclude
 sCordova) %7B%0A    
@@ -1726,22 +1726,8 @@
  = %5B
-'cordova.js', 
 'cor
@@ -2462,24 +2462,24 @@
 e%5D);%0A    %7D%0A%0A
+
     return t
@@ -2487,312 +2487,8 @@
 ee;%0A
-  %7D,%0A%0A  postBuild(outputDest) %7B%0A    if (this.project.targetIncludesCordova) %7B%0A      var cordovaOutputPath = path.join(this.project.root, './ember-cordova/cordova/www');%0A      var cordovaInputPath = path.join(this.project.root, outputDest);%0A%0A      copydir.sync(cordovaInputPath, cordovaOutputPath);%0A    %7D%0A
   %7D%0A

use proper targetIsCordova flag, deprecatle copy