<?php
declare(strict_types=1);

/**
 * @var \Magento\Framework\View\Element\Template $block
 * @var \Magento\Framework\Escaper $escaper
 * @var \ClawRock\Debug\Model\View\Profiler $viewModel
 * @var \ClawRock\Debug\Model\Collector\ModelCollector $collector
 */
$viewModel = $block->getViewModel();
$collector = $block->getCollector();
?>
<h2><?= $escaper->escapeHtml(__('Model CRUD metrics')) ?></h2>
<div class="metrics">
    <?php foreach ($collector->getMetrics() as $metric => $value): ?>
        <div class="metric">
            <span class="value"><?= /* @noEscape */$value ?></span>
            <span class="label"><?= /* @noEscape */ $collector->getMetricName($metric) ?></span>
        </div>
    <?php endforeach; ?>
    <div class="metric">
        <span class="value"><?= /* @noEscape */ $collector->getTime(); ?> ms</span>
        <span class="label"><?= $escaper->escapeHtml(__('Total time')) ?></span>
    </div>
</div>
<div class="sf-tabs">
    <div class="tab">
        <?php $log = $collector->getLog(); ?>
        <h3 class="tab-title"><?= $escaper->escapeHtml(__('All')) ?> <span class="badge"><?= count($log) ?></span></h3>
        <div id="all-crud-operations" class="tab-content">
            <?php foreach ($collector->getOperations() as $operation): ?>
                <span data-toggle="crud-operation" data-operation="<?= /* @noEscape */$operation ?>" class="label">
                    <strong><?= /* @noEscape */strtoupper($operation) ?></strong>
                    <span class="count">
                        (<span><?= /* @noEscape */ $collector->getMetric($operation) ?></span>)
                    </span>
                </span>
            <?php endforeach; ?>
            <table>
                <thead>
                    <tr>
                        <th><?= $escaper->escapeHtml(__('Class')) ?></th>
                        <th><?= $escaper->escapeHtml(__('Action')) ?></th>
                        <th><?= $escaper->escapeHtml(__('Time')) ?></th>
                    </tr>
                </thead>
                <tbody>
                    <?php /** @var \ClawRock\Debug\Model\ValueObject\ModelAction $action */ ?>
                    <?php foreach ($log as $action): ?>
                        <tr data-operation="<?= /* @noEscape */ $action->getName() ?>">
                            <td>
                                <strong><?= /* @noEscape */ $action->getModel() ?></strong><br>
                                <?= /* @noEscape */ $viewModel->renderTrace($action->getTrace()) ?>
                            </td>
                            <td><?= /* @noEscape */ $action->getName() ?></td>
                            <td class="nowrap"><?= /* @noEscape */ $action->getTime() ?> ms</td>
                        </tr>
                    <?php endforeach; ?>
                </tbody>
            </table>
        </div>
    </div>
    <div class="tab">
        <h3 class="tab-title">
            <?= $escaper->escapeHtml(__('Load calls in loops')) ?>
            <span class="badge"><?= /* @noEscape */ $collector->getLoopLoadMetric() ?></span>
        </h3>
        <div class="tab-content">
            <?php if (!$collector->getLoopLoadMetric()): ?>
                <div class="empty">
                    <p><?= $escaper->escapeHtml(__('No load calls in loops detected')) ?></p>
                </div>
            <?php else: ?>
                <table>
                    <thead>
                        <tr>
                            <th><?= $escaper->escapeHtml(__('Class')) ?></th>
                            <th><?= $escaper->escapeHtml(__('Count')) ?></th>
                            <th><?= $escaper->escapeHtml(__('Time')) ?></th>
                        </tr>
                    </thead>
                    <tbody>
                    <?php /** @var \ClawRock\Debug\Model\ValueObject\LoopModelAction $action */ ?>
                        <?php foreach ($collector->getLoadLoops() as $action): ?>
                            <tr>
                                <td>
                                    <strong><?= /* @noEscape */ $action->getModel() ?></strong><br>
                                    <?= /* @noEscape */ $viewModel->renderTrace($action->getTrace()) ?>
                                </td>
                                <td class="nowrap"><?= /* @noEscape */ $action->getCount() ?></td>
                                <td class="nowrap"><?= /* @noEscape */ $action->getTime() ?> ms</td>
                            </tr>
                        <?php endforeach; ?>
                    </tbody>
                </table>
            <?php endif; ?>
        </div>
    </div>
</div>
<script>
    require(['jquery'], function($) {
        var allOperations = $('#all-crud-operations'),
            crudOperations = allOperations.find('span[data-toggle="crud-operation"]');

        crudOperations.click(function () {
            var $this = $(this),
                operation = $this.data('operation'),
                makeActive = !$this.hasClass('status-success');

            crudOperations.removeClass('status-success');
            allOperations.removeAttr('data-operation');

            if (makeActive) {
                $this.addClass('status-success');
                allOperations.attr('data-operation', operation);
            }
        });
    });
</script>
<style>
    #all-crud-operations span[data-toggle="crud-operation"] {
        cursor: pointer;
    }
    <?php foreach ($collector->getOperations() as $name): ?>
        <?php // phpcs:ignore Generic.Files.LineLength.TooLong ?>
        #all-crud-operations[data-operation="<?= /* @noEscape */ $name ?>"] tbody > tr:not([data-operation="<?= /* @noEscape */ $name ?>"]) {
            display: none;
        }
    <?php endforeach;?>
</style>




