@php
use App\Models\Game;
use App\Models\GameList;
use App\Models\Platform;
use Illuminate\Support\Str;

$games = Game::wherePlatformId($platform->id)->orderBy('created_at', 'DESC')->paginate(25);
@endphp
<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">{{ $platform->name }}</h3>
                </div>
                <div class="card-body">
                    @foreach ($games as $game)

                    @php
                        $gameName = str_replace('/', '_', str_replace(' ', '_', $game->name));
                        $averageRating = round(GameList::whereGameId($game->id)->avg('rating') * 2) / 2;
                    @endphp

                    <!--begin::Game-->
                    <div class="d-flex align-items-start flex-grow-1">
                        <!--begin::Boxart-->
                        <div class="me-5">
                            <img class="mw-125px img-thumbnail" src="{{ Storage::url('assets/boxart/') . $game->boxart }}" alt="">
                        </div>
                        <!--end::Boxart-->
                        <!--begin::Info-->
                        <div class="d-flex flex-column">
                            <a href="/game/{{ $game->id }}/{{ $gameName }}" class="text-primary-900 text-hover-primary-600 fs-3 fw-bolder">{{ $game->name }}</a>
                            @if ($game->alt_titles)
                            @php
                            $altTitles = str_replace('|', ', ', $game->alt_titles);
                            @endphp
                            <span class="text-dark"><span class="fw-bolder">Alternative Titles:</span> {{ $altTitles }}</span>
                            @endif
                            <div class="rating">
                                @php
                                while ($averageRating > 0) {
                                    if ($averageRating == 0.5) {
                                        echo '<div class="rating-label me-2 checked">
                                            <i class="fas fa-star-half text-warning"></i>
                                        </div>';
                                        $averageRating = $averageRating - 0.5;
                                    } else {
                                        echo '<div class="rating-label me-2 checked">
                                            <i class="fas fa-star text-warning fs-6"></i>
                                        </div>';
                                        $averageRating--;
                                    }
                                }
                                @endphp
                            </div>
                            <p class="text-gray-800 fw-normal mb-5">{{ Str::words($game->description, 50) }}</p>
                            {{-- <div class="d-flex align-items-center mb-5">
                                <div class="btn btn-sm btn-light btn-color-muted btn-active-light-primary px-4 py-2 me-4">
                                <!--begin::Svg Icon | path: icons/duotune/communication/com012.svg-->
                                <span class="svg-icon svg-icon-3">
                                    <svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none">
                                        <path opacity="0.3" d="M20 3H4C2.89543 3 2 3.89543 2 5V16C2 17.1046 2.89543 18 4 18H4.5C5.05228 18 5.5 18.4477 5.5 19V21.5052C5.5 22.1441 6.21212 22.5253 6.74376 22.1708L11.4885 19.0077C12.4741 18.3506 13.6321 18 14.8167 18H20C21.1046 18 22 17.1046 22 16V5C22 3.89543 21.1046 3 20 3Z" fill="currentColor"></path>
                                        <rect x="6" y="12" width="7" height="2" rx="1" fill="currentColor"></rect>
                                        <rect x="6" y="7" width="12" height="2" rx="1" fill="currentColor"></rect>
                                    </svg>
                                </span>
                                <!--end::Svg Icon-->120</div>
                                <div class="btn btn-sm btn-light btn-color-muted btn-active-light-danger px-4 py-2">
                                <!--begin::Svg Icon | path: icons/duotune/general/gen030.svg-->
                                <span class="svg-icon svg-icon-2">
                                    <svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none">
                                        <path d="M18.3721 4.65439C17.6415 4.23815 16.8052 4 15.9142 4C14.3444 4 12.9339 4.73924 12.003 5.89633C11.0657 4.73913 9.66 4 8.08626 4C7.19611 4 6.35789 4.23746 5.62804 4.65439C4.06148 5.54462 3 7.26056 3 9.24232C3 9.81001 3.08941 10.3491 3.25153 10.8593C4.12155 14.9013 9.69287 20 12.0034 20C14.2502 20 19.875 14.9013 20.7488 10.8593C20.9109 10.3491 21 9.81001 21 9.24232C21.0007 7.26056 19.9383 5.54462 18.3721 4.65439Z" fill="currentColor"></path>
                                    </svg>
                                </span>
                                <!--end::Svg Icon-->15</div>
                            </div> --}}
                        </div>
                        <!--end::Info-->
                    </div>
                    <!--end::Game-->
                    <div class="separator my-5"></div>
                    @endforeach
                    <div class="d-flex justify-content-center">
                        {{ $games->links('partials.general._pagination') }}
                    </div>
                </div>
            </div>
        </div>
        <!--end::Col-->
        <!--begin::Col-->
        <div class="col-xxl-4">
            @if (Auth::check())
            {{ theme()->getView('partials/general/_donate-block') }}
            @endif
            {{ theme()->getView('partials/general/_recent-user-updates-block') }}
            {{ theme()->getView('partials/general/_most-popular-games-block') }}
        </div>
        <!--end::Col-->
    </div>
    <!--end::Row-->

</x-base-layout>
