"use strict";

var Promise       = require('bluebird')
  , _             = require('lodash')
  , config        = require('config')
  , urljoin       = require('url-join')
  , GitHubApi     = require('github')
  ;

let hook_url = config.getUrl('git.hook_url');

class GitHub {
  constructor() {
  }

  static client(repository, options) {
    let conf      = repository.config()
      , opts      = _.extend({
                        version : '3.0.0'
                      , Promise
                    }, conf.options)
      , client    = new GitHubApi(opts)
      , { token } = options
      ;

    client.authenticate({ type : 'oauth', token });

    return { client, conf };
  }

  // try to hook / on success, update
  create_hook(repository, options) {
    return Promise
            .try(() => {
              let { conf, client }  = GitHub.client(repository, options)
                , owner             = repository.username()
                , repo              = repository.repo()
                , secret            = _.get(this.conf, 'hook.secret')
                , url               = hook_url
                ;

              let msg = {
                  owner
                , repo
                , name    : 'web'
                , events  : ['*']
                , config  : {
                      url
                    , content_type  : 'json'
                    , insecure_ssl  : '0'
                    , secret
                  }
              };

              return client.repos.createHook(msg);
            });
  }

  get_hooks(repository, options) {
    return Promise
            .try(() => {
              let { client }  = GitHub.client(repository, options)
                , owner       = repository.username()
                , repo        = repository.repo()
                ;

              let msg = {
                  owner
                , repo
                , per_page : 100
              };

              return client.repos.getHooks(msg);
            });
  }

  del_hook(repository, id, options) {
    return Promise
            .try(() => {
              let { client }  = GitHub.client(repository, options)
                , owner       = repository.username()
                , repo        = repository.repo()
                ;

              let msg = {
                  owner
                , repo
                , id
              };

              return client.repos.deleteHook(msg);
            });
  }

  clean_hooks(repository, options) {
  //   return this
  //           .get_hooks(repository, options)
  //           .then((hooks) => {
  //             // todo [akamel] hooks might be undefined.data
  //             return Promise
  //                     .map(hooks.data, (hook) => {
  //                       let { url } = hook.config;
  //
  //                       if (_.startsWith(url, 'https://www.oncue.io')
  //                         || _.startsWith(url, 'http://www.taskmill.io')
  //                         || _.startsWith(url, 'https://www.taskmill.io')
  //                         || _.startsWith(url, 'https://www.breadboard.io')
  //                         || _.startsWith(url, 'http://192.168.1.6:1337')
  //                         || _.startsWith(url, 'http://localhost:1337')
  //                       ) {
  //                         return hook;
  //                       }
  //                     })
  //                     .then((arr) => {
  //                       let rm = _.chain(arr).compact().map(i => i.id).value();
  //
  //                       return Promise.map(rm, (i) => this.del_hook(repository, i, options));
  //                     });
  //             // console.dir(hooks, { depth : 10 });
  //           });
  // }
    return Promise.resolve();
  }
}

module.exports = GitHub;

@@ -2469,47 +2469,173 @@
 %0A%0A  
-clean_hooks(repository, options) %7B%0A  //
+// todo %5Bakamel%5D use flag for hook set as well%0A  clean_hooks(repository, options) %7B%0A    if (config.has('codedb.hook.clean') && config.get('codedb.hook.clean')) %7B%0A   
    r
@@ -2647,18 +2647,17 @@
  this%0A  
-//
+ 
         
@@ -2689,26 +2689,25 @@
  options)%0A  
-//
+ 
            .
@@ -2718,34 +2718,33 @@
 ((hooks) =%3E %7B%0A  
+ 
-//
              // 
@@ -2789,18 +2789,17 @@
 .data%0A  
-//
+ 
         
@@ -2820,18 +2820,17 @@
 omise%0A  
-//
+ 
         
@@ -2865,34 +2865,33 @@
 , (hook) =%3E %7B%0A  
-//
+ 
                 
@@ -2924,25 +2924,20 @@
 config;%0A
+%0A
   
-//%0A  //
+ 
         
@@ -2991,34 +2991,33 @@
 ww.oncue.io')%0A  
-//
+ 
                 
@@ -3066,34 +3066,33 @@
 taskmill.io')%0A  
-//
+ 
                 
@@ -3142,34 +3142,33 @@
 taskmill.io')%0A  
-//
+ 
                 
@@ -3224,26 +3224,24 @@
 oard.io')%0A  
-//
             
@@ -3245,32 +3245,33 @@
                 
+ 
 %7C%7C _.startsWith(
@@ -3296,34 +3296,32 @@
 68.1.6:1337')%0A  
-//
                 
@@ -3321,32 +3321,33 @@
                 
+ 
 %7C%7C _.startsWith(
@@ -3374,26 +3374,25 @@
 st:1337')%0A  
-//
+ 
             
@@ -3404,26 +3404,25 @@
       ) %7B%0A  
-//
+ 
             
@@ -3445,26 +3445,25 @@
 urn hook;%0A  
-//
+ 
             
@@ -3477,18 +3477,17 @@
     %7D%0A  
-//
+ 
         
@@ -3504,18 +3504,17 @@
    %7D)%0A  
-//
+ 
         
@@ -3545,18 +3545,17 @@
  =%3E %7B%0A  
-//
+ 
         
@@ -3629,17 +3629,12 @@
 ();%0A
+%0A
   
-//%0A  //
+ 
     
@@ -3720,26 +3720,25 @@
 ptions));%0A  
-//
+ 
             
@@ -3748,26 +3748,25 @@
       %7D);%0A  
-//
+ 
             
@@ -3807,18 +3807,17 @@
 0 %7D);%0A  
-//
+ 
         
@@ -3829,13 +3829,13 @@
 ;%0A  
-//
+ 
  %7D%0A
+%0A
     

use flag instead of comment out code