@php
use Illuminate\Support\Facades\Route;
use App\Http\Controllers\UserSiteSettingController;

// Check cookie to see if dark mode is enabled or not.
$darkMode = (isset($_COOKIE['dark_mode']) && $_COOKIE['dark_mode'] == 'yes') ? 'yes' : 'no';

// Update the users dark mode preference in the database.
if (auth()->check()) {
    if ($darkMode === 'yes') {
        $result = UserSiteSettingController::setDarkMode('yes');
    } elseif ($darkMode === 'no') {
        $result = UserSiteSettingController::setDarkMode('no');
    }
}
@endphp

<!DOCTYPE html>
<html lang="{{ str_replace('_', '-', app()->getLocale()) }}"{!! theme()->printHtmlAttributes('html') !!} {{ theme()->printHtmlClasses('html') }}>
{{-- begin::Head --}}
<head>
    <meta charset="utf-8"/>
    <title>{{ ucfirst(theme()->getOption('meta', 'title')) }}</title>
    <meta name="description" content="{{ ucfirst(theme()->getOption('meta', 'description')) }}"/>
    <meta name="keywords" content="{{ theme()->getOption('meta', 'keywords') }}"/>
    <link rel="canonical" href="{{ ucfirst(theme()->getOption('meta', 'canonical')) }}"/>
    <meta name="viewport" content="width=device-width, initial-scale=1"/>
    <link rel="shortcut icon" href="{{ asset(theme()->getDemo() . '/' .theme()->getOption('assets', 'favicon')) }}"/>
    <meta name="csrf-token" content="{{ csrf_token() }}">

    {{-- begin::Fonts --}}
    {{ theme()->includeFonts() }}
    {{-- end::Fonts --}}

    @if (theme()->hasOption('page', 'assets/vendors/css'))
        {{-- begin::Page Vendor Stylesheets(used by this page) --}}
        @foreach (array_unique(theme()->getOption('page', 'assets/vendors/css')) as $file)
            {!! preloadCss(assetCustom($file)) !!}
        @endforeach
        {{-- end::Page Vendor Stylesheets --}}
    @endif

    @if (theme()->hasOption('page', 'assets/custom/css'))
        {{-- begin::Page Custom Stylesheets(used by this page) --}}
        @foreach (array_unique(theme()->getOption('page', 'assets/custom/css')) as $file)
            {!! preloadCss(assetCustom($file)) !!}
        @endforeach
        {{-- end::Page Custom Stylesheets --}}
    @endif

    @if (theme()->hasOption('assets', 'css'))
        {{-- begin::Global Stylesheets Bundle(used by all pages) --}}
        @foreach (array_unique(theme()->getOption('assets', 'css')) as $file)
            @if (strpos($file, 'plugins') !== false)
                {!! preloadCss(assetCustom($file)) !!}
            @else
                <link href="{{ assetCustom($file) }}" rel="stylesheet" type="text/css"/>
            @endif
        @endforeach
        {{-- end::Global Stylesheets Bundle --}}
    @endif

    @yield('styles')

</head>
{{-- end::Head --}}

{{-- begin::Body --}}
<body {!! theme()->printHtmlAttributes('body') !!} {!! theme()->printHtmlClasses('body') !!} {!! theme()->printCssVariables('body') !!}>

@if (theme()->getOption('layout', 'loader/display') === true)
    <div class="page-loader">
        <span class="spinner-border text-primary" role="status">
            <span class="visually-hidden">Loading...</span>
        </span>
    </div>
@endif

@yield('content')

{{-- begin::Javascript --}}
@if (theme()->hasOption('assets', 'js'))
    {{-- begin::Global Javascript Bundle(used by all pages) --}}
    @foreach (array_unique(theme()->getOption('assets', 'js')) as $file)
        <script src="{{ asset(theme()->getDemo() . '/' .$file) }}"></script>
    @endforeach
    {{-- end::Global Javascript Bundle --}}
@endif

@if (theme()->hasOption('page', 'assets/vendors/js'))
    {{-- begin::Page Vendors Javascript(used by this page) --}}
    @foreach (array_unique(theme()->getOption('page', 'assets/vendors/js')) as $file)
        <script src="{{ asset(theme()->getDemo() . '/' .$file) }}"></script>
    @endforeach
    {{-- end::Page Vendors Javascript --}}
@endif

@if (theme()->hasOption('page', 'assets/custom/js'))
    {{-- begin::Page Custom Javascript(used by this page) --}}
    @foreach (array_unique(theme()->getOption('page', 'assets/custom/js')) as $file)
        <script src="{{ asset(theme()->getDemo() . '/' .$file) }}"></script>
    @endforeach
    {{-- end::Page Custom Javascript --}}
@endif

@yield('scripts')

<script src="https://kit.fontawesome.com/c809253084.js" crossorigin="anonymous"></script>

<script>
    KTUtil.onDOMContentLoaded(function() {
        var darkModeToggle = document.querySelector('#theme_mode_dark');
        var lightModeToggle = document.querySelector('#theme_mode_light');

        darkModeToggle.addEventListener('click', function() {
            KTApp.setThemeMode("dark", function() {
                KTCookie.set("dark_mode", "yes");

                darkModeToggle.classList.add('d-none');
                lightModeToggle.classList.remove('d-none');
            });
        });

        lightModeToggle.addEventListener('click', function() {
            KTApp.setThemeMode("light", function() {
                KTCookie.set("dark_mode", "no");

                darkModeToggle.classList.remove('d-none');
                lightModeToggle.classList.add('d-none');
            });
        });

        var darkMode = KTCookie.get("dark_mode");

        if (darkMode == 'yes') {
            KTApp.setThemeMode('dark');
        } else {
            KTApp.setThemeMode('light');
        }
    });

    $(document).ready(function() {
        $("#searchGames input").keyup(function(event) {
            if (event.keycode == 13) {
                $(this).closest('form').submit();
            }
        });
    });
</script>

@if(Route::currentRouteName() == 'admin.games.index' || Route::currentRouteName() == 'admin.game.edit')
@include('partials.footer-scripts.admin.games._toasts')
@endif

@if(Route::currentRouteName() == 'games.list.index' || Route::currentRouteName() == 'games.list.user.index')
<script src="{{ asset(theme()->getDemo() . '/plugins/custom/datatables/datatables.bundle.js') }}"></script>
@endif

@if(Route::currentRouteName() == 'games.list.index')
@include('partials.footer-scripts.game-lists._data-tables-games-list')
@include('partials.footer-scripts.game-lists._toasts')
@endif

@if(Route::currentRouteName() == 'games.list.user.index')
@include('partials.footer-scripts.game-lists._data-tables-user')
@endif

@if(Route::currentRouteName() == 'games.list.create')
@include('partials.footer-scripts.game-lists._create_start_finish_date_pickers')
@endif

@if(Route::currentRouteName() == 'games.list.edit')
@include('partials.footer-scripts.game-lists._edit_start_finish_date_pickers')
@include('partials.footer-scripts.game-lists._toasts')
@endif

<script defer data-domain="myvideogamelist.com" src="https://plausible-analytics.linuxbox.ninja/js/plausible.js"></script>

{{-- end::Javascript --}}

</body>
{{-- end::Body --}}
</html>
