<?php

namespace Sigma\Flatlab;

use Laravel\Ui\UiCommand;
use Illuminate\Support\Str;
use Sigma\Flatlab\FlatlabPreset;
use Sigma\Flatlab\Facades\Flatlab;
use Illuminate\Support\Facades\View;
use Illuminate\Foundation\AliasLoader;
use Illuminate\Support\ServiceProvider;
use Sigma\Flatlab\Composers\MenuComposer;
use Sigma\Flatlab\Console\CreateDatabase;
use Sigma\Flatlab\Console\InstallFlatlab;
use Sigma\Flatlab\Console\PublishFlatlab;
use Sigma\Flatlab\Middlewares\MenuMiddleware;
use Sigma\Flatlab\Console\MakeAdminUserSeeder;
use Sigma\Flatlab\Middlewares\AdminMiddleware;



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

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

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

        //Comandos de consola
        if ($this->app->runningInConsole()) {
            $this->commands([PublishFlatlab::class]);
            $this->commands([InstallFlatlab::class]);
            $this->commands([CreateDatabase::class]);
            // $this->commands([MakeAdminUserSeeder::class]);
        }
        $this->app->booting(function() {
            $loader = AliasLoader::getInstance();
            $loader->alias('Str', Str::class);
        });
        $this->mergeConfigFrom(
            FLATLAB_PATH.'/config/flatlab.php', 'flatlab'
        );
        $this->registerConfigurations();
    }

    protected function presetCommand(){
        UiCommand::macro('flatlab' , function(UiCommand $command) {
            FlatlabPreset::install();
            $command->info('Preset de Flatlab establecido!');
        });
    }

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

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

    private function publishLanguages()
    {
        $this->loadTranslationsFrom(FLATLAB_PATH.'/lang/', 'flatlab');
        $this->publishes(Flatlab::languages(),'lang-flatlab');
    }

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

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

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