const electron = require("electron");
const ipcRenderer = electron.ipcRenderer;
const dialog = electron.remote.dialog;

const replay_webview = document.getElementById("replay");

/*
button #open
display a file selector and call ipc "open-warc" on main
*/
function openFile() {
  dialog.showOpenDialog(
    {
      properties: ["openFile"],
      filters: [{ name: "Warc", extensions: ["gz", "warc", "arc", "warcgz"] }]
    },
    function(filename) {
      if (filename) {
        ipcRenderer.send("open-warc", filename.toString());
      }
    }
  );
}

document.getElementById("open").addEventListener("click", openFile);


/*
Go Back
(excluding about:blank and loader.html)
*/
document.getElementById("back").addEventListener("click", _ => {
  webview_history = replay_webview.getWebContents().history;
  current = replay_webview.getWebContents().getURL();
  previous = webview_history[webview_history.indexOf(current) - 1];
  if (previous.startsWith("http")) {
    replay_webview.goBack();
  }
});

/*
Go Forward
*/
document.getElementById("forward").addEventListener("click", _ => {
  replay_webview.goForward();
});

/*
Go to collection listing?
*/
document.getElementById("home").addEventListener("click", _ => {
  //TODO
});

/*
renderer ipc "loadWebview"
called by main after pywb is launched, load a url into webview
*/
ipcRenderer.on("loadWebview", (event, message) => {
  replay_webview.loadURL(message);
});


replay_webview.addEventListener("ipc-message", (event) => {
  openFile();
});


/*
hides all .btn on webview dom-ready
*/
/*
replay_webview.addEventListener("dom-ready", () => {
  replay_webview.executeJavaScript(
    'for (let el of document.querySelectorAll(".btn")) el.style.visibility = "hidden";'
  );
});
*/

@@ -172,16 +172,68 @@
 lay%22);%0A%0A
+const top_bar = document.getElementById(%22topBar%22);%0A%0A
 /*%0Abutto
@@ -1543,24 +1543,429 @@
 ile();%0A%7D);%0A%0A
+replay_webview.addEventListener('did-navigate', (event) =%3E %7B%0A%0A  // Initial View%0A  if (event.url.startsWith(%22file://%22)) %7B%0A     topBar.className = %22side%22;%0A  // Collection view: eg http://localhost:8090/local/collection%0A  %7D else if (event.url.split(%22/%22).length == 5) %7B%0A     topBar.className = %22side viewCollection%22;%0A  // Anything else is replay!%0A  %7D else %7B%0A     topBar.className = %22side replay%22;%0A  %7D%0A%0A%7D);%0A%0A%0A%0A
 %0A/*%0Ahides al

add top-bar style transitions