@php
use App\Models\Game;
use App\Models\GameList;
use App\Models\Genre;
use App\Models\Platform;

// Turn $game->genre_ids into an array.
$genres = explode(',', $game->genre_ids);

// Determine the friendly genre name(s).
$genreNames = [];
foreach ($genres as $genre) {
    array_push($genreNames, Genre::whereId($genre)->value('name'));
}

// Create a comma separated string of the genre names.
$genreNames = implode(', ', $genreNames);

// Determine what other platforms this game exists on.
$alsoExistsOn = Game::whereName($game->name)->where('id', '!=', $game->id)->get();

$platforms = [];
if (count($alsoExistsOn) >= 1) {
    foreach ($alsoExistsOn as $alsoExistsOnGame) {
        array_push($platforms, [
            'id' => $alsoExistsOnGame->id,
            'name' => Platform::whereId($alsoExistsOnGame->platform_id)->value('name'),
        ]);
    }
}
@endphp

{{-- Check to see if the user is logged in. --}}
@if (Auth::check())
    {{-- Check to see if the user has this game on their list already. --}}
    @php
    $hasGame = GameList::whereUserId(Auth::user()->id)->whereGameId($game->id)->first();
    @endphp

    {{-- Check to see if the user has this game favorited already. --}}

    {{-- Check to see if the user has this wishlisted already. --}}
