<?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\CacheCollector $collector
 */
$viewModel = $block->getViewModel();
$collector = $block->getCollector();
?>
<h2><?= $escaper->escapeHtml(__('Cache metrics')) ?></h2>
<div class="metrics">
    <?php foreach ($collector->getStats() as $key => $value): ?>
        <div class="metric">
            <span class="value"><?= /* @noEscape */ $value ?></span>
            <span class="label"><?= /* @noEscape */ ucwords(str_replace('stats_', '', $key)) ?></span>
        </div>
    <?php endforeach; ?>
    <div class="metric">
        <span class="value"><?= /* @noEscape */ $collector->getTotalTime() ?> ms</span>
        <span class="label"><?= $escaper->escapeHtml(__('Time')) ?></span>
    </div>
</div>
<h2><?= $escaper->escapeHtml(__('Cache configuration')) ?></h2>
<table class="cache-table">
    <thead>
        <tr>
            <th class="key"><?= $escaper->escapeHtml(__('Id')) ?></th>
            <th><?= $escaper->escapeHtml(__('Type')) ?></th>
            <th class="text-center"><?= $escaper->escapeHtml(__('Profiled Status')) ?></th>
            <th class="text-center"><?= $escaper->escapeHtml(__('Current Status')) ?></th>
            <th class="text-center"><?= $escaper->escapeHtml(__('Valid')) ?></th>
            <th class="text-right">
                <a data-url="<?= $escaper->escapeUrl($block->getUrl('_debug/cache/clean', ['type' => 'all'])) ?>"
                   class="btn btn-sm ajax-action">Clean all</a>
            </th>
        </tr>
    </thead>
    <tbody>
        <?php foreach ($collector->getCacheList() as $cache): ?>
            <tr>
                <td><?= /* @noEscape */ $cache->getId() ?></td>
                <td><?= /* @noEscape */ $cache->getCacheType() ?></td>
                <td class="text-center">
                    <?php if ($cache->getStatus()): ?>
                        <img src="
                        <?= $escaper->escapeUrl(
                            $block->getViewFileUrl('ClawRock_Debug::images/icon/yes.svg')
                        ); ?>
                        " alt="<?= $escaper->escapeHtmlAttr(__('Yes')) ?>"/>
                    <?php else: ?>
                        <img src="
                        <?= $escaper->escapeUrl(
                            $block->getViewFileUrl('ClawRock_Debug::images/icon/no.svg')
                        ); ?>
                        " alt="<?= $escaper->escapeHtmlAttr(__('No')) ?>"/>
                    <?php endif; ?>
                </td>
                <td class="text-center">
                    <?php if ($collector->getCacheCurrentStatus($cache->getId())): ?>
                        <img src="
                        <?= $escaper->escapeUrl(
                            $block->getViewFileUrl('ClawRock_Debug::images/icon/yes.svg')
                        ); ?>
                        " alt="<?= $escaper->escapeHtmlAttr(__('Yes')) ?>"/>
                    <?php else: ?>
                        <img src="
                        <?= $escaper->escapeUrl(
                            $block->getViewFileUrl('ClawRock_Debug::images/icon/no.svg')
                        ); ?>
                        " alt="<?= $escaper->escapeHtmlAttr(__('No')) ?>"/>
                    <?php endif; ?>
                </td>
                <td class="text-center">
                    <?php if (!$collector->getCacheCurrentStatus($cache->getId())): ?>
                        <img src="
                        <?= $escaper->escapeUrl(
                            $block->getViewFileUrl('ClawRock_Debug::images/icon/no.svg')
                        ); ?>
                        " alt="<?= $escaper->escapeHtmlAttr(__('No')) ?>"/>
                    <?php else: ?>
                        <?php if ($collector->isInvalidated($cache->getId())): ?>
                            <img src="
                            <?= $escaper->escapeUrl(
                                $block->getViewFileUrl('ClawRock_Debug::images/icon/no.svg')
                            ); ?>
                            " alt="<?= $escaper->escapeHtmlAttr(__('No')) ?>"/>
                        <?php else: ?>
                            <img src="
                            <?= $escaper->escapeUrl(
                                $block->getViewFileUrl('ClawRock_Debug::images/icon/yes.svg')
                            ); ?>
                            " alt="<?= $escaper->escapeHtmlAttr(__('Yes')) ?>"/>
                        <?php endif; ?>
                    <?php endif; ?>
                </td>
                <td class="text-right">
                    <?php if ($collector->getCacheCurrentStatus($cache->getId())): ?>
                        <a href="#"
                           data-url="<?= $escaper->escapeUrl($block->getUrl('_debug/cache/clean', [
                               'type' => $cache->getId(),
                           ])) ?>"
                           class="btn btn-sm ajax-action"><?= $escaper->escapeHtml(__('Clean')) ?></a>
                        <a href="#"
                           data-url="<?= $escaper->escapeUrl($block->getUrl('_debug/cache/disable', [
                               'type' => $cache->getId(),
                           ])) ?>"
                           class="btn btn-sm ajax-action"><?= $escaper->escapeHtml(__('Disable')) ?></a>
                    <?php else: ?>
                        <a href="#"
                           data-url="<?= $escaper->escapeUrl($block->getUrl('_debug/cache/enable', [
                               'type' => $cache->getId(),
                           ])) ?>"
                           class="btn btn-sm ajax-action"><?= $escaper->escapeHtml(__('Enable')) ?></a>
                    <?php endif; ?>
                </td>
            </tr>
        <?php endforeach; ?>
    </tbody>
