{% import '@CoreShopFrontend/Common/Macro/currency.html.twig' as currency %}

{# first sort by unit, if given #}
{% set sorted_unit_ranges = [] %}

{% for range in rule.ranges %}
    {% set unit_identifier = range.hasUnitDefinition ? range.unitDefinition.unit.translation(app.request.locale).fullLabel : 'default' %}
    {% set unit_definition = range.hasUnitDefinition ? range.unitDefinition : null %}
    {% if sorted_unit_ranges[unit_identifier] is not defined %}
        {% set sorted_unit_ranges = sorted_unit_ranges|merge({ (unit_identifier): {'ranges' : [] } }) %}
    {% endif %}
    {% set current_ranges = sorted_unit_ranges[unit_identifier]['ranges'] %}
    {% set current_ranges = current_ranges|merge([range]) %}
    {% set sorted_unit_ranges = sorted_unit_ranges|merge({ (unit_identifier): {'ranges' : current_ranges, 'unit_definition' : unit_definition } }) %}
{% endfor %}

<div class="product-quantity-price-ranges quantity-type-{{ rule.calculationBehaviour }} d-block">
    <h3>{{ 'coreshop.ui.product_quantity_price_rules.ranges_available'|trans }}</h3>
    <table class="table table-striped">
        <thead class="">
        <tr class="">
            <th scope="col">{{ 'coreshop.ui.product_quantity_price_rules.quantity'|trans }}</th>
            {% set unit_label = sorted_unit_ranges|length == 1 and sorted_unit_ranges|keys|first == 'default'
                ? (product.itemQuantityFactor > 1
                    ? 'coreshop.ui.product_quantity_price_rules.price_per_item_quantity_factor'|trans|format(product.itemQuantityFactor)
                    : 'coreshop.ui.product_quantity_price_rules.price_per_item'|trans)
                : (product.itemQuantityFactor > 1
                    ? 'coreshop.ui.product_quantity_price_rules.unit_price_quantity_factor'|trans|format(product.itemQuantityFactor)
                    : 'coreshop.ui.product_quantity_price_rules.unit_price'|trans)
            %}
            {% set unit_label_excl = sorted_unit_ranges|length == 1 and sorted_unit_ranges|keys|first == 'default'
                 ? (product.itemQuantityFactor > 1
                     ? 'coreshop.ui.product_quantity_price_rules.price_per_item_quantity_factor_excl'|trans|format(product.itemQuantityFactor)
                     : 'coreshop.ui.product_quantity_price_rules.price_per_item_excl'|trans)
                 : (product.itemQuantityFactor > 1
                     ? 'coreshop.ui.product_quantity_price_rules.unit_price_quantity_factor_excl'|trans|format(product.itemQuantityFactor)
                     : 'coreshop.ui.product_quantity_price_rules.unit_price_excl'|trans)
            %}
            <th scope="col">{{ unit_label }}</th>
            <th scope="col">{{ unit_label_excl }}</th>
        </tr>
        </thead>
        <tbody class="" {{ coreshop_test_html_attribute('product-quantity-price-rules') }}>

        {% for sorted_unit_range in sorted_unit_ranges %}

            {% if sorted_unit_range.unit_definition is not null %}
                <tr class="">
                    <td colspan="3"><strong>{{ sorted_unit_range.unit_definition.unit.translation(app.request.locale).fullLabel }}</strong></td>
                </tr>
            {% endif %}

            {% for range in sorted_unit_range.ranges %}
                <tr class=""
                    {% if sorted_unit_range.unit_definition is not null %}
                        {{ coreshop_test_html_attribute('product-quantity-price-rule-unit-' ~ sorted_unit_range.unit_definition.unit.id) }}
                    {% else %}
                        {{ coreshop_test_html_attribute('product-quantity-price-rule') }}
                    {% endif %}
                >
                    <td class="">

                        {{ 'coreshop.ui.product_quantity_price_rules.starting_from'|trans }}
                        <span {{ coreshop_test_html_attribute('product-quantity-price-rule-start', range.rangeStartingFrom) }}>
                            {{ range.rangeStartingFrom }}
                        </span>
                        {% if range.isHighlighted %}
                            <span class="badge badge-info">{{ 'coreshop.ui.product_quantity_price_rules.best_offer'|trans }}</span>
                        {% endif %}
                        {% if range.hasPseudoPrice %}
                            <small>{{ 'coreshop.ui.product_quantity_price_rules.list_price'|trans }}
                                <del>{{ currency.convertAndFormatFromCustomCurrency(range.currency.isoCode, range.pseudoPrice) }}</del>
                            </small>
                        {% elseif range.pricingBehaviour == 'percentage_decrease' %}
                            <small>{{ 'coreshop.ui.product_quantity_price_rules.you_save'|trans }}: {{ range.percentage }}%</small>
                        {% elseif range.pricingBehaviour == 'amount_decrease' %}
                            <small>{{ 'coreshop.ui.product_quantity_price_rules.you_save'|trans }}: {{ currency.convertAndFormatFromCustomCurrency(range.currency.isoCode, range.amount) }}</small>
                        {% endif %}
                    </td>
                    {% set price = currency.convertAndFormat(coreshop_quantity_price_rule_range_price(range, product, coreshop.context|merge({'unitDefinition' : sorted_unit_range.unit_definition }), true)) %}
                    {% set price_excl = currency.convertAndFormat(coreshop_quantity_price_rule_range_price(range, product, coreshop.context|merge({'unitDefinition' : sorted_unit_range.unit_definition }), false)) %}
                    <td class="">
                        <span class="range-price" {{ coreshop_test_html_attribute('product-quantity-price-rule-price-inc', price) }}>
                            {{ price }}
                        </span>
                    </td>
                    <td class="">
                        <span class="range-price" {{ coreshop_test_html_attribute('product-quantity-price-rule-price-exc', price_excl) }}>
                            {{ price_excl }}
                        </span>
                    </td>
                </tr>
            {% endfor %}

        {% endfor %}
        </tbody>
    </table>
</div>
