"use strict";

window.onload = function() {
    redraw();
};

function redraw() {
    redrawQueries();
    redrawPools();
}

function redrawQueries() {
    var queries = document.getElementById("queries");
    while (queries.firstChild)
        queries.removeChild(queries.firstChild);

    if (!tideData.Queries) {
        return;
    }
    for (var i = 0; i < tideData.Queries.length; i++) {
        var query = tideData.Queries[i];

        var a = document.createElement("a");
        a.href = "https://github.com/search?utf8=" + encodeURIComponent("\u2713") + "&q=" + encodeURIComponent(query);
        a.appendChild(document.createTextNode(query));

        //var div = document.createElement("div");
        //div.appendChild(a);
        var li = document.createElement("li");
        li.appendChild(a);

        queries.appendChild(li);
    }
}

function redrawPools() {
    var pools = document.getElementById("pools").getElementsByTagName("tbody")[0];
    while (pools.firstChild)
        pools.removeChild(pools.firstChild);

    // TODO(spxtr): Sort these.
    if (!tideData.Pools) {
        return;
    }
    for (var i = 0; i < tideData.Pools.length; i++) {
        var pool = tideData.Pools[i];
        var r = document.createElement("tr");

        var repoName = pool.Org + "/" + pool.Repo + " " + pool.Branch;
        var repoLink = "https://github.com/" + pool.Org + "/" + pool.Repo + "/tree/" + pool.Branch;
        r.appendChild(createLinkCell(repoName, repoLink, ""));
        r.appendChild(createTextCell(pool.Action));
        r.appendChild(createPRCell(pool, pool.BatchPending));
        r.appendChild(createPRCell(pool, pool.PassingPRs));
        r.appendChild(createPRCell(pool, pool.PendingPRs));
        r.appendChild(createPRCell(pool, pool.MissingPRs));

        pools.appendChild(r);
    }
}

function createLinkCell(text, url, title) {
    var c = document.createElement("td");
    var a = document.createElement("a");
    a.href = url;
    if (title !== "") {
        a.title = title;
    }
    a.appendChild(document.createTextNode(text));
    c.appendChild(a);
    return c;
}

function createTextCell(text) {
    var c = document.createElement("td");
    c.appendChild(document.createTextNode(text));
    return c;
}

function createPRCell(pool, prs) {
    var c = document.createElement("td");
    if (prs) {
        for (var i = 0; i < prs.length; i++) {
            var a = document.createElement("a");
            a.href = "https://github.com/" + pool.Org + "/" + pool.Repo + "/pull/" + prs[i].Number;
            a.appendChild(document.createTextNode("#" + prs[i].Number));
            c.appendChild(a);
            c.appendChild(document.createTextNode(" "));
        }
    }
    return c;
}

@@ -1508,28 +1508,30 @@
 Child(create
-Text
+Action
 Cell(pool.Ac
@@ -1531,15 +1531,8 @@
 pool
-.Action
 ));%0A
@@ -1643,15 +1643,15 @@
 ool.
-Passing
+Success
 PRs)
@@ -2121,210 +2121,611 @@
 eate
-TextCell(text) %7B%0A    var c = document.createElement(%22td%22);%0A    c.appendChild(document.createTextNode(text));%0A    return c;%0A%7D%0A%0Afunction createPRCell(pool, prs) %7B%0A    var c = document.createElement(%22td%22);
+ActionCell(pool) %7B%0A    var action = pool.Action;%0A    var targetted = pool.Target && pool.Target.length%0A    var c = document.createElement(%22td%22);%0A%0A    if (targetted) %7B%0A        action += %22: %22%0A    %7D%0A    c.appendChild(document.createTextNode(action));%0A    if (targetted) %7B%0A        addPRsToElem(c, pool, pool.Target)%0A    %7D%0A    return c;%0A%7D%0A%0Afunction createPRCell(pool, prs) %7B%0A    var c = document.createElement(%22td%22);%0A    addPRsToElem(c, pool, prs)%0A    return c;%0A%7D%0A%0A// addPRsToElem adds a space separated list of PR numbers that link to the corresponding PR on github.%0Afunction addPRsToElem(elem, pool, prs) %7B
 %0A   
@@ -3009,33 +3009,36 @@
 ));%0A            
-c
+elem
 .appendChild(a);
@@ -3046,25 +3046,133 @@
             
-c
+// Add a space after each PR number except the last.%0A            if (i+1 %3C prs.length) %7B%0A                elem
 .appendChild
@@ -3215,28 +3215,27 @@
     
+    
 %7D%0A    
+ 
+   
 %7D%0A    
-return c;
+%7D
 %0A%7D
-%0A

Fix typo in Tide js and make front end show action target.