{% extends "base.html" %}

{% import "bootstrap/wtf.html" as wtf %}
{% import "bootstrap/fixes.html" as fixes %}
{% import "bootstrap/utils.html" as util %}
{% import "macros.html" as pbw %}

{% block title %}Node Catalog{% endblock %}

{% block content %}
    <div class="container">
        {{ util.flashed_messages(dismissible=True, container=False) }}

        <ol class="breadcrumb">
            <li><a href="{{ url_for('ui.home') }}">Catalog</a></li>
            <li class="active">Nodes</li>
        </ol>

        <div class="panel panel-default">
            <div class="panel-heading">
                <h3 class="panel-title">Nodes</h3>
            </div>
            <div class="panel-body">
                <form action="{{ url_for('ui.view_nodes') }}">
                    <div class="input-group">
                        <input type="text" class="form-control" placeholder="Search for nodes..." name="search">
                        <span class="input-group-btn">
                    <input class="btn btn-default" type="submit">
                </span>
                    </div>
                </form>
            </div>
        </div>

        <div class="panel panel-default">
            <div class="list-group" id="edge-list">
                {% for node in nodes %}
                    <div class="list-group-item">
                        <h4 class="list-group-item-heading">
                            <a href="{{ url_for('ui.view_node', node_hash=node.md5) }}">{{ node.bel }}</a>

                            <span class="pull-right">
                                <small class="text-muted">
                                    In-Edges: {{ node.in_edges.count() }} |
                                    Out-Edges: {{ node.out_edges.count() }} |

                                    {% if node.type == 'Gene' %}
                                        Orthologies: {{ node.out_edges.filter_by(relation='orthologous').count() }} |
                                    {% endif %}

                                    Equivalencies: {{ node.out_edges.filter_by(relation='equivalentTo').count() }} |
                                    Classes: {{ node.out_edges.filter_by(relation='isA').count() }} |
                                    Children: {{ node.in_edges.filter_by(relation='isA').count() }} |

                                    <a href="{{ url_for('ui.view_explorer_node', node_hash=node.md5) }}">Explore</a>

                                    {% if current_user.is_authenticated and current_user.is_admin %}
                                        |
                                        <a href="{{ url_for('dbs.get_node_by_hash', node_hash=node.md5) }}">JSON</a>
                                    {% endif %}
                                </small>
                            </span>
                        </h4>
                        <div class="list-group-item-text">
                            {% set node_members = node.out_edges.filter_by(relation='hasComponent').all() %}
                            {% if node_members %}
                                <h4>Components</h4>
                                <ul>
                                    {% for node_member in node_members %}
                                        <li>
                                            <a href="{{ url_for('ui.view_node', node_hash=node_member.target.md5) }}">
                                                {{ node_member.target.bel }}
                                            </a>
                                        </li>
                                    {% endfor %}
                                </ul>
                            {% endif %}

                            {% set node_in_edges = node.in_edges.limit(3) %}
                            {% set node_out_edges = node.out_edges.limit(2) %}

                            {% if node_in_edges or node_out_edges %}
                                <h4>Example Edges:</h4>
                                <ul>
                                    {% for edge in node_in_edges %}
                                        <li>
                                            <a href="{{ url_for('ui.view_edge', edge_hash=edge.md5) }}">
                                                {{ edge.bel }}
                                            </a>
                                        </li>
                                    {% endfor %}
                                    {% for edge in node_out_edges %}
                                        <li>
                                            <a href="{{ url_for('ui.view_edge', edge_hash=edge.md5) }}">
                                                {{ edge.bel }}
                                            </a>
                                        </li>
                                    {% endfor %}
                                </ul>
                            {% endif %}

                            <h4>Appears in Networks:</h4>
                            <ul>
                                {% for network in node.networks %}
                                    <li>
                                        <a href="{{ url_for('ui.view_network', network_id=network.id) }}">
                                            {{ network }}
                                        </a>
                                    </li>
                                {% endfor %}
                            </ul>

                            {% if node.namespace_entry %}
                                {% if hgnc_manager and node.namespace_entry.namespace.keyword == 'HGNC' %}
                                    {{ pbw.render_node_hgnc_enrichment(hgnc_manager, node) }}
                                {% elif entrez_manager and node.namespace_entry.namespace.keyword in ['EGID', 'EG', 'ENTREZ'] %}
                                    {{ pbw.render_node_entrez_enrichment(entrez_manager, node) }}
                                {% elif chebi_manager and node.namespace_entry.namespace.keyword in ['CHEBI', 'CHEBIID'] %}
                                    {{ pbw.render_node_chebi_enrichment(chebi_manager, node) }}
                                {% elif go_manager and node.namespace_entry.namespace.keyword in ['GO', 'GOBP', 'GOBPID'] %}
                                    {{ pbw.render_node_go_enrichment(go_manager, node) }}
                                {% else %}
                                    <h4>Entity</h4>
                                    <dl class="dl-horizontal">
                                        <dt>Name</dt>
                                        <dd>{{ node.namespace_entry.name }}</dd>
                                        <dt>Namespace</dt>
                                        <dd>{{ node.namespace_entry.namespace.keyword }}</dd>
                                        <dt>Namespace Version</dt>
                                        <dd>{{ node.namespace_entry.namespace.version }}</dd>
                                        {% if node.namespace_entry.namespace.url %}
                                            <dt>Namespace URL</dt>
                                            <dd>
                                                <a href="{{ node.namespace_entry.namespace.url }}">
                                                    {{ node.namespace_entry.namespace.url }}
                                                </a>
                                            </dd>
                                        {% else %}
                                            <dt>Pattern</dt>
                                            <dd>{{ node.namespace_entry.namespace.pattern }}</dd>
                                        {% endif %}
                                    </dl>
                                {% endif %}
                            {% endif %}
                        </div>
                    </div>
                {% endfor %}
            </div>
            {% if count %}
                <div class="panel-footer">
                    <p>Showing
                        {% if offset != 0 %}{{ offset }} - {% endif %}{{ offset + nodes.count() }} of {{ count }} nodes.

                        {% if offset + limit * 2 < count %}
                            <a href="{{ url_for('ui.view_nodes', limit=limit, offset=limit+offset) }}">Next {{ limit }}</a>
                        {% else %}
                            <a href="{{ url_for('ui.view_nodes', limit=count - limit - offset, offset=limit + offset) }}">Next {{ count - limit - offset }}</a>
                        {% endif %}
                    </p>
                </div>
            {% endif %}
        </div>
    </div>

    {% include "footer.html" %}
{% endblock %}