@endif
<x-base-layout>

    <!--begin::Row-->
    <div class="row gy-5 g-xl-8">
        <!--begin::Col-->
        <div class="col-xxl-8">
            <div class="card shadow-sm mb-2">
                <div class="card-header">
                    <h3 class="card-title">{{ $game->name }}</h3>
                    @if (Auth::check())
                    <div class="card-toolbar">
                        <!--begin::Menu wrapper-->
                        <div>
                            <!--begin::Toggle-->
                            <button type="button" class="btn btn-secondary btn-sm rotate" data-kt-menu-trigger="click" data-kt-menu-placement="bottom-start" data-kt-menu-offset="30px, 30px">
                                Actions
                                <i class="fas fa-angle-down rotate-180 ms-3 me-0"></i>
                            </button>
                            <!--end::Toggle-->

                            <!--begin::Menu-->
                            <div class="menu menu-sub menu-sub-dropdown menu-column menu-rounded menu-gray-800 menu-state-bg-light-primary fw-bold w-200px py-4" data-kt-menu="true">
                                <!--begin::Menu item-->
                                <div class="menu-item px-3 my-1">
                                    @if ($hasGame)
                                    <a href="{{ route('games.list.edit', $hasGame) }}" class="menu-link px-3">Edit List Entry</a>
                                    @else
                                    <a href="{{ route('games.list.create', ['gameId' => $game->id]) }}" class="menu-link px-3">
                                        Add to List
                                    </a>
                                    @endif
                                </div>
                                <!--end::Menu item-->

                                <!--begin::Menu item-->
                                {{-- <div class="menu-item px-3 my-1">
                                    <a href="#" class="menu-link px-3 disabled">
                                        Add to Favorites
                                    </a>
                                </div> --}}
                                <!--end::Menu item-->

                                <!--begin::Menu item-->
                                {{-- <div class="menu-item px-3 my-1">
                                    <a href="#" class="menu-link px-3 disabled">
                                        Add to Wishlist
                                    </a>
                                </div> --}}
                                <!--end::Menu item-->

                                <!--begin::Menu item-->
                                {{-- <div class="menu-item px-3 my-1">
                                    <a href="#" class="menu-link px-3 disabled">
                                        Write a Review
                                    </a>
                                </div> --}}
                                <!--end::Menu item-->
                            </div>
                            <!--end::Menu-->
                        </div>
                        <!--end::Dropdown wrapper-->
                    </div>
                </div>
                    @else
                </div>
                    @endif
                <div class="card-body">
                    {!! Markdown::parse($game->description) !!}
                    @if ($game->source)
                    <p class="mb-0"><a href="{{ $game->source }}" target="_blank">Source</a>.</p>
                    @endif
                </div>
            </div>
            <div class="card shadow-sm">
                <div class="card-header">
                    <h3 class="card-title">Comments</h3>
                </div>
                <div class="card-body">
                    <p class="mb-0">Coming soon...</p>
                </div>
            </div>
        </div>
        <!--end::Col-->

        <!--begin::Col-->
        <div class="col-xxl-4">
            <div class="card shadow-sm mb-2">
                <div class="card-header">
                    <h3 class="card-title">Game Information</h3>
                </div>
                <div class="card-body">
                    <div class="d-flex justify-content-center"><img class="img-fluid img-thumbnail" src="{{ Storage::url('assets/boxart/') .  $game->boxart }}" alt="image"></div>
                    <dl class="row mt-5">
                        @if ($game->alt_titles)
                            @php
                            $altTitles = str_replace('|', ', ', $game->alt_titles);
                            @endphp
                        <dt class="col-sm-5 fw-bolder">Alternative Titles</dt>
                        <dd class="col-sm-7">{{ $altTitles }}</dd>
                        @endif
                        <dt class="col-sm-5 fw-bolder">Platform</dt>
                        <dd class="col-sm-7">{{ $game->platform()->get()[0]->name }}</dd>
                        <dt class="col-sm-5 fw-bolder">Developer(s)</dt>
                        <dd class="col-sm-7">{{ $game->developers ?? '-' }}</dd>
                        <dt class="col-sm-5 fw-bolder">Publishers(s)</dt>
                        <dd class="col-sm-7">{{ $game->publishers ?? '-' }}</dd>
                        <dt class="col-sm-5 fw-bolder">Genres(s)</dt>
                        <dd class="col-sm-7">{{ $genreNames }}</dd>
                        @if ($game->na_release_date)
                        <dt class="col-sm-5 fw-bolder">NA Release Date</dt>
                        <dd class="col-sm-7">{{ $game->na_release_date }}</dd>
                        @endif
                        @if ($game->jp_release_date)
                        <dt class="col-sm-5 fw-bolder">JP Release Date</dt>
                        <dd class="col-sm-7">{{ $game->jp_release_date }}</dd>
                        @endif
                        @if ($game->eu_release_date)
                        <dt class="col-sm-5 fw-bolder">EU Release Date</dt>
                        <dd class="col-sm-7">{{ $game->eu_release_date }}</dd>
                        @endif
                        @if ($game->aus_release_date)
                        <dt class="col-sm-5 fw-bolder">AUS Release Date</dt>
                        <dd class="col-sm-7">{{ $game->aus_release_date }}</dd>
                        @endif
                        @if ($game->esrb_rating)
                        <dt class="col-sm-5 fw-bolder">ESRB Rating</dt>
                        <dd class="col-sm-7">{{ $game->esrb_rating ?? '-' }}</dd>
                        @endif
                        @if ($game->pegi_rating)
                        <dt class="col-sm-5 fw-bolder">PEGI Rating</dt>
                        <dd class="col-sm-7">{{ $game->pegi_rating ?? '-' }}</dd>
                        @endif
                        @if ($game->cero_rating)
                        <dt class="col-sm-5 fw-bolder">CERO Rating</dt>
                        <dd class="col-sm-7">{{ $game->cero_rating ?? '-' }}</dd>
                        @endif
                        @if ($game->acb_rating)
                        <dt class="col-sm-5 fw-bolder">ACB Rating</dt>
                        <dd class="col-sm-7">{{ $game->acb_rating ?? '-' }}</dd>
                        @endif
                        <dt class="col-sm-5 fw-bolder">MVGL User Score</dt>
                        <dd class="col-sm-7">{{ (round(GameList::whereGameId($game->id)->avg('rating'), 1)) != 0 ? round(GameList::whereGameId($game->id)->avg('rating'), 1) : '-'}} {{ (GameList::whereGameId($game->id)->where('rating', '>=', 1)->count()) != 0 ? ' by ' . GameList::whereGameId($game->id)->where('rating', '>=', 1)->count() . ' user(s) '  : '' }}</dd>
                        <dt class="col-sm-5 fw-bolder">MVGL Difficulty</dt>
                        <dd class="col-sm-7">{{ GameList::whereGameId($game->id)->avg('difficulty') ?? '-' }}</dd>
                        <dt class="col-sm-5 fw-bolder">Composer(s)</dt>
                        <dd class="col-sm-7">{{ $game->composers ?? '-' }}</dd>
                        @if ($game->website)
                        <dt class="col-sm-5 fw-bolder">Website</dt>
                        <dd class="col-sm-7"><a href="{{ $game->website }}">Official Website</a></dd>
                        @endif
                        <dt class="col-sm-5 fw-bolder">Added by</dt>
                        <dd class="col-sm-7">{{ GameList::whereGameId($game->id)->count() ?? '0' }} User(s)</dd>
                    </dl>
                    @if (count($alsoExistsOn) >= 1)
                    <p class="lh-lg">This game also exists on:<br />
                        @foreach ($platforms as $platform)
                        <a class="mt-1" href="/game/{{ $platform['id'] }}/{{ str_replace('/', '_', str_replace(' ', '_', $game->name)) }}"><span class="badge badge-primary">{{ $platform['name'] }}</span></a>
                        @endforeach
                    </p>
                    @endif
                </div>
            </div>
            <div class="card shadow-sm mb-2">
                <div class="card-header">
                    <h3 class="card-title">Recently Completed By</h3>
                </div>
                <div class="card-body">
                    <p class="mb-0">Coming soon...</p>
                </div>
            </div>
            <div class="card shadow-sm">
                <div class="card-header">
                    <h3 class="card-title">Recently Favorited By</h3>
                </div>
                <div class="card-body">
                    <p class="mb-0">Coming soon...</p>
                </div>
            </div>
        </div>
        <!--end::Col-->
    </div>
    <!--end::Row-->

</x-base-layout>
