<?php

declare(strict_types=1);

use Fisharebest\Webtrees\Auth;
use Fisharebest\Webtrees\Fact;
use Fisharebest\Webtrees\Http\RequestHandlers\AddMediaFileModal;
use Fisharebest\Webtrees\Http\RequestHandlers\DeleteRecord;
use Fisharebest\Webtrees\Http\RequestHandlers\EditRecordPage;
use Fisharebest\Webtrees\Http\RequestHandlers\LinkMediaToFamilyModal;
use Fisharebest\Webtrees\Http\RequestHandlers\LinkMediaToIndividualModal;
use Fisharebest\Webtrees\Http\RequestHandlers\LinkMediaToSourceModal;
use Fisharebest\Webtrees\Http\RequestHandlers\ReorderMediaFilesPage;
use Fisharebest\Webtrees\I18N;
use Fisharebest\Webtrees\Media;
use Illuminate\Support\Collection;

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

?>

<div class="dropdown wt-page-menu">
    <button class="btn btn-primary dropdown-toggle wt-page-menu-button" type="button" id="page-menu" data-bs-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
        <?= view('icons/menu') ?>
        <?= I18N::translate('edit') ?>
    </button>

    <div class="dropdown-menu dropdown-menu-end wt-page-menu-items" aria-labelledby="page-menu">
        <a class="dropdown-item" href="#" data-wt-href="<?= e(route(AddMediaFileModal::class, ['tree' => $record->tree()->name(), 'xref' => $record->xref()])) ?>" data-bs-target="#wt-ajax-modal" data-bs-toggle="modal">
            <?= view('icons/add') ?>
            <?= I18N::translate('Add a media file') ?>
        </a>

        <?php if ($record->mediaFiles()->count() >= 2) : ?>
            <a class="dropdown-item" href="<?= route(ReorderMediaFilesPage::class, ['xref' => $record->xref(), 'tree' => $record->tree()->name()]) ?>">
                <?= view('icons/reorder') ?>
                <?= I18N::translate('Re-order media files') ?>
            </a>
        <?php endif ?>

        <?php if ($record->missingFacts() !== []) : ?>
            <div class="dropdown-divider"></div>

            <?= view('record-page-menu-facts', ['record' => $record]) ?>
        <?php endif ?>

        <?php if ($clipboard_facts->isNotEmpty()) : ?>
            <div class="dropdown-divider"></div>

            <?= view('record-page-menu-clipboard', ['clipboard_facts' => $clipboard_facts, 'record' => $record]) ?>
        <?php endif ?>

        <div class="dropdown-divider"></div>

        <a class="dropdown-item" href="#" data-wt-href="<?= e(route(LinkMediaToIndividualModal::class, ['tree' => $record->tree()->name(), 'xref' => $record->xref()])) ?>" data-bs-target="#wt-ajax-modal" data-bs-toggle="modal">
            <?= view('icons/link') ?>
            <?= I18N::translate('Link this media object to an individual') ?>
        </a>

        <a class="dropdown-item" href="#" data-wt-href="<?= e(route(LinkMediaToFamilyModal::class, ['tree' => $record->tree()->name(), 'xref' => $record->xref()])) ?>" data-bs-target="#wt-ajax-modal" data-bs-toggle="modal">
            <?= view('icons/link') ?>
            <?= I18N::translate('Link this media object to a family') ?>
        </a>

        <a class="dropdown-item" href="#" data-wt-href="<?= e(route(LinkMediaToSourceModal::class, ['tree' => $record->tree()->name(), 'xref' => $record->xref()])) ?>" data-bs-target="#wt-ajax-modal" data-bs-toggle="modal">
            <?= view('icons/link') ?>
            <?= I18N::translate('Link this media object to a source') ?>
        </a>

        <?php if (Auth::isManager($record->tree())) : ?>
            <div class="dropdown-divider"></div>

            <a class="dropdown-item" href="<?= route(EditRecordPage::class, ['xref' => $record->xref(), 'tree' => $record->tree()->name()]) ?>">
                <?= view('icons/edit') ?>
                <?= I18N::translate('Edit') ?>
            </a>
        <?php endif ?>

        <div class="dropdown-divider"></div>

        <a class="dropdown-item" href="#" data-wt-confirm="<?= I18N::translate('Are you sure you want to delete “%s”?', strip_tags($record->fullName())) ?>" data-wt-post-url="<?= e(route(DeleteRecord::class, ['tree' => $record->tree()->name(), 'xref' => $record->xref()])) ?>">
            <?= view('icons/delete') ?>
            <?= I18N::translate('Delete') ?>
        </a>
    </div>
</div>
