const yaml = require('js-yaml');
const visitor = require('probot-visitor');
const Freeze = require('./lib/freeze');
const formatParser = require('./lib/format-parser');

/* Configuration Variables */

module.exports = robot => {
  robot.on('integration_installation.added', config);
  robot.on('issue_comment', handleFreeze);
  const visit = visitor(robot, {interval: 60 * 30 * 1000}, handleThaw);

  async function config(event) {
    const github = await robot.auth(event.payload.installation.id);
    const freeze = await forRepository(github, event.payload.repository);

    github.issues.getLabel(context.repositories_added[0]({
      name: freeze.labelName}).catch(() => {
        return github.issues.createLabel(context.repositories_added[0]({
          name: freeze.config.labelName,
          color: freeze.config.labelColor
        }));
      }));
  }

  async function handleFreeze(event, context) {
    const github = await robot.auth(event.payload.installation.id);
    const freeze = await forRepository(github, event.payload.repository);
    const comment = event.payload.comment;
    if (!context.isBot && freeze.freezable(comment)) {
      freeze.freeze(
        context,
        freeze.propsHelper(context.event.payload.comment.user.login, comment.body)
    );
    }
  }

  async function handleThaw(installation, repository) {
    const github = await robot.auth(installation.id);
    const freeze = await forRepository(github, repository);

    const frozenIssues = await github.search.issues({q:'label:' + this.labelName});
    frozenIssues.items.forEach(issue => {
      const comment = freeze.getLastFreeze(github.issues.getComments(formatParser.commentUrlToIssueRequest(issue.comments_url)));

      if (freeze.unfreezable(comment)) {
        freeze.unfreeze(issue, formatParser.propFromComment(comment));
      }
    });
  }

  async function forRepository(github, repository) {
    const owner = repository.owner.login;
    const repo = repository.name;
    const path = '.github/probot-freeze.yml';
    let config = {};

    try {
      const data = await github.repos.getContent({owner, repo, path});
      config = yaml.load(new Buffer(data.content, 'base64').toString()) || {};
    } catch (err) {
      if (err.code !== 403) {
        visit.stop(repository);
      }
    }

    config = Object.assign(config, {owner, repo, logger: robot.log});

    return new Freeze(github, config);
  }
};

@@ -161,16 +161,69 @@
 arser');
+%0Aconst githubHelper = require('./lib/github-helper');
 %0A%0A/* Con
@@ -423,10 +423,9 @@
 0 * 
-30
+5
  * 1
@@ -481,76 +481,8 @@
 ) %7B%0A
-    const github = await robot.auth(event.payload.installation.id);%0A
     
@@ -508,32 +508,40 @@
 t forRepository(
+context.
 github, event.pa
@@ -564,16 +564,24 @@
 );%0A%0A    
+context.
 github.i
@@ -687,16 +687,24 @@
  return 
+context.
 github.i
@@ -917,76 +917,8 @@
 ) %7B%0A
-    const github = await robot.auth(event.payload.installation.id);%0A
     
@@ -944,32 +944,40 @@
 t forRepository(
+context.
 github, event.pa
@@ -1046,16 +1046,41 @@
     if (
+freeze.config.perform && 
 !context
@@ -1317,62 +1317,8 @@
 ) %7B%0A
-    const github = await robot.auth(installation.id);%0A
     
@@ -1344,32 +1344,40 @@
 t forRepository(
+context.
 github, reposito
@@ -1409,24 +1409,32 @@
 ues = await 
+context.
 github.searc
@@ -1555,24 +1555,32 @@
 tLastFreeze(
+context.
 github.issue
@@ -1593,26 +1593,26 @@
 omments(
-formatPars
+githubHelp
 er.comme
@@ -2153,56 +2153,78 @@
     
-%7D catch (err) %7B%0A      if (err.code !== 403
+  config = Object.assign(config, %7Bperform:true%7D);%0A    %7D catch (err
 ) %7B%0A
-  
     
@@ -2249,24 +2249,16 @@
 itory);%0A
-      %7D%0A
     %7D%0A%0A 

move github variable into context, new in probot .5