var fs = require("fs");
var path = require("path");

function rootBuildGradleExists() {
  var target = path.join("platforms", "android", "build.gradle");
  return fs.existsSync(target);
}

/*
 * Helper function to read the build.gradle that sits at the root of the project
 */
function readRootBuildGradle() {
  var target = path.join("platforms", "android", "build.gradle");
  return fs.readFileSync(target, "utf-8");
}

/*
 * Added a dependency on 'com.google.gms' based on the position of the know 'com.android.tools.build' dependency in the build.gradle
 */
function addDependencies(buildGradle) {
  // find the known line to match
  var match = buildGradle.match(/^(\s*)classpath 'com.android.tools.build(.*)/m);
  var whitespace = match[1];

  // modify the line to add the necessary dependencies
  var googlePlayDependency = whitespace + 'classpath \'com.google.gms:google-services:4.1.0\' // google-services dependency from cordova-plugin-firebase';
  var fabricDependency = whitespace + 'classpath \'io.fabric.tools:gradle:1.25.4\' // fabric dependency from cordova-plugin-firebase'
  var modifiedLine = match[0] + '\n' + googlePlayDependency + '\n' + fabricDependency;

  // modify the actual line
  return buildGradle.replace(/^(\s*)classpath 'com.android.tools.build(.*)/m, modifiedLine);
}

/*
 * Add Crashlytics repos to the repository list
 */
function addRepos(buildGradle) {
  // find the known line to match
  var match = buildGradle.match(/^(\s*)jcenter\(\)/m);
  var whitespace = match[1];

  // modify the line to add the necessary repo
  var fabricMavenRepo = whitespace + 'maven { url \'https://maven.fabric.io/public\' } // Fabrics Maven repository from cordova-plugin-firebase'
  var modifiedLine = match[0] + '\n' + fabricMavenRepo;

  // modify the actual line
  return buildGradle.replace(/^(\s*)jcenter\(\)/m, modifiedLine);
}

/*
 * Helper function to write to the build.gradle that sits at the root of the project
 */
function writeRootBuildGradle(contents) {
  var target = path.join("platforms", "android", "build.gradle");
  fs.writeFileSync(target, contents);
}

module.exports = {

  modifyRootBuildGradle: function() {
    // be defensive and don't crash if the file doesn't exist
    if (!rootBuildGradleExists) {
      return;
    }

    var buildGradle = readRootBuildGradle();

    // Add Google Play Services Dependency
    buildGradle = addDependencies(buildGradle);

    // Add Google's Maven Repo
    buildGradle = addRepos(buildGradle);

    writeRootBuildGradle(buildGradle);
  },

  restoreRootBuildGradle: function() {
    // be defensive and don't crash if the file doesn't exist
    if (!rootBuildGradleExists) {
      return;
    }

    var buildGradle = readRootBuildGradle();

    // remove any lines we added
    buildGradle = buildGradle.replace(/(?:^|\r?\n)(.*)cordova-plugin-firebase*?(?=$|\r?\n)/g, '');

    writeRootBuildGradle(buildGradle);
  }
};

@@ -732,33 +732,35 @@
 ace = match%5B1%5D;%0A
+  
 %0A
-
   // modify the 
@@ -1174,17 +1174,19 @@
 ndency;%0A
+  
 %0A
-
   // mod
@@ -1315,25 +1315,18 @@
 Add 
-Crashlytics repos
+'google()'
  to 
@@ -1340,16 +1340,21 @@
 ository 
+repo 
 list%0A */
@@ -1553,16 +1553,125 @@
 ry repo%0A
+  var googlesMavenRepo = whitespace + 'google() // Google%5C's Maven repository from cordova-plugin-firebase';%0A
   var fa
@@ -1841,24 +1841,50 @@
 %5B0%5D + '%5Cn' +
+ googlesMavenRepo + '%5Cn' +
  fabricMaven
@@ -2536,25 +2536,27 @@
 ildGradle);%0A
+  
 %0A
-
     // Add G
@@ -2995,17 +2995,19 @@
 g, '');%0A
+  
 %0A
-
     writ
@@ -3045,7 +3045,6 @@
   %7D%0A
+
 %7D;
-%0A

Revert "remove google repo"