var width = 1060;
var height = 768;

var casper = require('casper').create({
  viewportSize: {
    width: width,
    height: height
  },
  verbose: true,
  logLevel: "debug"
});

var options = casper.cli.options;
var isDebug = !!options['d'];

var rootUrl = isDebug ? 'http://localhost:5000/' : 'https://quay.io/';
var repo = isDebug ? 'complex' : 'r0';
var org = isDebug ? 'buynlarge' : 'devtable'
var orgrepo = isDebug ? 'buynlarge/orgrepo' : 'quay/testconnect2';
var buildrepo = isDebug ? 'devtable/building' : 'quay/testconnect2';

var outputDir = "screenshots/";

casper.on("remote.message", function(msg, trace) {
  this.echo("Message: " + msg, "DEBUG");
});

casper.on("page.error", function(msg, trace) {
  this.echo("Page error: " + msg, "ERROR");
  for (var i = 0; i < trace.length; i++) {
    this.echo(JSON.stringify(trace[i]), "ERROR");
  }
});

casper.start(rootUrl + 'signin', function () {
  this.wait(1000);
});

casper.thenClick('.accordion-toggle[data-target="#collapseSignin"]', function() {
  this.wait(1000);
});

casper.then(function () {
  this.fill('.form-signin', {
    'username': isDebug ? 'devtable' : 'quaydemo',
    'password': isDebug ? 'password': 'C>K98%y"_=54x"<',
  }, false);
});

casper.thenClick('.form-signin button[type=submit]', function() {
  this.waitForText('Top Repositories');
});

casper.then(function() {
  this.waitForSelector('.fa-lock');
  this.log('Generating user home screenshot.');
});

casper.then(function() {
  this.capture(outputDir + 'user-home.png');
});

casper.then(function() {
  this.log('Generating repository view screenshot.');
});

casper.thenOpen(rootUrl + 'repository/devtable/' + repo + '?tag=v2.0', function() {
  // Wait for the tree to initialize.
  this.waitForSelector('.image-tree', function() {
    // Wait for the tree's animation to finish.
    this.wait(4000);
  });
});

casper.then(function() {
  this.capture(outputDir + 'repo-view.png');
});


casper.thenClick('a[data-image="c3d710edbd3b"]', function() {
  this.waitForText('And 3048 more...', function() {
    this.capture(outputDir + 'image-view.png');
  });
});

casper.thenClick('.image-link', function() {
  this.waitForSelector('.result-count', function() {
    this.capture(outputDir + 'repo-changes.png', {
      top: 0,
      left: 0,
      width: width,
      height: height
    });
  });
});


casper.then(function() {
  this.log('Generating repository admin screenshot.');
});

casper.thenOpen(rootUrl + 'repository/devtable/' + repo + '/admin', function() {
  this.waitForSelector('.repo-access-state');
});

casper.then(function() {
  this.capture(outputDir + 'repo-admin.png');
});

casper.then(function() {
  this.log('Generating organization repo list screenshot.');
});

casper.thenOpen(rootUrl + 'repository/?namespace=' + org, function() {
  this.waitForText('Repositories');
});

casper.then(function() {
  this.capture(outputDir + 'org-repo-list.png');
});

casper.then(function() {
  this.log('Generating organization teams screenshot.');
});

casper.thenOpen(rootUrl + 'organization/' + org, function() {
  this.waitForSelector('.organization-name');
});

casper.then(function() {
  this.capture(outputDir + 'org-teams.png');
});

casper.then(function() {
  this.log('Generating organization admin screenshot.');
});

casper.thenOpen(rootUrl + 'organization/' + org + '/admin', function() {
  this.waitForSelector('#repository-usage-chart');
});

casper.then(function() {
  this.capture(outputDir + 'org-admin.png');
});

casper.then(function() {
  this.log('Generating organization logs screenshot.');
});

casper.thenClick('a[data-target="#logs"]', function() {
  this.waitForSelector('svg > g', function() {
    this.wait(1000, function() {
      this.capture(outputDir + 'org-logs.png', {
        top: 0,
        left: 0,
        width: width,
        height: height + 200
      });
    });
  });
});

casper.then(function() {
  this.log('Generating oganization repository admin screenshot.');
});

casper.thenOpen(rootUrl + 'repository/' + orgrepo + '/admin', function() {
  this.waitForText('Robot Account')
});

casper.then(function() {
  this.capture(outputDir + 'org-repo-admin.png');
});


casper.then(function() {
  this.log('Generating build history screenshot.');
});

casper.thenOpen(rootUrl + 'repository/' + buildrepo + '/build', function() {
  this.waitForText('Starting');
});

casper.then(function() {
  this.capture(outputDir + 'build-history.png');
});

casper.run();
