'use strict';

//create a file-specific context via a function
(function(Piv, ElectionId) {
    var View = Piv.view

    var headerStyle = {"class": "font-size-1"}

    // page layout
    Piv.html(View.workspace, "h1", "Snapshots", headerStyle)
    var SnapshotsDiv = Piv.div(View.workspace, "Snapshots", "text1")
    Piv.html(View.workspace, "h1", "Candidates", headerStyle)
    var CandidatesDiv = Piv.div(View.workspace, "Candidates", "text1")
    Piv.html(View.workspace, "h1", "Ballots", headerStyle)
    var BallotsDiv = Piv.div(View.workspace, "Ballots", "text1")
    Piv.html(View.workspace, "h1", "Tie-Breaker (partial)", headerStyle)
    var TiePartialDiv = Piv.div(View.workspace, "TiePartial", "text1")
    Piv.html(View.workspace, "h1", "Tie-Breaker (total)", headerStyle)
    var TieTotalDiv = Piv.div(View.workspace, "TieTotal", "text1")
    Piv.html(View.workspace, "h1", "Results", headerStyle)
    var ResultsDiv = Piv.div(View.workspace, "Results", "text1")
    Piv.html(View.workspace, "h1", "Plot", headerStyle)
    var PlotDiv = Piv.div(View.workspace, "Plot", "text1")

    function main() {
	// generic setup
	View.setHeader("Election Debugger", ElectionId)
	View.statusbar.innerHTML = ""
	Piv.electionsMenu(View.sidenav, ElectionId)
	Piv.removeHrefsForCurrentLoc()
	
	// fetch snapshots
	Piv.http.get(["/api/elections/" + ElectionId + "/result_snapshots"],
		     getSnapshots, showErrorMessage)
    }

    function getSnapshots(snapshots) {
	snapshots.forEach(snapshot => {
	    console.log(snapshot);
	    piv.html(SnapshotsDiv, "span", snapshot.id + "<br>")
	});
    }

    function showErrorMessage(error) {
	alert(error);
	Piv.div(View.workspace, "", "w100 text3", "Results for this election are not currently available.")
	if (!error) return
	Piv.div(View.workspace, "", "100 text3", error.response.data.message)
    }

    main(piv, election)
})(piv, election)

@@ -1108,17 +1108,24 @@
 ain() %7B%0A
-%09
+        
 // gener
@@ -1133,17 +1133,24 @@
 c setup%0A
-%09
+        
 View.set
@@ -1189,17 +1189,24 @@
 tionId)%0A
-%09
+        
 View.sta
@@ -1227,17 +1227,24 @@
 ML = %22%22%0A
-%09
+        
 Piv.elec
@@ -1279,17 +1279,24 @@
 tionId)%0A
-%09
+        
 Piv.remo
@@ -1322,11 +1322,103 @@
 c()%0A
-%09%0A%09
+%0A        // start workflow%0A        getSnapshots()%0A    %7D%0A%0A    function getSnapshots() %7B%0A        
 // f
@@ -1432,17 +1432,24 @@
 apshots%0A
-%09
+        
 Piv.http
@@ -1513,17 +1513,31 @@
 %22%5D,%0A
-%09%09
+                
      g
-e
+o
 tSna
@@ -1575,33 +1575,33 @@
 %0A%0A    function g
-e
+o
 tSnapshots(snaps
@@ -1608,17 +1608,24 @@
 hots) %7B%0A
-%09
+        
 snapshot
@@ -1652,83 +1652,849 @@
 %3E %7B%0A
-%09    console.log(snapshot);%0A%09    piv.html(SnapshotsDiv, %22span%22, snapshot.id
+            var button = piv.html(SnapshotsDiv, %22button%22, snapshot.id)%0A            button.onclick = function() %7B%0A                getSnapshot(snapshot.id)%0A            %7D%0A        %7D)%0A    %7D%0A%0A    function getSnapshot(SnapshotId) %7B%0A        Piv.http.get(%5B%22/api/elections/%22 + ElectionId + %22/result_snapshots/%22 + SnapshotId%5D,%0A                     gotSnapshot, showErrorMessage)%0A    %7D%0A%0A    function gotSnapshot(snapshot) %7B%0A        if (snapshot.format_version %3C 3) %7B%0A            alert(%22You can only debug snapshots with version 3 or greater.  This one was version %22 + snapshot.format_version + %22.%22)%0A            return%0A        %7D%0A        console.log(snapshot)%0A%0A        CandidatesDiv.innerHTML = %22%22%0A        snapshot.result_blob.debug_private.candidates.forEach(candidate =%3E %7B%0A            piv.html(CandidatesDiv, %22span%22, candidate.id + %22: %22 + candidate.name
  + %22
@@ -2504,12 +2504,18 @@
 %3E%22)%0A
-%09
+        
 %7D)
-;
 %0A   
@@ -2557,17 +2557,24 @@
 rror) %7B%0A
-%09
+        
 alert(er
@@ -2581,11 +2581,17 @@
 ror)
-;%0A%09
+%0A        
 Piv.
@@ -2686,17 +2686,24 @@
 able.%22)%0A
-%09
+        
 if (!err
@@ -2717,9 +2717,16 @@
 urn%0A
-%09
+        
 Piv.

add candidate id=>name mapping