const electron = require("electron");

const ipcRenderer = electron.ipcRenderer;
const dialog = electron.remote.dialog;

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

const top_bar = document.getElementById("topBar");

function getHost() {
  return electron.remote.getGlobal('sharedConfig').host;
}

/*
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
});

/*
Go to collection listing
*/
document.getElementById("backToCollection").addEventListener("click", _ => {
  replay_webview.loadURL(`${getHost()}/local/collection`);
});


/*
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();
});

replay_webview.addEventListener('did-navigate', (event) => {

  // Initial View
  if (event.url.startsWith("file://")) {
     topBar.className = "side";
  // Collection view: eg http://localhost:8090/local/collection
  } else if (event.url.split("/").length == 5) {
     topBar.className = "side viewCollection";
  // Anything else is replay!
  } else {
     topBar.className = "side replay";
  }

});




/*
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";'
  );
});
*/

@@ -281,17 +281,17 @@
 tGlobal(
-'
+%22
 sharedCo
@@ -294,17 +294,17 @@
 edConfig
-'
+%22
 ).host;%0A
@@ -541,16 +541,23 @@
 %22warcgz%22
+, %22har%22
 %5D %7D%5D%0A   
@@ -595,32 +595,86 @@
     if (filename
+ && filename.toString().match(/%5C.w?arc(%5C.gz)?%7C%5C.har$/)
 ) %7B%0A        ipcR
@@ -721,16 +721,147 @@
 ing());%0A
+      %7D else %7B%0A        window.alert(%22Sorry, only WARC or ARC files (.warc, .warc.gz, .arc, .arc.gz) or HAR (.har) can be opened%22);%0A
       %7D%0A
@@ -944,17 +944,16 @@
 File);%0A%0A
-%0A
 /*%0AGo Ba
@@ -1724,17 +1724,16 @@
 );%0A%7D);%0A%0A
-%0A
 /*%0Arende
@@ -1912,17 +1912,16 @@
 );%0A%7D);%0A%0A
-%0A
 replay_w
@@ -1959,23 +1959,21 @@
 ssage%22, 
-(
 event
-)
  =%3E %7B%0A  
@@ -2021,17 +2021,17 @@
 istener(
-'
+%22
 did-navi
@@ -2038,25 +2038,22 @@
 gate
-'
+%22
 , 
-(
 event
-)
  =%3E %7B%0A
-%0A
   //
@@ -2103,25 +2103,24 @@
 ile://%22)) %7B%0A
- 
     topBar.c
@@ -2138,16 +2138,18 @@
 %22side%22;%0A
+  
   // Col
@@ -2253,25 +2253,24 @@
 == 5) %7B%0A    
- 
 topBar.class
@@ -2299,16 +2299,18 @@
 ction%22;%0A
+  
   // Any
@@ -2346,17 +2346,16 @@
 e %7B%0A    
- 
 topBar.c
@@ -2388,17 +2388,13 @@
   %7D%0A
-%0A
 %7D);%0A%0A
-%0A%0A%0A
 /*%0Ah

check file extensions before loading (#6)