Evader.sessionId = Math.floor(Math.random() * 1000000);

$(function() {
  var activateStep = function(step) {
    $('.step_content').hide();
    $('.' + step + '_content').show();
    $('.steps a').removeClass('active');
    $('.steps [data-step="' + step + '"] a').addClass('active');
  };

  $('.step a').click(function(e) {
    e.preventDefault();
    var step = $(this).parent().attr('data-step');
    activateStep(step);
  });

  $('.attack_options input').change(function() {
    var $currentOption = $('.attack_options input:checked');
    $('.attack_options .description .name').text($currentOption.attr('data-guiname') + ': ');
    $('.attack_options .description .text').text($currentOption.attr('data-description'));
  });

  $('.attacks input').change(function() {
    $('.attack_options').hide();
    var chosenAttack = $(this).val();
    $('.attack_options_for_' + chosenAttack).show();
    $('.attack_options_for_' + chosenAttack + ' [name="attack_payload"]:first').attr('checked', 'checked').trigger('change');
    $('.descriptions div').hide();
    $('.descriptions .description_for_' + chosenAttack).show();
  });
  
  $('.gateway_needed').change(function() {
    if($(this).val() === 'yes') {
      $('#netconfig_gateway').removeAttr('disabled');
    } else {
      $('#netconfig_gateway').attr('disabled', 'disabled').val('');
    }
  });

  $('input[type="submit"]').click(function(e) {
    e.preventDefault();
    var $button = $(this);

    if($button.val() === 'EXECUTE') {
      Evader.sessionId = Math.floor(Math.random() * 1000000);

      var $form = $button.parents('form');

      if(Evader.validate($form)) {
        window.CloseShell(function() {
          setTimeout(function() {
            $.get('/exploit/stop', function() {
              $.ajax({
                type: 'POST',
                url: $form.attr('action'),
                data: $form.serialize(),
                success: function() {
                  Evader.startLogging();
                  $button.val('STOP');
                }
              });
            });        
          }, 1000);
        });
      } else {
        activateStep('step_2');
      }
    } else if($button.val() === 'STOP') {
      $.get('/exploit/stop');
      $button.val('EXECUTE');
    } else if($button.val() === 'LOG') {
      var $log = $('.log');
      $.colorbox({
        inline: true,
        title: 'Log',
        href: $log,
        width: '750px',
        onComplete: function() {
          var scrollHeight = $log.prop('scrollHeight');
          if(scrollHeight > 0) {
            $log.scrollTop(scrollHeight);
          }
        },
        onCleanup: function() {
          if($('.log').html().match(/Close shell/)) {
            window.CloseShell();
          }
        }
      });
    } else if($button.val() === 'DOWNLOAD REPORT') {
      var data = {
        log: $('.log').html(),
        duration: parseInt($('.duration').html(), 10),
        breaches: _.map($('.breaches_list li'), function(breach) {
          return $(breach).text();
        })
      };

      $.ajax({
        type: 'POST',
        url: '/exploit/report',
        data: JSON.stringify(data),
        dataType: 'json',
        contentType: 'application/json',
        success: function(response) {
          window.open(response.url);
        }
      });
    }
  });  
});