<?php
/**
 * @var App\View\GlobalSections $sections
 * @var App\Http\RouterInterface $router
 * @var array $stations
 * @var array $sync_times
 * @var array $queue_totals
 */

$this->layout('main', [
    'title' => __('System Debugger'),
    'manual' => true,
]);

$sections->appendStart('bodyjs');
?>
<script src="<?= $this->assetUrl('dist/lib/luxon/luxon.min.js') ?>"></script>

<script>
    jQuery(function ($) {
        $('time[data-duration]').each(function () {
            $(this).text(luxon.DateTime.fromSeconds($(this).data('duration')).toRelative());
        });
    });
</script>
<?php
$sections->end();
?>

<h2 class="outside-card-header mb-1"><?= __('System Debugger') ?></h2>

<div class="row">
    <div class="col-md-6">
        <div class="card mb-3">
            <div class="card-header bg-primary-dark">
                <h2 class="card-title"><?= __('Clear Cache') ?></h2>
            </div>
            <div class="card-body">
                <p><?= __('Clearing the application cache may log you out of your session.') ?></p>
                <div class="buttons">
                    <a class="btn btn-sm btn-primary" role="button"
                       href="<?= $router->named('admin:debug:clear-cache') ?>">
                        <?= __('Clear Cache') ?>
                    </a>
                </div>
            </div>
        </div>
    </div>
    <div class="col-md-6">
        <div class="card mb-3">
            <div class="card-header bg-primary-dark">
                <h2 class="card-title"><?= __('Clear All Message Queues') ?></h2>
            </div>
            <div class="card-body">
                <p><?= __('This will clear any pending unprocessed messages in all message queues.') ?></p>
                <div class="buttons">
                    <a class="btn btn-sm btn-primary" role="button"
                       href="<?= $router->named('admin:debug:clear-queue') ?>">
                        <?= __('Clear All Message Queues') ?>
                    </a>
                </div>
            </div>
        </div>
    </div>
</div>

<div class="card mb-3">
    <div class="card-header bg-primary-dark">
        <h2 class="card-title"><?= __('Synchronization Tasks') ?></h2>
    </div>
    <table class="table">
        <colgroup>
            <col width="40%">
            <col width="20%">
            <col width="20%">
            <col width="20%">
        </colgroup>
        <thead>
        <tr>
            <th><?= __('Task Name') ?></th>
            <th><?= __('Last Run') ?></th>
            <th><?= __('Next Run') ?></th>
            <th><?= __('Actions') ?></th>
        </tr>
        </thead>
        <?php
        foreach ($sync_times as $task => $taskInfo): ?>
            <tr>
                <td>
                    <big><?= $task ?></big><br>
                    <?= $taskInfo['pattern'] ?>
                </td>
                <td>
                    <?php
                    if (0 === $taskInfo['time']): ?>
                        <?= __('Not Run') ?>
                    <?php
                    else: ?>
                        <time <?php
                              if ($taskInfo['time'] < time() - (60 * 60 * 3)): ?>class="text-danger"<?php
                        endif; ?> data-duration="<?= $taskInfo['time'] ?>"></time>
                    <?php
                    endif; ?>
                </td>
                <td>
                    <time data-duration="<?= $taskInfo['nextRun'] ?>"></time>
                </td>
                <td>
                    <div class="buttons">
                        <a class="btn btn-sm btn-primary" role="button" href="<?= $router->named(
                            'admin:debug:sync',
                            ['task' => rawurlencode($task)]
                        ) ?>">
                            <?= __('Run Task') ?>
                        </a>
                    </div>
                </td>
            </tr>
        <?php
        endforeach; ?>
    </table>
</div>

<div class="card mb-3">
    <div class="card-header bg-primary-dark">
        <h2 class="card-title"><?= __('Message Queues') ?></h2>
    </div>
    <div class="card-body">
        <div class="row">
            <?php
            foreach ($queue_totals as $queueType => $queueTotal): ?>
                <div class="col">
                    <h5 class="mb-0"><?= $queueType ?></h5>
                    <p><?= sprintf(__('%d queued messages'), $queueTotal) ?></p>

                    <div class="buttons">
                        <a class="btn btn-sm btn-primary" role="button" href="<?= $router->named(
                            'admin:debug:clear-queue',
                            ['queue' => $queueType]
                        ) ?>">
                            <?= __('Clear Queue') ?>
                        </a>
                    </div>
                </div>
            <?php endforeach; ?>
        </div>
    </div>
</div>

<div class="card">
    <div class="card-header bg-primary-dark">
        <h2 class="card-title"><?=__('Station-Specific Debugging')?></h2>
    </div>
    <div class="card-body pb-0">
        <ul class="nav nav-pills nav-pills-scrollable card-header-pills">
            <?php foreach ($stations as $station): ?>
                <li class="nav-item">
                    <a class="nav-link" role="tab" data-toggle="tab" aria-expanded="true" aria-controls="debug_station_<?=$station['id']?>" href="#debug_station_<?=$station['id']?>"><?=$this->e($station['name'])?></a>
                </li>
            <?php endforeach; ?>
        </ul>
    </div>
    <div class="tab-content">
        <?php foreach ($stations as $station): ?>
            <div class="card-body card-padding-sm tab-pane" id="debug_station_<?= $station['id'] ?>">
                <h3><?= $station['name'] ?></h3>

                <div class="row">
                    <div class="col-md-4">
                        <h5><?= __('AutoDJ Queue') ?></h5>

                        <div class="buttons">
                            <a class="btn btn-sm btn-primary" role="button" href="<?= $router->named(
                                'admin:debug:clear-station-queue',
                                ['station_id' => $station['id']]
                            ) ?>">
                                <?= __('Clear Queue') ?>
                            </a>
                            <a class="btn btn-sm btn-primary" role="button" href="<?= $router->named(
                                'admin:debug:nextsong',
                                ['station_id' => $station['id']]
                            ) ?>">
                                <?= __('Get Next Song') ?>
                            </a>
                        </div>
                    </div>
                    <div class="col-md-4">
                        <h5><?= __('Get Now Playing') ?></h5>

                        <div class="buttons">
                            <a class="btn btn-sm btn-primary" role="button" href="<?= $router->named(
                                'admin:debug:nowplaying',
                                ['station_id' => $station['id']]
                            ) ?>">
                                <?= __('Run Task') ?>
                            </a>
                        </div>
                    </div>
                    <?php
                    if ($station['backend_type'] === App\Radio\Enums\BackendAdapters::Liquidsoap->value): ?>
                        <div class="col-md-4">
                            <h5><?= __('Send Liquidsoap Telnet Command') ?></h5>

                            <form class="form" method="POST" action="<?= $router->named(
                                'admin:debug:telnet',
                                ['station_id' => $station['id']]
                            ) ?>">
                                <div class="form-group">
                                    <label for="<?= $station['id'] ?>_command"><?= __('Command') ?>:</label>
                                    <input id="<?= $station['id'] ?>_command" name="command" class="form-control"
                                           type="text">
                                </div>

                                <button type="submit" class="btn btn-primary"><?= __('Execute Command') ?></button>
                            </form>
                        </div>
                    <?php endif; ?>
                </div>
            </div>
        <?php endforeach; ?>
    </div>
</div>
