<?php

declare(strict_types=1);

use Fisharebest\Webtrees\Auth;
use Fisharebest\Webtrees\Fact;
use Fisharebest\Webtrees\Http\RequestHandlers\DeleteFact;
use Fisharebest\Webtrees\Http\RequestHandlers\EditMediaFileModal;
use Fisharebest\Webtrees\I18N;
use Fisharebest\Webtrees\Media;
use Fisharebest\Webtrees\Registry;
use Fisharebest\Webtrees\Tree;
use Illuminate\Support\Collection;

/**
 * @var Collection<int,Fact> $clipboard_facts
 * @var Media                $record
 * @var Tree                 $tree
 */

?>

<table class="table wt-facts-table">
    <?php foreach ($record->mediaFiles() as $media_file) : ?>
        <tr class="<?= $media_file->isPendingAddition() ? 'wt-new' : '' ?><?= $media_file->isPendingDeletion() ? 'wt-old' : '' ?>">
            <th scope="row">
                <?= I18N::translate('Media file') ?>
                <?php if ($record->canEdit() && !$media_file->isPendingDeletion()) : ?>
                    <div class="pt-2 wt-fact-edit-links">
                        <a class="btn btn-link" href="#" data-bs-toggle="modal" data-bs-target="#wt-ajax-modal" data-wt-href="<?= route(EditMediaFileModal::class, ['tree' => $record->tree()->name(), 'xref' => $record->xref(), 'fact_id' => $media_file->factId()]) ?>" title="<?= I18N::translate('Edit') ?>">
                            <?= view('icons/edit') ?>
                            <span class="visually-hidden">
                            <?= I18N::translate('Edit') ?>
                        </span>
                        </a>

                        <?php if (count($record->mediaFiles()) > 1) : ?>
                            <a class="btn btn-link" href="#" data-wt-confirm="<?= I18N::translate('Are you sure you want to delete this fact?') ?>" data-wt-post-url="<?= e(route(DeleteFact::class, ['tree' => $record->tree()->name(), 'xref' => $record->xref(), 'fact_id' => $media_file->factId()])) ?>" title="<?= I18N::translate('Delete') ?>">
                                <?= view('icons/delete') ?>
                                <span class="visually-hidden">
                            <?= I18N::translate('Delete') ?>
                        </span>
                            </a>

                        <?php endif ?>
                    </div>
                <?php endif ?>
            </th>
            <td class="d-flex justify-content-between">
                <div>
                    <?php if ($media_file->isExternal()) : ?>
                        <?= Registry::elementFactory()->make('OBJE:FILE')->labelValue($media_file->filename(), $record->tree()) ?>
                    <?php elseif (Auth::isEditor($record->tree())) :?>
                        <?php if (!$media_file->fileExists()) : ?>
                            <p class="alert alert-danger">
                                <?= I18N::translate('The file “%s” does not exist.', e($media_file->filename())) ?>
                            </p>
                        <?php endif ?>
                        <?= Registry::elementFactory()->make('OBJE:FILE')->labelValue($media_file->filename(), $record->tree()) ?>
                    <?php endif ?>

                    <?= Registry::elementFactory()->make('OBJE:FILE:TITL')->labelValue($media_file->title(), $record->tree()) ?>
                    <?= Registry::elementFactory()->make('OBJE:FILE:FORM:TYPE')->labelValue($media_file->type(), $record->tree()) ?>
                    <?= Registry::elementFactory()->make('OBJE:FILE:FORM')->labelValue($media_file->format(), $record->tree()) ?>
                </div>

                <div>
                    <?php if (!$media_file->isExternal()) : ?>
                        <?php if (explode('/', $media_file->mimeType())[0] === 'audio') : ?>
                        <audio controls>
                            <source src="<?= e($media_file->downloadUrl('inline')) ?>" type="<?= $media_file->mimeType() ?>">
                        </audio>
                        <?php elseif (explode('/', $media_file->mimeType())[0] === 'video') : ?>
                            <video controls width="300">
                                <source src="<?= e($media_file->downloadUrl('inline')) ?>" type="<?= $media_file->mimeType() ?>">
                            </video>
                        <?php else : ?>
                            <?= $media_file->displayImage(200, 150, 'contain', []) ?>
                        <?php endif ?>
                        <?php if ($record->tree()->getPreference('SHOW_MEDIA_DOWNLOAD') >= Auth::accessLevel($record->tree()) && $media_file->fileExists()) : ?>
                            <br>
                            <a href="<?= e($media_file->downloadUrl('attachment')) ?>">
                                <?= I18N::translate('Download file') ?>
                            </a>
                        <?php endif ?>
                    <?php endif ?>
                </div>
            </td>
        </tr>
    <?php endforeach ?>

    <?php foreach ($record->facts()->filter(fn (Fact $fact): bool => $fact->tag() !== 'OBJE:FILE') as $fact) : ?>
        <?= view('fact', ['fact' => $fact, 'record' => $record]) ?>
    <?php endforeach ?>
</table>
