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

// 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.
    $hasGame = GameList::whereUserId(Auth::user()->id)->whereGameId($gameList->game_id)->first();
}

// Fetch the game object.
$game = Game::findOrFail($gameList->game_id);

// 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
<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>
                    <div class="card-toolbar">
                        <div class="text-light-primary bg-primary p-3 rounded">
                            {{ $game->platform()->get()[0]->name }}
                        </div>
                    </div>
                </div>
                <div class="card-body">
                    @if (count($errors) > 0)
                    <!--begin::Alert-->
                    <div class="alert alert-danger d-flex align-items-center p-5 mb-10">
                        <span class="svg-icon svg-icon-2hx svg-icon-danger me-4">
                            <i class="fad fa-times-circle"></i>
                        </span>
                        <div class="d-flex flex-column">
                            <h4 class="mb-1 text-danger">Error Editing List Entry</h4>
                            <ul>
                                @foreach ($errors->all() as $error)
                                <li>{{ $error }}</li>
                                @endforeach
                            </ul>
                        </div>
                    </div>
                    <!--end::Alert-->
                    @endif

                    @if (!$hasGame)
                    <!--begin::Alert-->
                    <div class="alert alert-danger d-flex align-items-center p-5 mb-0">
                        <span class="svg-icon svg-icon-2hx svg-icon-danger me-4">
                            <i class="fad fa-times-circle"></i>
                        </span>
                        <div class="d-flex flex-column">
                            <h4 class="mb-1 text-danger">Error Editing List Entry</h4>
                            <p>You do not appear to have this game on your list.</p>
                        </div>
                    </div>
                    <!--end::Alert-->
                    @else
                    <form class="form w-100" novalidate="novalidate" action="{{ route('games.list.update', $gameList) }}" method="POST">
                        @csrf
                        @method('PUT')

                        @isset ($game->alt_titles)
                        <!--begin::Input group-->
                        <div class="fv-row col-md-6 mb-5">
                            <!--begin::Label-->
                            <label class="form-label fs-6 fw-bolder text-dark">Display Title</label>
                            <!--end::Label-->
                            <!--begin::Input-->
                            <select class="form-select" data-control="select2" data-placeholder="Select an option" data-allow-clear="true" name="name">
                                <option></option>
                                @php
                                $altTitles = explode('|', $game->alt_titles);
                                @endphp
                                @foreach ($altTitles as $altTitle)
                                    <option value="{{ $altTitle }}" @selected(old('name') ?? $gameList->name == $altTitle)>
                                        {{ $altTitle }}
                                    </option>
                                @endforeach
                            </select>
                            <div class="text-muted mt-1">The title of the game that will be displayed on your list. If nothing is selected the default title will be used.</div>
                            <!--end::Input-->
                        </div>
                        <!--end::Input group-->
                        @else
                        <!--begin::Input group-->
                        <div class="fv-row col-md-6 mb-5">
                            <!--begin::Label-->
                            <label class="form-label fs-6 fw-bolder text-dark">Name</label>
                            <!--end::Label-->
                            <!--begin::Input-->
                            <input class="form-control" name="name" value="{{ $game->name }}" disabled/>
                            <!--end::Input-->
                        </div>
                        <!--end::Input group-->
                        @endisset

                        <!--begin::Input group-->
                        <div class="fv-row col-md-6 mb-5">
                            <!--begin::Label-->
                            <label class="form-label fs-6 fw-bolder text-dark">Ownership</label>
                            <!--end::Label-->
                            <!--begin::Input-->
                            <select class="form-select" data-control="select2" data-placeholder="Select an option" data-allow-clear="true" name="ownership">
                                <option></option>
                                @php
                                $ownerships = [
                                    1 => 'Played, but don\'t own',
                                    2 => 'Own Digital Copy',
                                    3 => 'Own Retail Version',
                                    4 => 'Own Collectors Edition',
                                ]
                                @endphp
                                @foreach ($ownerships as $key => $value)
                                    <option value="{{ $key }}" @selected(old('ownership') ?? $gameList->ownership == $key)>
                                        {{ $value }}
                                    </option>
                                @endforeach
                            </select>
                            <!--end::Input-->
                        </div>
                        <!--end::Input group-->

                        <!--begin::Input group-->
                        <div class="fv-row col-md-6 mb-5">
                            <!--begin::Label-->
                            <label class="form-label fs-6 fw-bolder text-dark required">Status</label>
                            <!--end::Label-->
                            <!--begin::Input-->
                            <select class="form-select" data-control="select2" data-placeholder="Select an option" name="status">
                                <option></option>
                                @php
                                $statuses = [
                                    1 => 'Playing',
                                    2 => 'Continuously Playing',
                                    3 => 'Completed',
                                    4 => 'On-Hold',
                                    5 => 'Dropped',
                                    6 => 'Plan to Play',
                                ]
                                @endphp
                                @foreach ($statuses as $key => $value)
                                    <option value="{{ $key }}" @selected(old('status') ?? $gameList->status == $key)>
                                        {{ $value }}
                                    </option>
                                @endforeach
                            </select>
                            <!--end::Input-->
                        </div>
                        <!--end::Input group-->

                        <!--begin::Input group-->
                        <div class="fv-row col-md-6 mb-5">
                            <!--begin::Label-->
                            <label class="form-label fs-6 fw-bolder text-dark">Rating</label>
                            <!--end::Label-->
                            <!--begin::Input-->
                            <select class="form-select" data-control="select2" data-placeholder="Select an option" data-allow-clear="true" name="rating">
                                <option></option>
                                @php
                                $ratings = [
                                    1 => '1 (Unplayable)',
                                    2 => '2 (Horrible)',
                                    3 => '3 (Very Bad)',
                                    4 => '4 (Bad)',
                                    5 => '5 (Playable)',
                                    6 => '6 (Fine)',
                                    7 => '7 (Good)',
                                    8 => '8 (Very Good)',
                                    9 => '9 (Great)',
                                    10 => '10 (Masterpiece)',
                                ]
                                @endphp
                                @foreach ($ratings as $key => $value)
                                    <option value="{{ $key }}" @selected(old('rating') ?? $gameList->rating == $key)>
                                        {{ $value }}
                                    </option>
                                @endforeach
                            </select>
                            <!--end::Input-->
                        </div>
                        <!--end::Input group-->

                        <!--begin::Input group-->
                        <div class="fv-row col-md-6 mb-5">
                            <!--begin::Label-->
                            <label class="form-label fs-6 fw-bolder text-dark">Priority</label>
                            <!--end::Label-->
                            <!--begin::Input-->
                            <select class="form-select" data-control="select2" data-placeholder="Select an option" data-allow-clear="true" name="priority">
                                <option></option>
                                @php
                                $priorities = [
                                    1 => 'Low',
                                    2 => 'Medium',
                                    3 => 'High',
                                ]
                                @endphp
                                @foreach ($priorities as $key => $value)
                                    <option value="{{ $value }}" @selected(old('priority') ?? $gameList->priority == $value)>
                                        {{ $value }}
                                    </option>
                                @endforeach
                            </select>
                            <!--end::Input-->
                        </div>
                        <!--end::Input group-->

                        <!--begin::Input group-->
                        <div class="fv-row col-md-6 mb-5">
                            <!--begin::Label-->
                            <label class="form-label fs-6 fw-bolder text-dark">Difficulty</label>
                            <!--end::Label-->
                            <!--begin::Input-->
                            <select class="form-select" data-control="select2" data-placeholder="Select an option" data-allow-clear="true" name="difficulty">
                                <option></option>
                                @php
                                $difficulties = [
                                    1 => 'Easy',
                                    2 => 'Medium',
                                    3 => 'Hard',
                                    4 => 'Extremely Hard',
                                ]
                                @endphp
                                @foreach ($difficulties as $key => $value)
                                    <option value="{{ $value }}" @selected(old('difficulty') ?? $gameList->difficulty == $value)>
                                        {{ $value }}
                                    </option>
                                @endforeach
                            </select>
                            <!--end::Input-->
                        </div>
                        <!--end::Input group-->

                        <!--begin::Input group-->
                        <div class="fv-row col-md-6 mb-5">
                            <!--begin::Label-->
                            <label class="form-label fs-6 fw-bolder text-dark">Hours Played</label>
                            <!--end::Label-->
                            <!--begin::Input-->
                            <select class="form-select" data-control="select2" data-placeholder="Select an option" data-allow-clear="true" name="hoursPlayed">
                                <option></option>
                                @php
                                $hoursPlayed = [
                                    1 => 'Less than 1 Hour',
                                    2 => '1-2 Hours',
                                    3 => '3-4 Hours',
                                    4 => '5-10 Hours',
                                    5 => '11-20 Hours',
                                    6 => '21-30 Hours',
                                    7 => '31-40 Hours',
                                    8 => '41-50 Hours',
                                    9 => '51-75 Hours',
                                    10 => '76-100 Hours',
                                    11 => '101-200 Hours',
                                    12 => '201-500 Hours',
                                    13 => '501+ Hours',
                                ]
                                @endphp
                                @foreach ($hoursPlayed as $key => $value)
                                    <option value="{{ $key }}" @selected(old('hoursPlayed') ?? $gameList->hours_played == $key)>
                                        {{ $value }}
                                    </option>
                                @endforeach
                            </select>
                            <!--end::Input-->
                        </div>
                        <!--end::Input group-->

                        <!--begin::Input group-->
                        <div class="fv-row col-md-6 mb-5">
                            <!--begin::Label-->
                            <label class="form-label fs-6 fw-bolder text-dark">Start Date</label>
                            <!--end::Label-->
                            <!--begin::Input-->
                            <div class="input-group mb-5">
                                <span class="input-group-text" id="basic-addon1"><i class="fal fa-calendar-day"></i></span>
                                <input class="form-control" placeholder="Pick a date" id="kt_datepicker_start" name="start_date" value="{{ $gameList->start_date }}" />
                            </div>
                            <!--end::Input-->
                        </div>
                        <!--end::Input group-->

                        <!--begin::Input group-->
                        <div class="fv-row col-md-6 mb-5">
                            <!--begin::Label-->
                            <label class="form-label fs-6 fw-bolder text-dark">Finish Date</label>
                            <!--end::Label-->
                            <!--begin::Input-->
                            <div class="input-group mb-5">
                                <span class="input-group-text" id="basic-addon1"><i class="fal fa-calendar-day"></i></span>
                                <input class="form-control" placeholder="Pick a date" id="kt_datepicker_finish" name="finish_date" value="{{ $gameList->finish_date }}" />
                            </div>
                            <!--end::Input-->
                        </div>
                        <!--end::Input group-->

                        <!--begin::Input group-->
                        @if ($gameList->status == 3)
                        <div class="fv-row col-md-6 mb-5">
                            <!--begin::Label-->
                            <label class="form-label fs-6 fw-bolder text-dark">Replay Value</label>
                            <!--end::Label-->
                            <!--begin::Input-->
                            <select class="form-select" data-control="select2" data-placeholder="Select an option" data-allow-clear="true" name="replayValue">
                                <option></option>
                                @php
                                $replayValues = [
                                    1 => 'Very Low',
                                    2 => 'Low',
                                    3 => 'Medium',
                                    4 => 'High',
                                    5 => 'Very High',
                                ]
                                @endphp
                                @foreach ($replayValues as $key => $value)
                                    <option value="{{ $key }}" @selected(old('replayValue') ?? $gameList->replay_value == $key)>
                                        {{ $value }}
                                    </option>
                                @endforeach
                                @endphp
                            </select>
                            <!--end::Input-->
                        </div>
                        <!--end::Input group-->
                        @endif

                        <!--begin::Input group-->
                        <div class="fv-row col-md-12 mb-5">
                            <!--begin::Label-->
                            <label class="form-label fs-6 fw-bolder text-dark">Notes</label>
                            <!--end::Label-->
                            <!--begin::Input-->
                            <textarea class="form-control" data-kt-autosize="true" rows="5" name="notes">{{ $gameList->notes ?? '' }}</textarea>
                            <!--end::Input-->
                        </div>
                        <!--end::Input group-->

                        <!--begin::Input group-->
                        @if ($gameList->status == 3)
                        <div class="fv-row col-md-6 mb-10">
                            <!--begin::Input-->
                            <div class="form-check form-switch form-check-custom form-check-solid">
                                <input class="form-check-input" type="checkbox" name="isReplaying" value="1" id="isReplaying" @checked(old('isReplaying') ?? $gameList->is_replaying == 'Y') />
                                <label class="form-check-label" for="isReplaying">Are You Replaying This Game?</label>
                            </div>
                            <!--end::Input-->
                        </div>
                        <!--end::Input group-->
                        @endif

                        <!--begin::Actions-->
                        <div class="d-flex justify-content-between">
                            <!--begin::Submit button-->

                            <button type="submit" id="kt_gameList_submit" class="btn btn-primary">
                                <span class="indicator-label">Submit</span>
                                <span class="indicator-progress">Please wait...
                                <span class="spinner-border spinner-border-sm align-middle ms-2"></span></span>
                            </button>
                            <!--end::Submit button-->
                        </form>
                            <!--begin::Delete button-->
                            <form class="d-inline m-0" method="POST" action="{{ route('games.list.destroy', $gameList) }}">
                                @method('DELETE')
                                @csrf
                                <a class="btn btn-danger" href="#" onclick="event.preventDefault(); this.closest('form').submit();">Delete</a>
                            </form>
                            <!--end::Delete button-->
                        </div>
                        <!--end::Actions-->
                    @endif
                </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>
                    <p class="lh-lg">This game also exists on:<br />
                        @foreach ($platforms as $platform)
                        <a class="mt-1" href="/game/{{ $platform['id'] }}"><span class="badge badge-primary">{{ $platform['name'] }}</span></a>
                        @endforeach
                    </p>
                </div>
            </div>
        </div>
        <!--end::Col-->
    </div>
    <!--end::Row-->

</x-base-layout>
