<?php

namespace App\Providers;

use App\Module;
use App\Setting;
use App\Language;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Config;
use Illuminate\Support\Facades\Route;
use Illuminate\Support\Facades\URL;
use Illuminate\Support\ServiceProvider;
use Illuminate\Support\Facades\View;
use Illuminate\Support\Facades\Schema;


class AppServiceProvider extends ServiceProvider
{
    protected static $meta = null;

    /**
     * Register any application services.
     *
     * @return void
     */

    /**
     * @return null
     */
    public static function getMeta()
    {
        return self::$meta;
    }

    public function register()
    {

    }

    /**
     * Bootstrap any application services.
     *
     * @return void
     */
    public function boot()
    {
        // Fix migrate issue associated with index key length
        Schema::defaultStringLength(191);

        // CMS Supported Languages
        $languages= Language::where('status', 1)->cursor();
        View::share('languages', $languages);

        View::composer('*', function($view) {

            $routePath= $this->app->request->getRequestUri();
            $routePath= explode('/', trim($routePath, '/'));

            $activeMenu= [];
            if (count($routePath)) {
                if ('admin' == $routePath[0]  ) {
                    // dd($routePath);
                    $activeMenu[$routePath[1] ?? ""]= 'active';
                    $view->with('activeMenu', $activeMenu);
                }elseif ( isset($routePath[1]) && 'admin' == $routePath[1] ) {
                    $activeMenu[$routePath[2] ?? ""]= 'active';
                    $view->with('activeMenu', $activeMenu);
                }
            }

            // Current User Info
            $userInfo= (object) ['photo'=> 'uploads/avatar.png'];
            if(Auth::check()){
                $userInfo= Auth::user();
                if (!is_file(public_path($userInfo->photo))) {
                    $userInfo->photo= 'uploads/avatar.png';
                }

                // Get Modules Current User can access
                if ($userInfo->role == 'admin') {
                    $userInfo->modules= Module::get();
                } else {
                }
            }
            $view->with('currentUserInfo', $userInfo);

            // Share meta for modules instead of language files
            if (Schema::hasTable('modules')){

                if (preg_match('/admin/', URL::current())){
                    $path = explode('.', Route::currentRouteName());
                    if (preg_match('/_/', $path[0])){
                        $path = explode('_', $path[0]);
                        $path = $path[0].ucfirst($path[0]);
                    } else {
                        $path = $path[0];
                    }

                    $module = Module::where('path', $path)->pluck('meta')->toArray();
                    $meta = json_decode(array_first($module));
                    static::$meta = $meta;
                    $view->with('meta', $meta);
                }


            }


        });

        // Values Got from settings

        if (Schema::hasTable('settings')){
            $settings = Setting::cursor();

            if(count($settings) > 0){
                foreach ($settings as $setting) {
                    $settings->{$setting['key']} = $setting['value'];
                }

                Config::set('app.name', $settings->website_name);
                Config::set('mail.name', $settings->website_name);
                Config::set('mail.driver',$settings->mail_driver);
                Config::set('mail.host',$settings->mail_host);
                Config::set('mail.port',$settings->mail_port);
                Config::set('mail.username',$settings->mail_username);
                Config::set('mail.password',$settings->mail_password);
                Config::set('mail.from', [
                    'address' => $settings->mail_from_address,
                    'name' => $settings->mail_from_name,
                ]);

            }
        }
    }
}
