<!DOCTYPE html>
<html i18n-values="dir:textdirection;">
<head>
<meta charset=UTF-8">
<title i18n-content="title"></title>
<style>
body {
  -webkit-user-select: none;
}

h1 {
  border-bottom: 1px solid #7289E2;
  padding: 8px;
  height: 32px;
  background-image: -webkit-linear-gradient(#D0DAF8, #A6BAF7);
  border: 1px solid #999;
  border-top: 0;
  color: black;
}

html[dir='rtl'] h1 {
  left: auto;
  right: 0;
  background-image: -webkit-linear-gradient(#D0DAF8, #A6BAF7);
}

.rowlink {
  height: 100%;
  -webkit-margin-end: 2px;
}

.devicerow div.icon {
  -webkit-margin-start: 5px;
  margin-top: -3px;
  float: left;
  width: 17px;
  height: 17px;
  background-repeat: no-repeat;
  background: url('shared/images/icon_folder.png');
}

html[dir='rtl'] .devicerow div.icon {
  float: right;
}

.burnicon {
  background:  url('shared/images/filebrowse_menu.png');
  position: absolute;
  right: 4px;
  top: 5px;
  height: 15px;
  width: 15px;
  margin-top: 5px;
  background-repeat: no-repeat;
  cursor: pointer;
  opacity: 0.3;
  -webkit-transition: opacity 0.15s ease-out;
  border: 0;
}

html[dir='rtl'].burnicon {
  right: auto;
  left: 4px;
}

.devicerow:hover .burnicon {
  opacity: 0.75;
  -webkit-transition: none;
}

.devicerow:hover .burnicon:hover {
  opacity: 1;
  -webkit-transition: none;
}

.devicerow {
  border-bottom: 1px solid #f7f7f7;
  padding: 8px 5px 5px 8px;
  font-size: .8em;
  position: relative;
  background-color: #fff;
}

html[dir='rtl'] .devicerow {
  padding: 8px 8px 5px 5px;
}

.devicerow:hover {
  background-color: #ebeff9;
  cursor: pointer;
}

.devicerow span.name {
  margin-top: 10px;
  -webkit-margin-start: 10px;
}

.selected {
  background-color: #b1c8f2;
}

h1.title {
  margin-top: 5px;
  text-align: center;
  font-size: 1.2em;
  font-weight: bold;
}

.columnlist {
  width: 100%;
  bottom: 0;
}

.fullcontainer {
  top: 0px;
  position: absolute;
  left: 0;
  bottom: 0;
  right: 600px;
  -webkit-border-end: 1px solid #7289E2;
}

html[dir='rtl'] .fullcontainer {
  left: 600px;
  right: 0;
}

progress {
  -webkit-margin-start: 15px;
  width: 700px;
}

.progressText {
  -webkit-padding-start: 20px;
  font-size: .6em;
  font: "Helvetica";
}

.statusText {
  padding-top: 5px;
  -webkit-padding-start: 15px;
  font-size: 1em;
}

.statusDiv{
  border-bottom: 1px solid #f7f7f7;
  padding-bottom: 2px;
  background-color: #fff;
}

#rootsColumn:not(.burningColumn) {
  height: 50%;
}

#statusColumn:not(.burningColumn) {
  height: 50%;
  border-top: 1px solid #7289E2;
}
</style>

<script src="shared/js/local_strings.js"></script>
<script>
function $(o) {
  return document.getElementById(o);
}

var currentlySelectedItem;
var currentlySelectedPath;
var localStrings;
var statusMessages;
var burnStarted;

function promptUserDownloadFinished() {
  // This could be done nicer.
  var answer = confirm(localStrings.getString('burnConfirmText1') +
      currentlySelectedPath + localStrings.getString('burnConfirmText2'));
  chrome.send(answer ? 'burnImage' : 'cancelBurnImage', []);
}

function alertUserDownloadAborted(){
  alert(localStrings.getString('downloadAbortedMessage'));
}

function burnSuccessful() {
  alert(localStrings.getString('burnSuccessfulMessage'));
}

function burnUnsuccessful(){
  alert(localStrings.getString('burnUnsuccessfulMessage'));
}

function rootsChanged(){
  chrome.send('getRoots', []);
}

function browseFileResult(info, results) {
  var path = info.path;
  var list = $('rootsList');
  list.innerHTML = '';
  var selectedRemoved = true;
  var addedRoots=[];
  for (var x = 0; x < results.length; x++) {
    if (!(results[x].title in addedRoots)) {
      addedRoots[results[x].title] = true;
      var element = createNewItem(results[x].title, results[x].path);
      if (results[x].path == currentlySelectedPath) {
        selectItem(element.id, results[x].path);
        selectedRemoved = false;
      }
      list.appendChild(element);
    }
  }
  if (selectedRemoved) {
    currentlySelectedItem = null;
    currentlySelectedPath = null;
  }
}

function selectItem(elementId, path) {
  var element = $(elementId);
  var index;
  if (currentlySelectedItem)
    currentlySelectedItem.className = 'devicerow';
  if (element) {
    element.className = 'selected devicerow';
    currentlySelectedItem = element;
    currentlySelectedPath = path;
  }
}

function createProgressElement(id) {
  var statusDiv = document.createElement('div');
  statusDiv.id = id;
  statusDiv.className = 'statusDiv';

  var statusText = document.createElement('div');
  statusText.id = id + 'StatusText';
  statusText.className = 'statusText';
  statusDiv.appendChild(statusText);

  var progressBar = document.createElement('progress');
  progressBar.id = id + 'ProgressBar';
  progressBar.className = 'progressBar';
  statusDiv.appendChild(progressBar);

  var progressText = document.createElement('div');
  progressText.id = id + 'ProgressText';
  progressText.className = 'progressText'
  statusDiv.appendChild(progressText);

  return statusDiv;
}

