<% if(n_total > 0) { %>

<% 
has_imports = !any(is.na(pkg$df_imports[, 1]))
%>

<% if(has_imports) { %>
<p>"Imports" information is from the <a onclick='show_namespace("<%=pkg$package%>")' class="fake-link">NAMESPACE</a> file of <span class="package"><%=pkg$package%></span>.</p>
<p><b>imports:</b> number of imported functions/objects; <b>importMethods</b>: number of imported S4 methods; <b>importClasses</b>: number of imported S4 classes.</p>
<% } %>

<p><b>Total required packages:</b> number of total dependency packages that a parent package brings in.</p>
<p><b>Heaviness on <span class="package"><%=pkg$package%></span>:</b> number of extra dependency packages that a parent uniquely imported to <span class="package"><%=pkg$package%></span>.</p>

<p>The full table can be obtained by:</p>

<pre class="code">
library(pkgndep)
pkgndep_opt$heaviness_db_version = "<%=pkgndep_opt$heaviness_db_version%>"
parent_dependency("<%=pkg$package%>")</pre>

 <code></code>.</p>

<% tb[, 1] = qq("<a href='?package=@{tb[, 1]}'>@{tb[, 1]}</a>", collapse = FALSE)
if(has_imports) {
    html_tb = as.character(knitr::kable(tb, format = "html", row.names = FALSE, escape = FALSE, col.names = c("Parent package", "Field", "imports", "importMethods", "importClasses", "Total required packages", qq("Heaviness on <span class='package'>@{pkg$package}</span>")), table.attr = "class='table table-striped' id='parent-dependency'", align = c("l", rep("r", ncol(tb) - 1))))
    html_tb = gsub("(<td[^>]*?> Suggests </td>\\s+)<td[^>]*?> 0 </td>\\s+<td[^>]*?> 0 </td>\\s+<td[^>]*?> 0 </td>\\s+", qq("\\1<td colspan=3 style='text-align:right;'>No object is imported into the namespace of <span class='package'>@{pkg$package}</span>.</td>\n"), html_tb)
    html_tb = gsub("(<td[^>]*?> Enhances </td>\\s+)<td[^>]*?> 0 </td>\\s+<td[^>]*?> 0 </td>\\s+<td[^>]*?> 0 </td>\\s+", qq("\\1<td colspan=3 style='text-align:right;'>No object is imported into the namespace of <span class='package'>@{pkg$package}</span>.</td>\n"), html_tb)
    html_tb = gsub("(<td[^>]*?> Depends </td>\\s+)<td[^>]*?> 0 </td>\\s+<td[^>]*?> 0 </td>\\s+<td[^>]*?> 0 </td>\\s+", qq("\\1<td colspan=3 style='text-align:right;'>The whole set of functions/methods/classes from parent package is imported to the namespace of <span class='package'>@{pkg$package}</span>.</td>\n"), html_tb)
    html_tb = gsub("(<td[^>]*?> Depends </td>\\s+)<td[^>]*?> -(\\d+) </td>\\s+<td[^>]*?> 0 </td>\\s+<td[^>]*?> 0 </td>\\s+", qq("\\1<td colspan=3 style='text-align:right;'>The whole set of functions/methods/classes from parent package excluding \\2 objects is imported to the namespace of <span class='package'>@{pkg$package}</span>.</td>\n"), html_tb)
    html_tb = gsub("(<td[^>]*?> Depends </td>\\s+)<td[^>]*?> -Inf </td>\\s+<td[^>]*?> 0 </td>\\s+<td[^>]*?> 0 </td>\\s+", qq("\\1<td colspan=3 style='text-align:right;'>Parent package is listed in <code>Depends</code> of <span class='package'>@{pkg$package}</span> but no object from parent package is imported.</td>\n"), html_tb)
    html_tb = gsub("(<td[^>]*?> Imports </td>\\s+)<td[^>]*?> 0 </td>\\s+<td[^>]*?> 0 </td>\\s+<td[^>]*?> 0 </td>\\s+", qq("\\1<td colspan=3 style='text-align:right;'>The whole set of functions/methods/classes from parent package is imported to the namespace of <span class='package'>@{pkg$package}</span>.</td>\n"), html_tb)
    html_tb = gsub("(<td[^>]*?> Imports </td>\\s+)<td[^>]*?> -(\\d+) </td>\\s+<td[^>]*?> 0 </td>\\s+<td[^>]*?> 0 </td>\\s+", qq("\\1<td colspan=3 style='text-align:right;'>The whole set of functions/methods/classes from parent package excluding \\2 objects is imported to the namespace of <span class='package'>@{pkg$package}</span>.</td>\n"), html_tb)
    html_tb = gsub("(<td[^>]*?> Imports </td>\\s+)<td[^>]*?> -Inf </td>\\s+<td[^>]*?> 0 </td>\\s+<td[^>]*?> 0 </td>\\s+", qq("\\1<td colspan=3 style='text-align:right;'>Parent package is listed in <code>Imports</code> of <span class='package'>@{pkg$package}</span> but no object from parent package is imported.</td>\n"), html_tb)
    html_tb = gsub("(<td[^>]*?> LinkingTo </td>\\s+)<td[^>]*?> 0 </td>\\s+<td[^>]*?> 0 </td>\\s+<td[^>]*?> 0 </td>\\s+", qq("\\1<td colspan=3 style='text-align:right;'>No object is imported into the namespace of <span class='package'>@{pkg$package}</span>.</td>\n"), html_tb)
    html_tb = gsub("(<td[^>]*?> LinkingTo </td>\\s+)<td[^>]*?> -(\\d+) </td>\\s+<td[^>]*?> 0 </td>\\s+<td[^>]*?> 0 </td>\\s+", qq("\\1<td colspan=3 style='text-align:right;'>The whole set of functions/methods/classes from parent package excluding \\2 objects is imported to the namespace of <span class='package'>@{pkg$package}</span>.</td>\n"), html_tb)
    html_tb = gsub("(<td[^>]*?> LinkingTo </td>\\s+)<td[^>]*?> -Inf </td>\\s+<td[^>]*?> 0 </td>\\s+<td[^>]*?> 0 </td>\\s+", qq("\\1<td colspan=3 style='text-align:right;'>Parent package is listed in <code>LinkingTo</code> of <span class='package'>@{pkg$package}</span> but no object from parent package is imported.</td>\n"), html_tb)
} else {
    tb = tb[, !colnames(tb) %in% c("imports", "importMethods", "importClasses"), drop = FALSE]
    html_tb = as.character(knitr::kable(tb, format = "html", row.names = FALSE, escape = FALSE, col.names = c("Parent package", "Field", "Total required packages", qq("Heaviness on <span class='package'>@{pkg$package}</span>")), table.attr = "class='table table-striped' id='parent-dependency'", align = c("l", rep("r", ncol(tb) - 1))))
}
%>

<%= html_tb %>


<style>
.row-depends {
    border-left: 6px solid #d9534f;
}
.row-imports {
    border-left: 6px solid #f0ad4e;
}
.row-linkingto {
    border-left: 6px solid #337ab7;
}
.row-suggests {
    border-left: 6px solid #5cb85c;
}
.row-enhances {
    border-left: 6px solid #5bc0de;
}
</style>

<script>
$("#parent-dependency tr td:nth-child(2)").each(function(index) {
    if($(this).text() == " Depends ") {
        $(this).parent().addClass("row-depends");
    } else if($(this).text() == " Imports ") {
        $(this).parent().addClass("row-imports");
    } else if($(this).text() == " LinkingTo ") {
        $(this).parent().addClass("row-linkingto");
    } else if($(this).text() == " Suggests ") {
        $(this).parent().addClass("row-suggests");
    } else if($(this).text() == " Enhances ") {
        $(this).parent().addClass("row-enhances");
    }
})
</script>

  <% nr = n_total
  if(nr > records_per_page) { %>
    <%= page_select2(page, ceiling(nr/records_per_page), "parent_dependency", pkg$package) %>
  <% } %>

  <% m = co_heaviness(pkg)
    if(nrow(m) > 1) {
        if(attr(m, "max") > 10) {
            m2 = co_heaviness(pkg, jaccard = TRUE)

            pair1 = NULL
            pair2 = NULL
            hv1 = NULL
            hv2 = NULL

            nr = nrow(m)
            for(i in 1:(nr-1)) {
                for(j in (i+1):nr) {
                    if(m[i, j] > 10) {
                        pair1 = c(pair1, rownames(m)[i])
                        pair2 = c(pair2, rownames(m)[j])
                        hv1 = c(hv1, m[i, j])
                        hv2 = c(hv2, m2[i, j])
                    }
                }
            }

            relation = rep("", length(pair1))
            for(i in seq_along(pair1)) {
                v = "not-clear"
                p = c(pair1[i], pair2[i])
                if(length(p)) {
                    if(is_parent(p[1], p[2])) {
                        v = "parent-child"
                    } else if(is_parent(p[2], p[1])) {
                        v = "parent-child"
                    } else if(is_upstream(p[1], p[2])) {
                        v = "upstream-downstream"
                    } else if(is_parent(p[2], p[1])) {
                        v = "upstream-downstream"
                    } else {
                        up1 = heaviness_from_upstream(p[1])
                        up2 = heaviness_from_upstream(p[2])
                        # the max heaviness of common upstream
                        max = attr(m, "max")
                        cn = intersect(names(up1[up1 > max*0.75]), names(up2[up2 > max*0.75]))
                        if(length(cn)) {
                            v = "have-common-upstream"
                        }
                    }
                } 
                relation[i] = v
            }
    

            co_tb = data.frame(pair1 = qq("<a href='package?package=@{pair1}'>@{pair1}</a>", collapse = FALSE), pair2 = qq("<a href='package?package=@{pair2}'>@{pair2}</a>", collapse = FALSE), relation = relation, heaviness = hv1, jaccard = round(hv2, 3)) 
            co_tb = co_tb[order(co_tb$heaviness, decreasing = TRUE), , drop = FALSE]
        %>
        <hr>
<p>The following table lists pairs of parent packages with co-heaviness larger than 10. The co-heaviness measures the number of additional 
dependencies that two parent packages simultaneously import and are only imported by the two parents.</p>
<p>Note the co-heaviness from parents can be always easily observed from the dependency heatmap.</p>
            <%= as.character(knitr::kable(co_tb, format = "html", row.names = FALSE, escape = FALSE, col.names = c("Parent 1", "Parent 2", "Relation of two parents", qq("Co-heaviness on @{pkg$package}"), "Co-heaviness as Jaccard coeffcient"), table.attr = "class='table table-striped' style='width:1000px'", align = c("l", "l", "r", "r", "r") )) %>
<p>Denote the heaviness from parent <i>A</i> on <i>P</i> as $h_A$, the heaviness from parent <i>B</i> on <i>P</i> as $h_B$, and the co-heaviness from <i>A</i> and <i>B</i> as $h_{co}$,
the Jaccard coeffcient is calculated as: $h_{co}/(h_{co} + h_A + h_B)$.</p>
   <%     }
    }
  %>

<% } else { %>

<p>No parent dependency found.</p>

<% } %>
