<?php
    /**
     * @var AppView $this
     */

    use App\View\AppView;

    $isLoggedIn      = $this->Auth->isLoggedIn();
    $isImpersonating = $isLoggedIn && $this->Auth->isImpersonating();

    $name  = $this->Auth->get('name') ?? "Guest";
    $name  = $this->Auth->getUserData('name') ?? "Guest";
    $email = $this->Auth->getUserData('email');
?>

<!-- User Profile -->
<div class="dropdown">
    <a
        href="javascript: void(0);"
        class="dropdown-toggle no-arrow d-flex align-items-center justify-content-center bg-white rounded-circle border shadow-sm mx-1 mx-lg-2 w-40px h-40px"
        role="button"
        data-bs-toggle="dropdown"
        data-bs-auto-close="outside"
        aria-haspopup="true"
        aria-expanded="false"
        data-bs-offset="0,10"
    >
        <?php
            $class = '';
            if (!$isLoggedIn) {
                $class = 'text-muted';
            } elseif ($isImpersonating) {
                $class = 'text-warning';
            }
        ?>

        <div
            class="avatar avatar-circle avatar-sm d-flex justify-content-center align-items-center <?= $class ?>"
        >
            <i class="fa-solid fa-user"></i>
        </div>
    </a>

    <div class="dropdown-menu">
        <div class="dropdown-item-text">
            <div class="d-flex align-items-center">
                <div class="avatar avatar-sm avatar-circle">
                    <span class="avatar-title <?= $isImpersonating ? 'text-bg-warning' : '' ?>"><?= $name[0] ?></span>
                </div>
                <div class="flex-grow-1 ms-3">
                    <?php if ($isImpersonating): ?>
                        <div class="fs-5">Temporarily signed in as:</div>
                    <?php endif ?>
                    <h4 class="mb-0"><?= $name ?></h4>
                    <p class="card-text"><?= $email ?></p>
                </div>
            </div>
        </div>
        <hr class="dropdown-divider">
        <?php if ($this->Identity->isLoggedIn()): ?>

            <?= $this->Html->link(
                '<i class="fa-user-edit fa fa-fw me-3"></i>My Account Settings',
                '/Icing.Users::profile',
                ['class' => 'dropdown-item', 'data-fetch' => 'modal', 'data-fetch-size' => 'md']
            ) ?>

            <?= $this->Html->link(
                '<i class="fa-solid fa-user-shield fa-fw me-3"></i>Configure Two-Factor Authentication',
                '/Icing.Auth::setupTwoFactorAuthentication',
                ['class' => 'dropdown-item', 'target' => '_blank']
            ) ?>

            <hr class="dropdown-divider">

            <a
                class="dropdown-item"
                href="/logout"
            >
                <i class="fa-solid fa-sign-out fa-fw me-3"></i>
                Sign Out
            </a>
        <?php else: ?>
            <a
                class="dropdown-item"
                href="/login"
            >
                <i class="fa-solid fa-sign-in fa-fw me-3"></i>
                Sign In
            </a>
        <?php endif ?>


    </div>
</div>
