{% import 'macros/_account_macros.html' as account_macros with context %}

{% macro render_diff_and_number(balance, cost, currency) %}
  {% set num = balance.pop(currency, 0) %}
  {% if currency in cost %}
    {% set cost_num = cost.pop(currency, 0) %}
    {% set diff = num - cost_num %}
    {% if diff %}
    <span class="diff{{ ' positive' if diff > 0 else ' negative' }}" title="{{ cost_num|format_currency(currency) }} {{ currency }}">
      ({{ diff|format_currency(currency) }})
    </span>
    {% endif %}
  {% endif %}
  <span class="number">{{ num|format_currency(currency) }}</span>
{% endmacro %}

{% macro tree(account_node) %}
<tree-table>
<ol class="flex-table tree-table{{ ' two-currencies' if ledger.options.operating_currency|length > 1 else '' }}" title="{{ _('Hold Shift while clicking to expand all children.
Hold Ctrl or Cmd while clicking to expand one level.') }}">
  <li class="head">
    <p>
      <span class="account-cell"><button type="button" class="link expand-all hidden" title="{{ _('Expand all accounts') }}">{{ _('Expand all') }}</button></span>
      {% for currency in ledger.options.operating_currency %}
      <span class="num">{{ currency }}</span>
      {% endfor %}
      <span class="num other">{{ _('Other') }}</span>
    </p>
  </li>
  {% for account in ([account_node] if account_node.name else account_node.children) if account|should_show recursive %}
  {% set balance = account.balance|cost_or_value(ledger.end_date) %}
  {% set balance_children = account.balance_children|cost_or_value(ledger.end_date) %}
  {% set cost = account.balance|cost if g.conversion == 'at_value' else {} %}
  {% set cost_children = account.balance_children|cost if g.conversion == 'at_value' else {} %}
  <li{{ ' class=toggled' if account.name|collapse_account else '' }}>
    <p{{ ' class=has-balance' if not balance.is_empty() else '' }}>
    <span class="account-cell depth-{{ loop.depth0 }} droptarget{{ ' has-children' if account.children else '' }}" data-account-name="{{ account.name }}">
      {{ account_macros.account_name(account.name, last_segment=True) }}
    </span>
    {% for currency in ledger.options.operating_currency %}
    <span class="num">
      <span class="balance">{{ render_diff_and_number(balance, cost, currency) }}</span>
      <span class="balance-children">{{ render_diff_and_number(balance_children, cost_children, currency) }}</span>
    </span>
    {% endfor %}
    <span class="num other">
      <span class="balance">
        {% for currency in balance.keys()|sort %}
        {{ render_diff_and_number(balance, cost, currency) }} {{ currency }}<br>
        {% endfor %}
      </span>
      <span class="balance-children">
        {% for currency in balance_children.keys()|sort %}
        {{ render_diff_and_number(balance_children, cost_children, currency) }} {{ currency }}<br>
        {% endfor %}
      </span>
    </span>
    </p>
    {% if account.children %}
    <ol>
      {{ loop(account.children|sort(attribute='name')) }}
    </ol>
    {% endif %}
  </li>
  {% endfor %}
</ol>
</tree-table>
{% endmacro %}

{% macro render_budget(budget, currency, number=0) %}
{% if currency in budget %}
{% set diff = budget[currency] - number %}
<span class="diff {% if diff > 0 %}positive{% elif diff < 0 %}negative{% endif %}" title="{{ budget[currency]|format_currency(currency) }} {{ currency }}">
  ({{ diff|format_currency(currency, show_if_zero=True) }}{{ ' '+currency if not number else '' }})
</span>
{% endif %}
{% endmacro %}

{% macro account_tree(account_name, interval_balances, dates, accumulate) %}
<tree-table>
<ol class="fullwidth flex-table tree-table">
    <li class="head">
        <p>
        <span class="account-cell"><button type="button" class="link expand-all hidden" title="{{ _('Expand all accounts') }}">{{ _('Expand all') }}</button></span>
        {% for begin_date, end_date in dates %}
            {% if accumulate %}
                {% set time_filter = dates[0][0]|string + ' - ' + end_date|string %}
            {% else %}
                {% set time_filter = begin_date|string + ' - ' + end_date|string %}
            {% endif %}

            <span class="num other"><a href="{{ url_for('account', name=account_name, time=time_filter) }}">{{ begin_date|format_date }}</a></span>
        {% endfor %}
        </p>
    </li>
{% for account in [interval_balances[0]|get_or_create(account_name)] recursive %}
    <li{{ ' class=toggled' if account.account|collapse_account else '' }}>
        <p>
        <span class="account-cell depth-{{ loop.depth0 }} droptarget
        {{- '' if not account|length else ' has-children'}}
        " data-account-name="{{ account.account }}">
            {% if account|length %}<span class="expander"></span>{% endif %}
        {{ account_macros.account_name(account.account, last_segment=True) }}</span>
    {% for begin_date, end_date in dates %}
        {% if accumulate %}{% set begin_date = dates[-1][0] %}{% endif %}
        {% set budget = ledger.budgets.calculate(account.account, begin_date, end_date) %}
        {% set budget_children = ledger.budgets.calculate_children(account.account, begin_date, end_date) %}
        {% set current_account = interval_balances[loop.index0]|get_or_create(account.account) %}
        {% set balance = current_account.balance|cost_or_value(end_date) %}
        {% set balance_children = current_account|balance_children|cost_or_value(end_date) %}
        <span class="num other{{ ' has-balance' if (budget or not balance.is_empty()) else '' }}">
            <a href="{{ url_for('account', name=account.account, time=begin_date|string + ' - ' + end_date|string) }}">
            {% for pos in balance %}
                <span class="balance">
                    {{ render_budget(budget, pos.units.currency, pos.units.number) }}
                    <span class="number">{{ pos.units|format_amount }}</span>
                </span>
            {% endfor %}
            {% for currency, number in budget.items() if currency not in balance.currencies() %}
              <span class="balance">
              {{ render_budget(budget, currency) }}
              </span>
            {% endfor %}
            {% for pos in balance_children %}
                <span class="balance-children">
                    {{ render_budget(budget_children, pos.units.currency, pos.units.number) }}
                    <span class="number">{{ pos.units|format_amount }}</span>
                </span>
            {% endfor %}
            {% for currency, number in budget_children.items() if currency not in balance_children.currencies() %}
              <span class="balance-children">
              {{ render_budget(budget_children, currency) }}
              </span>
            {% endfor %}
            </a>
        </span>
    {% endfor %}
    </p>
    <ol>
    {{ loop(account.values()|sort(attribute='account')) }}
    </ol>
    </li>
{% endfor %}
</ol>
</tree-table>
{% endmacro %}
