'use strict';

var debug = require('debug')('gitevents-milestone');
var config = require('../common/config');

var createMilestone = function(payload, event, repo, github) {
  return new Promise(function(resolve, reject) {
    github.issues.createMilestone({
      user: config.github.org,
      repo: repo,
      title: event.name,
      due_on: event.startDate,
      description: event.id
    }, function(error) {
      if (error) { return reject(new Error(error)); }
      debug('GitHub:createdMilestone - ' + repo);
      return resolve();
    });
  });
};

var getMilestones = function getMilestones(repo, eventId, github) {
  return new Promise(function(resolve, reject) {
    github.issues.getAllMilestones({
      user: config.github.org,
      repo: repo,
      state: 'open'
    }, function (error, milestones) {
      if (error) { return reject(new Error(error)); }

      if (milestones.length > 0) {
        milestones.forEach(function(milestone) {
          if (milestone.description === eventId) {
            return resolve({ 'milestone': milestone, 'repo': repo });
          }
        });
        return resolve({ 'milestone': false, 'repo': repo });
      } else {
        resolve({ 'milestone': false, 'repo': repo });
      }
    })
  });
}

module.exports = function milestone(payload, event, github) {
  return new Promise(function(resolve, reject) {
    debug('Setup Milestones');

    for (var repo in config.github.repos) {
      if (config.github.repos.hasOwnProperty(repo)) {
        getMilestones(config.github.repos[repo], event.id, github)
          .then(function gotMilestones(args) {
            var milestone = args.milestone;
            var repo = args.repo;

            if (milestone === false) {
              if (args.repo === config.github.repos.gitevent) {
                createMilestone(payload, event, args.repo, github);
              } else {
                if (args.repo !== config.github.repos.gitevent) {
                  createMilestone(payload, event, args.repo, github);
                }
              }
            } else {
              debug(milestone.id + ' already exists for ' + args.repo);
            }
        }, function(error) {
          return new Error(error);
        });
      }
    }
  });
};

@@ -134,16 +134,32 @@
 function
+ createMilestone
 (payload
@@ -414,32 +414,43 @@
 , function(error
+, milestone
 ) %7B%0A      if (er
@@ -562,16 +562,56 @@
 resolve(
+%7B 'milestone': milestone, 'repo': repo %7D
 );%0A    %7D
@@ -1328,769 +1328,1910 @@
 %0A%7D%0A%0A
-module.exports = function milestone(payload, event, github) %7B%0A  return new Promise(function(resolve, reject) %7B%0A    debug('Setup Milestones');%0A%0A    for (var repo in config.github.repos) %7B%0A      if (config.github.repos.hasOwnProperty(repo)) %7B%0A        getMilestones(config.github.repos%5Brepo%5D, event.id, github)%0A          .then(function gotMilestones(args) %7B%0A            var milestone = args.milestone;%0A            var repo = args.repo;%0A%0A            if (milestone === false) %7B%0A              if (args.repo === config.github.repos.gitevent) %7B%0A                createMilestone(payload, event, args.repo, github);%0A              %7D else %7B%0A                if (args.repo !== config.github.repos.gitevent) %7B%0A                  createMilestone(payload, event, args.repo, github
+var addMilestone = function addMilestone(issue, repo, milestone, github) %7B%0A  return new Promise(function(resolve, reject) %7B%0A    debug('GitHub:addMilestone');%0A    console.log(issue, repo, milestone, github);%0A%0A    github.issues.edit(%7B%0A      user: config.github.org,%0A      repo: repo,%0A      title: issue.title,%0A      body: issue.body,%0A      assignee: issue.assignee,%0A      state: issue.state,%0A      milestone: milestone.number,%0A      labels: issue.labels%0A    %7D, function (error, issue) %7B%0A      if (error) %7B return reject(new Error(error)); %7D%0A      debug('Milestone added to issue');%0A      return resolve(%7B 'issue': issue %7D);%0A    %7D);%0A    %0A  %7D);%0A%7D;%0A%0Amodule.exports = function milestone(payload, event, github) %7B%0A  return new Promise(function(resolve, reject) %7B%0A    debug('Setup Milestones');%0A%0A    for (var repo in config.github.repos) %7B%0A      if (config.github.repos.hasOwnProperty(repo)) %7B%0A        getMilestones(config.github.repos%5Brepo%5D, event.id, github)%0A          .then(function gotMilestones(args) %7B%0A            var milestone = args.milestone;%0A            var repo = args.repo;%0A%0A            if (milestone === false) %7B%0A              if (args.repo === config.github.repos.gitevent) %7B%0A                createMilestone(payload, event, args.repo, github)%0A                  .then(function(args) %7B%0A                    console.log(args)%0A                    console.log('adding milestone to resources')%0A                    addMilestone(issue, args.repo, args.milestone, github);%0A                  %7D);%0A              %7D else %7B%0A                if (args.repo !== config.github.repos.gitevent) %7B%0A                  createMilestone(payload, event, args.repo, github)%0A                    .then(function(args) %7B%0A                      console.log(args)%0A                      console.log('adding milestone to everything else')%0A                      addMilestone(issue, args.repo, args.milestone, github);%0A                  %7D
 );%0A 

add milestone to issue not firing