</table>

<h2>
    <?= $escaper->escapeHtml(__('Cache Backend')) ?><br>
    <small><?= /* @noEscape */ $collector->getBackendName() ?></small>
</h2>
<table>
    <thead>
        <tr>
            <th><?= $escaper->escapeHtml(__('Key')) ?></th>
            <th><?= $escaper->escapeHtml(__('Value')) ?></th>
        </tr>
    </thead>
    <tbody>
        <?php foreach ($collector->getBackendOptions() as $key => $value): ?>
            <tr>
                <td class="nowrap"><?= /* @noEscape */ $key ?> </td>
                <td class="nowrap"><?= /* @noEscape */ $value ?></td>
            </tr>
        <?php endforeach; ?>
    </tbody>
</table>

<h2><?= $escaper->escapeHtml(__('Cache Calls')) ?></h2>
<table>
    <thead>
        <tr>
            <th>#</th>
            <th><?= $escaper->escapeHtml(__('Action')) ?></th>
            <th style="width: 100%;">Id</th>
            <th><?= $escaper->escapeHtml(__('Hit')) ?></th>
            <th><?= $escaper->escapeHtml(__('Time')) ?></th>
        </tr>
    </thead>
    <tbody>
        <?php /** @var \ClawRock\Debug\Model\ValueObject\CacheAction $action */ ?>
        <?php foreach ($collector->getCacheLog() as $i => $action): ?>
            <tr>
                <td class="nowrap"><?= /* @noEscape */ ++$i ?> </td>
                <td><?= /* @noEscape */ $action->getName() ?></td>
                <td>
                    <?= /* @noEscape */ $action->getId() ?: '-' ?>
                    <?php if ($action->hasTags()): ?>
                        <br>
                        <small>
                            <strong><?= $escaper->escapeHtml(__('Tags')) ?>:</strong>
                            <?= /* @noEscape */ implode(', ', $action->getTags()) ?>
                        </small>
                    <?php endif; ?>
                </td>
                <td>
                    <?php if (!$action->isLoad()): ?>
                        -
                    <?php else: ?>
                        <?php if ($action->isHit()): ?>
                            <img src="
                            <?= $escaper->escapeUrl(
                                $block->getViewFileUrl('ClawRock_Debug::images/icon/yes.svg')
                            ); ?>
                            " alt="<?= $escaper->escapeHtmlAttr(__('Yes')) ?>"/>
                        <?php else: ?>
                            <img src="
                            <?= $escaper->escapeUrl(
                                $block->getViewFileUrl('ClawRock_Debug::images/icon/no.svg')
                            ); ?>
                            " alt="<?= $escaper->escapeHtmlAttr(__('No')) ?>"/>
                        <?php endif; ?>
                    <?php endif ?>
                </td>
                <td class="nowrap">
                    <?= /* @noEscape */ $viewModel->getFormatter()->microtime($action->getTime()) ?> ms
                </td>
            </tr>
        <?php endforeach; ?>
    </tbody>
</table>
<script>
    (function () {
        var actions = document.querySelectorAll('.cache-table .ajax-action');
        for (var i = 0; i < actions.length; i++) {
            (function () {
                var button = actions[i],
                    url    = button.getAttribute('data-url');
                button.addEventListener('click', function (e) {
                    e.preventDefault();
                    Sfjs.request(url, function () {
                        window.location.reload();
                    });
                });
            })();
        }
    })();
</script>
