<?php

declare(strict_types=1);

use Fisharebest\Webtrees\Auth;
use Fisharebest\Webtrees\Fact;
use Fisharebest\Webtrees\Gedcom;
use Fisharebest\Webtrees\I18N;
use Fisharebest\Webtrees\Individual;
use Fisharebest\Webtrees\Menu;
use Fisharebest\Webtrees\Module\ModuleChartInterface;
use Fisharebest\Webtrees\Services\ModuleService;
use Illuminate\Support\Collection;
use Ramsey\Uuid\Uuid;

/**
 * @var Individual|null   $individual
 * @var ModuleService     $module_service
 * @var Collection|Menu[] $menus
 */

if ($individual === null) {
    echo '<div class="wt-chart-box"></div>';

    return;
}

$module_service = app(ModuleService::class);

$menus = $module_service->findByComponent(ModuleChartInterface::class, $individual->tree(), Auth::user())->map(static function (ModuleChartInterface $module) use ($individual): ?Menu {
    return $module->chartBoxMenu($individual);
})->filter();

foreach ($individual->spouseFamilies() as $family) {
    $menus->push(new Menu('<strong>' . I18N::translate('Family with spouse') . '</strong>', $family->url()));
    $spouse  = $family->spouse($individual);
    if ($spouse && $spouse->canShow()) {
        $menus->push(new Menu($spouse->fullName(), $spouse->url()));
    }
    foreach ($family->children() as $child) {
        if ($child->canShow()) {
            $menus->push(new Menu($child->fullName(), $child->url()));
        }
    }
}

// Do not show these facts in the expanded chart boxes.
$exclude = [
    'FAM:CHAN',
    'FAM:CHIL',
    'FAM:HUSB',
    'FAM:NOTE',
    'FAM:OBJE',
    'FAM:RESN',
    'FAM:SOUR',
    'FAM:WIFE',
    'INDI:ADDR',
    'INDI:ALIA',
    'INDI:ASSO',
    'INDI:CHAN',
    'INDI:EMAIL',
    'INDI:FAMC',
    'INDI:FAMS',
    'INDI:NAME',
    'INDI:NOTE',
    'INDI:OBJE',
    'INDI:PHON',
    'INDI:RESI',
    'INDI:RESN',
    'INDI:SEX',
    'INDI:SOUR',
    'INDI:SSN',
    'INDI:SUBM',
    'INDI:TITL',
    'INDI:URL',
    'INDI:WWW',
    'INDI:_EMAIL',
    'INDI:_TODO',
    'INDI:_UID',
];

/** @var Collection|Fact[] $all_facts */
$all_facts = $individual->facts();
foreach ($individual->spouseFamilies() as $family) {
    foreach ($family->facts() as $fact) {
        $all_facts->push($fact);
    }
}

$all_facts = $all_facts->filter(static function (Fact $fact) use ($exclude): bool {
    return !in_array($fact->tag(), $exclude, true);
});

$all_facts = Fact::sortFacts($all_facts);

$id = Uuid::uuid4()->toString();
?>

<div class="wt-chart-box wt-chart-box-<?= strtolower($individual->sex()) ?> <?= $individual->isPendingAddition() ? 'wt-new' : '' ?> <?= $individual->isPendingDeletion() ? 'wt-old' : '' ?> overflow-hidden" data-xref="<?= e($individual->xref()) ?>" data-tree="<?= e($individual->tree()->name()) ?>">
    <?php if ($individual->canShow() && $individual->tree()->getPreference('SHOW_HIGHLIGHT_IMAGES')) : ?>
        <div class="wt-chart-box-thumbnail float-left mr-1">
            <?= $individual->displayImage(40, 50, 'crop', ['class' => 'wt-chart-box-thumbnail']) ?>
        </div>
    <?php endif ?>

    <?php if ($individual->canShow()) : ?>
        <div class="wt-chart-box-extra d-print-none float-right ml-1">
            <div class="dropdown position-static wt-chart-box-zoom">
                <a class="wt-chart-box-icon" href="#" role="button" id="chart-box-zoom-<?= $id ?>" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
                    <div ><?= view('icons/zoom-in') ?></div>
                    <div class="d-none"><?= view('icons/zoom-out') ?></div>
                    <span class="sr-only"><?= I18N::translate('Links') ?></span>
                </a>

                <div class="dropdown-menu dropdown-menu-right wt-chart-box-dropdown wt-chart-box-zoom-dropdown" style="position: inherit" aria-labelledby="#chart-box-zoom-<?= $id ?>">
                    <?php foreach ($all_facts as $fact) : ?>
                        <?= $fact->summary() ?>
                    <?php endforeach ?>
                </div>
            </div>

            <div class="dropdown position-static wt-chart-box-links">
                <a class="wt-chart-box-icon" href="#" role="button" id="chart-box-menu-<?= $id ?>" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
                    <i class="icon-pedigree" title="<?= I18N::translate('Links') ?>"></i>
                    <span class="sr-only"><?= I18N::translate('Links') ?></span>
                </a>

                <div class="dropdown-menu dropdown-menu-right wt-chart-box-dropdown wt-chart-box-links-dropdown" style="position: inherit" aria-labelledby="#chart-box-menu-<?= $id ?>">
                    <?php foreach ($menus as $menu) : ?>
                        <a class="dropdown-item p-1 <?= e($menu->getClass()) ?>" href="<?= e($menu->getLink()) ?>">
                            <?= $menu->getLabel() ?>
                        </a>
                    <?php endforeach ?>
                </div>
            </div>
        </div>
    <?php endif ?>

    <div class="wt-chart-box-name">
        <?php if ($individual->canShow()) : ?>
            <a href="<?= e($individual->url()) ?>"><?= $individual->fullName() ?></a>
        <?php else : ?>
            <?= $individual->fullName() ?>
        <?php endif ?>
    </div>

    <div class="wt-chart-box-name wt-chart-box-name-alt">
        <?= $individual->alternateName() ?>
    </div>

    <div class="wt-chart-box-lifespan">
        <?= $individual->lifespan() ?>
    </div>

    <div class="wt-chart-box-facts">
        <div class="wt-chart-box-fact small">
            <?php
            $opt_tags = preg_split('/\W/', $individual->tree()->getPreference('CHART_BOX_TAGS'), 0, PREG_SPLIT_NO_EMPTY);
            // Show BIRT or equivalent event

            foreach (Gedcom::BIRTH_EVENTS as $birttag) {
                if (!in_array($birttag, $opt_tags, true)) {
                    $event = $individual->facts([$birttag])->first();
                    if ($event instanceof Fact) {
                        echo $event->summary();
                        break;
                    }
                }
            }
            // Show optional events (before death)
            foreach ($opt_tags as $key => $tag) {
                if (!in_array($tag, Gedcom::DEATH_EVENTS, true)) {
                    $event = $individual->facts([$tag])->first();
                    if ($event instanceof Fact) {
                        echo $event->summary();
                        unset($opt_tags[$key]);
                    }
                }
            }
            // Show DEAT or equivalent event
            foreach (Gedcom::DEATH_EVENTS as $deattag) {
                $event = $individual->facts([$deattag])->first();
                if ($event instanceof Fact) {
                    echo $event->summary();
                    if (in_array($deattag, $opt_tags, true)) {
                        unset($opt_tags[array_search($deattag, $opt_tags, true)]);
                    }
                    break;
                }
            }
            // Show remaining optional events (after death)
            foreach ($opt_tags as $tag) {
                $event = $individual->facts([$tag])->first();
                if ($event instanceof Fact) {
                    echo $event->summary();
                }
            }
            ?>
        </div>
    </div>
</div>
