<?php

namespace AleshaCMS\Providers;

use AleshaCMS\CMS\Hook\HookManager;
use AleshaCMS\CMS\Theme\ThemeManager;
use AleshaCMS\CMS\Plugin\PluginManager;
use AleshaCMS\CMS\Content\ContentTypeManager;
use AleshaCMS\CMS\PageBuilder\PageBuilderRenderer;
use Illuminate\Support\ServiceProvider;
use Illuminate\Support\Facades\View;
use Illuminate\Support\Facades\Blade;

class CMSServiceProvider extends ServiceProvider
{
    public function register(): void
    {
        $this->app->singleton(HookManager::class);
        $this->app->singleton(ThemeManager::class);
        $this->app->singleton(PluginManager::class);
        $this->app->singleton(ContentTypeManager::class);
        $this->app->singleton(PageBuilderRenderer::class);

        $this->app->alias(HookManager::class, 'hook');

        $this->registerClassAliases();
    }

    protected function registerClassAliases(): void
    {
        $aliases = [
            'AleshaSetting'  => \AleshaCMS\Models\CMS\Setting::class,
            'AleshaMenu'     => \AleshaCMS\Models\CMS\Menu::class,
            'AleshaPlugin'   => \AleshaCMS\Models\CMS\Plugin::class,
            'AleshaSlider'   => \AleshaCMS\Models\CMS\Slider::class,
            'AleshaTaxonomy' => \AleshaCMS\Models\CMS\Taxonomy::class,
            'AleshaBuilder'  => \AleshaCMS\CMS\PageBuilder\PageBuilderRenderer::class,
            'AleshaPost'     => \AleshaCMS\Models\Post::class,
        ];

        foreach ($aliases as $alias => $class) {
            class_alias($class, $alias);
        }
    }

    public function boot(): void
    {
        $this->loadThemeViews();
        $this->registerBladeDirectives();
        $this->shareGlobalViewData();

        if ($this->app->runningInConsole()) {
            return;
        }

        try {
            app(PluginManager::class)->bootAll();
        } catch (\Throwable) {
        }
    }

    protected function loadThemeViews(): void
    {
        $themesPath = base_path('Themes');
        if (!is_dir($themesPath)) {
            return;
        }

        foreach (glob("{$themesPath}/*/views") as $viewPath) {
            $themeName = basename(dirname($viewPath));
            view()->addNamespace("theme-{$themeName}", $viewPath);
        }
    }

    protected function registerBladeDirectives(): void
    {
        Blade::directive('hook', function (string $expression) {
            return "<?php foreach(app(\AleshaCMS\CMS\Hook\HookManager::class)->fire({$expression}) as \$__hookOutput): echo \$__hookOutput; endforeach; ?>";
        });

        Blade::directive('setting', function (string $expression) {
            return "<?php echo \AleshaCMS\Models\CMS\Setting::get({$expression}); ?>";
        });
    }

    protected function shareGlobalViewData(): void
    {
        View::composer('*', function ($view) {
            if ($this->app->runningInConsole()) {
                return;
            }
            try {
                $view->with('_settings', \AleshaCMS\Models\CMS\Setting::all()->pluck('value', 'key'));
            } catch (\Throwable) {
            }
        });
    }
}
