var command = require('./command')
var rimraf = require('rimraf');

function error(message) {
  throw message;
}

function configure(config) {

  var local = config.local || error('No local path set on supplied git config');
  var remote = config.remote || error('No remote repo set on supplied git config');

  var git = {};

  git.initialise = function() {
    return git.status()
      .then(git.pull, git.freshCheckout);
  };

  git.status = function() {
    var statusCommand = 'cd {local} && git status'
      .replace('{local}', local);

    return command(statusCommand);
  };

  git.freshCheckout = function() {
    var user = config.user;

    function configureUserName() {
      return git.configure('user.name', user.name);
    }

    function configureUserEmail() {
      return git.configure('user.email', user.email);
    }

    return git.clean()
      .then(git.clone)
      .then(configureUserName)
      .then(configureUserEmail);
  }

  git.clean = function() {
    return new Promise(function(accept, reject) {
      rimraf(local, accept);
    });
  }

  git.clone = function() {
    var cloneCommand = 'git clone {remote} {local}'
      .replace('{remote}', config.remote)
      .replace('{local}', config.local);

    console.log('Clone command', cloneCommand);

    return command(cloneCommand);
  }

  git.pull = function() {
    var pullCommand = 'cd {local} && git pull'
      .replace('{local}', config.local);

    console.log('Pull command', pullCommand);

    return command(pullCommand);
  }

  git.push = function() {
    var pushCommand = 'cd {local} && git push'
      .replace('{local}', config.local);

    console.log('Push command', pushCommand);

    return command(pushCommand);
  }

  git.commit = function(message) {
    if (!message) {
      return Promise.reject('No store message provided');
    }

    var commitCommand = 'cd {local} && git commit -am "{message}"'
      .replace('{local}', config.local)
      .replace('{message}', message);

    console.log('Commit command', message, commitCommand);

    return command(commitCommand);
  }

  git.configure = function(key, value) {
    var configureCommand = 'cd {local} && git config {key} "{value}"'
      .replace('{local}', local)
      .replace('{key}', key)
      .replace('{value}', value);

    return command(configureCommand);
  }

  git.check = function() {
    var remotesCommand = 'cd {local} && git remote -v'
      .replace('{local}', local);

    return command(remotesCommand).then(function(result) {
      if (result.stdout && result.stdout.indexOf(remote) !== -1) {
        accept();
      } else {
        reject('Local repo did not contain expected remote');
      }
    });
  }

  return git;
}

module.exports = configure;

@@ -468,36 +468,37 @@
 statusCommand = 
-'
+%60
 cd 
+$
 %7Blocal%7D && git s
@@ -506,42 +506,9 @@
 atus
-'%0A      .replace('%7Blocal%7D', local)
+%60
 ;%0A%0A 
@@ -1086,17 +1086,17 @@
 mmand = 
-'
+%60
 git clon
@@ -1101,107 +1101,27 @@
 one 
-%7Bremote%7D %7Blocal%7D'%0A      .replace('%7Bremote%7D', config.remote)%0A      .replace('%7Blocal%7D', config.local)
+$%7Bremote%7D $%7Blocal%7D%60
 ;%0A%0A 
@@ -1247,36 +1247,37 @@
 r pullCommand = 
-'
+%60
 cd 
+$
 %7Blocal%7D && git p
@@ -1283,49 +1283,9 @@
 pull
-'%0A      .replace('%7Blocal%7D', config.local)
+%60
 ;%0A%0A 
@@ -1408,36 +1408,37 @@
 r pushCommand = 
-'
+%60
 cd 
+$
 %7Blocal%7D && git p
@@ -1444,49 +1444,9 @@
 push
-'%0A      .replace('%7Blocal%7D', config.local)
+%60
 ;%0A%0A 
@@ -1665,36 +1665,37 @@
 commitCommand = 
-'
+%60
 cd 
+$
 %7Blocal%7D && git c
@@ -1705,16 +1705,17 @@
 it -am %22
+$
 %7Bmessage
@@ -1720,86 +1720,9 @@
 ge%7D%22
-'%0A      .replace('%7Blocal%7D', config.local)%0A      .replace('%7Bmessage%7D', message)
+%60
 ;%0A%0A 
@@ -1880,36 +1880,37 @@
 figureCommand = 
-'
+%60
 cd 
+$
 %7Blocal%7D && git c
@@ -1919,23 +1919,25 @@
 fig 
+$
 %7Bkey%7D %22
+$
 %7Bvalue%7D%22
 '%0A  
@@ -1936,104 +1936,9 @@
 ue%7D%22
-'%0A      .replace('%7Blocal%7D', local)%0A      .replace('%7Bkey%7D', key)%0A      .replace('%7Bvalue%7D', value)
+%60
 ;%0A%0A 
@@ -2035,12 +2035,13 @@
 d = 
-'
+%60
 cd 
+$
 %7Bloc
@@ -2064,43 +2064,9 @@
 e -v
-'%0A      .replace('%7Blocal%7D', local);
+%60
 %0A%0A  

Replace {var}.replace({var}, var) with `${var}`