function setProgressElementFinalValues(element, statuses, event) {
  var statusTextDiv;
  var progressBarDiv;
  var progressTextDiv;
  for (var i = 0; i < element.children.length; i++ ) {
    switch(element.children[i].className) {
      case 'statusText':
        statusTextDiv = element.children[i];
        break;
      case 'progressBar':
        progressBarDiv = element.children[i];
        break;
      case 'progressText':
        progressTextDiv = element.children[i];
        break;
    }
  }
  if (event == 'COMPLETE')
    statusTextDiv.textContent = statuses['finished'];
  else
      statusTextDiv.textContent = statuses['canceled'];

  var progressBarParent = progressBarDiv.parentNode;
  progressBarParent.removeChild(progressBarDiv);

  var progressTextParent = progressTextDiv.parentNode;
  progressTextParent.removeChild(progressTextDiv);
  return element;
}

function setProgressElementValues(element, statuses, amountFinished,
                                  amountTotal, progressText){
  var statusTextDiv;
  var progressBarDiv;
  var progressTextDiv;
  for (var i = 0; i < element.children.length; i++ ) {
    switch(element.children[i].className) {
      case 'statusText':
        statusTextDiv = element.children[i];
        break;
      case 'progressBar':
        progressBarDiv = element.children[i];
        break;
      case 'progressText':
        progressTextDiv = element.children[i];
        break;
    }
  }

  if (!(amountFinished > 0)) {
    amountFinished = 0;
    statusTextDiv.textContent = statuses["start"];
    progressTextDiv.textContent = "";
  } else {
    statusTextDiv.textContent = statuses["inProgress"];
    progressTextDiv.textContent = progressText;
  }
  if(amountFinished > amountTotal)
    amountFinished = amountTotal;
  progressBarDiv.value = amountFinished;
  if (amountTotal)
    progressBarDiv.max = amountTotal;
  return element;
}

function updateProgressElement(progressType, progressInfo) {
  var element = $(progressType + 'ProgressElement');
  if (!element) {
    element = createProgressElement(progressType + 'ProgressElement');
    var column  = $('statusList');
    column.appendChild(element);
  }
  var errorOccurred = (progressInfo.state == 'CANCELLED');
  if (progressInfo.state == 'IN_PROGRESS') {
    setProgressElementValues(element, statusMessages[progressType],
        progressInfo.received, progressInfo.total,
        progressInfo.progress_status_text);
  } else {
    setProgressElementFinalValues(element, statusMessages[progressType],
                                  progressInfo.state);
  }
}

function downloadUpdated(result) {
  updateProgressElement('download', result);
}

function burnProgressUpdated(result) {
  if (!burnStarted) {
    $('rootsList').style.display = 'none';
    $('rootsListTitle').textContent = result.path;
    $('rootsColumn').className = 'burningColumn columnList';
    $('statusColumn').className = 'burningColumn columnList';
    burnStarted = true;
  }
  updateProgressElement('burn', result);
}

var elementIdCounter = 0;

function createNewItem(title, path) {
  var element = document.createElement('div');
  element.className = 'devicerow';
  element.id = path + elementIdCounter;
  elementIdCounter++;
  var link;
  link = document.createElement('div');
  link.onclick = function() { burnImage(element.id, path); };
  link.className = 'rowlink';

  var icon = document.createElement('div');
  icon.className = 'icon';
  link.appendChild(icon);

  var span = document.createElement('span');
  span.className = 'name';
  span.textContent = title;
  link.appendChild(span);

  element.appendChild(link);

  var burnicon = document.createElement('button');
  burnicon.className = 'burnicon';
  burnicon.onclick = function() { burnImage(element.id, path); };
  element.appendChild(burnicon);

  return element;
}

function burnImage(elementId, path) {
  selectItem(elementId, path);
  chrome.send('downloadImage', [path]);
}

function clearList(list) {
  while (list.childNodes.length >= 1) {
    list.removeChild(list.firstChild);
  }
}

document.addEventListener('DOMContentLoaded', function() {
  localStrings = new LocalStrings();
  burnStarted = false;
  chrome.send("getRoots", []);

  // Initialize messages that will be displayed as download
  // or burn progress is updated.
  statusMessages = [];

  var downloadStatuses = [];
  downloadStatuses['start'] = localStrings.getString('downloadStatusStart');
  downloadStatuses['inProgress'] =
      localStrings.getString('downloadStatusInProgress');
  downloadStatuses['finished'] =
      localStrings.getString('downloadStatusComplete');
  downloadStatuses['canceled'] =
      localStrings.getString('downloadStatusCanceled');
  statusMessages['download'] = downloadStatuses;

  var burnStatuses = [];
  burnStatuses['start'] = localStrings.getString('burnStatusStart');
  burnStatuses['inProgress'] = localStrings.getString('burnStatusInProgress');
  burnStatuses['finished'] = localStrings.getString('burnStatusComplete');
  burnStatuses['canceled'] = localStrings.getString('burnStatusCanceled');
  statusMessages['burn'] = burnStatuses;
});
</script>
</head>
<body>
  <div id="main" class="fullcontainer">
    <div id="rootsColumn" class="columnlist">
      <h1 id="rootsListTitle" class="title" i18n-content="listTitle"></h1>
      <div id="rootsList"></div>
    </div>
    <div id="statusColumn" class="columnlist">
      <div id="statusList"></div>
    </div>
  </div>
</body>
</html>