'use strict'

var async = require('async')
var moment = require('moment')
var parser = require('markdown-parse');
var tito = require('tito-api')

module.exports = GitEventsTito

function GitEventsTito(opts) {
  if (!(this instanceof GitEventsTito)) return new GitEventsTito(opts)
  this.t = tito({ authToken: opts.authToken, account: opts.account });
};

GitEventsTito.prototype.createEvent = function createEvent(payload, cb) {
  var that = this;

  async.waterfall([
    function(cb) { that.getLatestEvent(cb); },
    function(event, cb) { that.duplicateEvent(event, cb); },
    function(duplicatedEvent, cb) { that.updateEvent(payload, cb); }
  ], cb)
}

GitEventsTito.prototype.updateEvent = function updateEvent(payload, cb) {
  var updatedEvent;

  var titoUpdateEvent = function(err, eventDetails){
    this.t.updateEvent(eventDetails.slug, eventDetails)
      .on('data', function(data) { updatedEvent = data; })
      .on('end', function() { cb(null, updatedEvent); })
  }

  this.getEventDetails(payload, titoUpdateEvent.bind(this));
}

GitEventsTito.prototype.duplicateEvent = function duplicateEvent(event, cb) {
  var duplicatedEvent;
  this.t.duplicate(event.attributes.slug)
  .on('data', function(data) { duplicatedEvent = data; })
  .on('end', function() { cb(null, duplicatedEvent); })
}

GitEventsTito.prototype.getLatestEvent = function getLatestEvent(cb) {
  var events;
  this.t.events()
    .on('data', function (data) { events = data; })
    .on('end', function(){ cb(null, events[0]); })
}

GitEventsTito.prototype.issueIsValid = function issueIsValid(body) {
  if (!body.attributes) return false
  if (!body.attributes.name && !body.attributes.date && !body.attributes.venue && !body.attributes.address) return false
  return true
};

GitEventsTito.prototype.getEventSlug = function getEventSlug(date) {
  var m = moment(date.replace(/\//g, ' '), "DD MM YYYY");
  return m.format('MMMM').toLowerCase() + '-' + m.format('YYYY')
}

GitEventsTito.prototype.getEventDetails = function getEventDetails(payload, cb) {
  var that = this;
  parser(payload.issue.body, function(error, body) {
    if (error) { return new Error(error); }
    if (that.issueIsValid(body)) {
      var slug = that.getEventSlug(body.attributes.date)
      var startDate = moment(body.attributes.date.replace(/\//g, ' '), "DD MM YYYY").format();
      cb(null, {
        "data": {
          slug: slug,
          'start-date': startDate
        }
      })
    } else return new Error('invalid event info - body contains no attributes.');
  });
}

@@ -273,24 +273,25 @@
 o(opts)%0A
+%0A
   this.t
  = tito(
@@ -282,16 +282,19 @@
   this.t
+ito
  = tito(
@@ -349,17 +349,16 @@
 nt %7D);%0A%7D
-;
 %0A%0AGitEve
@@ -398,27 +398,16 @@
 unction 
-createEvent
 (payload
@@ -412,36 +412,36 @@
 ad, cb) %7B%0A  var 
-that
+self
  = this;%0A%0A  asyn
@@ -473,20 +473,20 @@
 n(cb) %7B 
-that
+self
 .getLate
@@ -527,20 +527,20 @@
 , cb) %7B 
-that
+self
 .duplica
@@ -598,20 +598,20 @@
 , cb) %7B 
-that
+self
 .updateE
@@ -690,27 +690,16 @@
 unction 
-updateEvent
 (payload
@@ -790,16 +790,19 @@
   this.t
+ito
 .updateE
@@ -1074,30 +1074,16 @@
 unction 
-duplicateEvent
 (event, 
@@ -1111,24 +1111,25 @@
 dEvent;%0A
+%0A
   this.t
 .duplica
@@ -1120,16 +1120,19 @@
   this.t
+ito
 .duplica
@@ -1157,16 +1157,18 @@
 s.slug)%0A
+  
   .on('d
@@ -1217,16 +1217,18 @@
 ata; %7D)%0A
+  
   .on('e
@@ -1328,30 +1328,16 @@
 unction 
-getLatestEvent
 (cb) %7B%0A 
@@ -1349,24 +1349,25 @@
 events;%0A
+%0A
   this.t
 .events(
@@ -1358,16 +1358,19 @@
   this.t
+ito
 .events(
@@ -1525,28 +1525,16 @@
 unction 
-issueIsValid
 (body) %7B
@@ -1571,16 +1571,17 @@
 n false%0A
+%0A
   if (!b
@@ -1693,16 +1693,17 @@
 n false%0A
+%0A
   return
@@ -1760,28 +1760,16 @@
 unction 
-getEventSlug
 (date) %7B
@@ -1804,33 +1804,33 @@
 ce(/%5C//g, ' '), 
-%22
+'
 DD MM YYYY%22);%0A  
@@ -1827,12 +1827,13 @@
 YYYY
-%22
+'
 );%0A
+%0A
   re
@@ -1947,31 +1947,16 @@
 unction 
-getEventDetails
 (payload
@@ -1973,49 +1973,29 @@
 var 
-that = this;%0A  parser(payload.issue.body,
+extractEventDetails =
  fun
@@ -2029,18 +2029,16 @@
  (error)
- %7B
  return 
@@ -2054,18 +2054,17 @@
 (error);
- %7D
+%0A
 %0A    if 
@@ -2066,18 +2066,18 @@
   if (th
-at
+is
 .issueIs
@@ -2110,18 +2110,18 @@
 lug = th
-at
+is
 .getEven
@@ -2243,16 +2243,17 @@
 rmat();%0A
+%0A
       cb
@@ -2273,14 +2273,12 @@
     
-%22
 data
-%22
 : %7B%0A
@@ -2361,13 +2361,13 @@
    %7D
- else
+%0A%0A   
  ret
@@ -2437,13 +2437,74 @@
 .');%0A  %7D
+%0A%0A  parser(payload.issue.body, extractEventDetails.bind(this)
 );%0A%7D%0A

refactor GitEventsTito