<?php

namespace App\Providers;

use App\Page;
use Illuminate\Support\Facades\Cookie;
use Illuminate\Support\Facades\Schema;
use Illuminate\Support\ServiceProvider;
use App\Http\Controllers\FrontendController;

class AppServiceProvider extends ServiceProvider
{
    /**
     * Register any application services.
     *
     * @return void
     */
    public function register()
    {
        //
    }

    /**
     * Bootstrap any application services.
     *
     * @return void
     */
    public function boot()
    {
        Schema::defaultStringLength(191);

        if ( alreadyInstalled() ) {
            app()->setLocale( $this->cacheIfLocale() );

            $frontend = new FrontendController;
            view()->share('genre_list', $frontend->genreLists());

            view()->share('pages', Page::all());
        }
    }

    private function cacheIfLocale()
    {
        $locales = get_locales();
        $locale = 'en';
        $segment = request()->segment(1);

        if (! is_null($segment)) {
            if ( array_key_exists($segment, $locales) ) {
                $locale = request()->segment(1);
            }
        }

        return $locale;
    }
}
