<?php

namespace Sargilla\Flatlab4;

use Carbon\Carbon;
use Laravel\Ui\UiCommand;
use Illuminate\Support\Facades\View;
use Sargilla\Flatlab4\Flatlab4Preset;
use Illuminate\Support\ServiceProvider;
use Sargilla\Flatlab4\Facades\Flatlab4;
use Sargilla\Flatlab4\Composers\MenuComposer;
use Sargilla\Flatlab4\Console\CreateDatabase;
use Sargilla\Flatlab4\Console\InstallFlatlab4;
use Sargilla\Flatlab4\Console\PublishFlatlab4;
use Sargilla\Flatlab4\Middlewares\GuestUser;
use Sargilla\Flatlab4\Middlewares\MenuMiddleware;
use Sargilla\Flatlab4\Middlewares\AdminMiddleware;
use Sargilla\Flatlab4\Console\MakeAdminUserSeeder;



class Flatlab4ServiceProvider extends ServiceProvider
{
    /**
     * Bootstrap services.
     *
     * @return void
     */
    public function boot(): void
    {
        $this->presetCommand();
        $this->publishConfig();
        $this->publishAndLoadRoutes();
        $this->publishLanguages();
        $this->publishViews();
        $this->loadMigrations();
        $this->registerMenuAdminComposer();
        $this->loadMiddlewares();

        Carbon::setLocale(config('app.locale', 'es'));
    }

    /**
     * Register services.
     *
     * @return void
     */
    public function register(): void
    {
        if (!defined('FLATLAB4_PATH')) {
            define('FLATLAB4_PATH', realpath(__DIR__.'/../'));
        }

        $this->app->bind('Flatlab4', function () {
            return new \Sargilla\Flatlab4\Flatlab4();
        });

        //Comandos de consola
        if ($this->app->runningInConsole()) {
            $this->commands([PublishFlatlab4::class]);
            $this->commands([InstallFlatlab4::class]);
            $this->commands([CreateDatabase::class]);
            // $this->commands([MakeAdminUserSeeder::class]);
        }

        $this->mergeConfigFrom(
            FLATLAB4_PATH.'/config/flatlab4.php', 'flatlab4'
        );
        $this->registerConfigurations();
    }

    protected function presetCommand(){
        UiCommand::macro('flatlab4' , function(UiCommand $command) {
            Flatlab4Preset::install();
            $command->info('Preset de Flatlab4 establecido!');
        });
    }

    private function publishConfig()
    {
        $this->publishes(Flatlab4::config(), 'config-flatlab');
    }

    private function publishAndLoadRoutes()
    {
        $this->publishes(Flatlab4::routes(), 'flatlab');
        $this->loadRoutesFrom(FLATLAB4_PATH.'/routes/menu.php');
        $this->loadRoutesFrom(FLATLAB4_PATH.'/routes/permisos.php');
    }

    private function publishLanguages()
    {
        $this->loadTranslationsFrom(FLATLAB4_PATH.'/lang/', 'flatlab');
    }

    private function publishViews()
    {
        $this->loadViewsFrom(FLATLAB4_PATH.'/resources/views/flatlab4/', 'flatlab');
        $this->loadViewsFrom(FLATLAB4_PATH.'/resources/views/notifications/', 'flatlab');
        $this->publishes(Flatlab4::views(), 'views-flatlab');
    }

    protected function loadMigrations()
    {
        $this->loadMigrationsFrom(Flatlab4::loadMigrationRoute());
    }

    private function registerMenuAdminComposer()
    {
        View::composer('flatlab::_sidebar', MenuComposer::class);
    }
    protected function loadMiddlewares(){
        $this->app['router']->pushMiddlewareToGroup('web', GuestUser::class);
        $this->app['router']->aliasMiddleware('isAdmin',AdminMiddleware::class);
        $this->app['router']->aliasMiddleware('gestionaMenus', MenuMiddleware::class);
    }
    public function registerConfigurations()
    {
        $this->mergeConfigFrom(
            FLATLAB4_PATH . '/config/imagecache_templates.php', 'imagecache.templates', 'imagecache'
        );
    }
}
