<?php

namespace Seka\Portal\Modul\Terminy\Providers;

use Illuminate\Support\ServiceProvider;
use Seka\Portal\Modul\Terminy\Entities\Registracia\Registracia;
use Seka\Portal\Modul\Terminy\Entities\Registracia\RegistraciaObserver;
use Seka\Portal\Modul\Terminy\Entities\Udalost\Udalost;
use Seka\Portal\Modul\Terminy\Entities\Udalost\UdalostObserver;
use Seka\Portal\Modul\Terminy\RolesAndPermissions;
use Seka\Portal\Modul\Terminy\Services\External\Calendar\CalendarService;
use Seka\Portal\Modul\Terminy\Services\External\Calendar\Google\GoogleCalendarService;

/**
 * Class TerminyServiceProvider
 *
 * @package Seka\Portal\Modul\Terminy\Providers
 */
final class TerminyServiceProvider extends ServiceProvider
{
    /**
     * @throws \Illuminate\Contracts\Container\BindingResolutionException
     */
    public function boot(): void
    {
        Udalost::observe(UdalostObserver::class);
        Registracia::observe(RegistraciaObserver::class);

        $this->app->bind(RolesAndPermissions::class, function () {
            return new RolesAndPermissions();
        });

        $this->app->bind(CalendarService::class, function () {
            return new GoogleCalendarService();
        });

        $this->app->tag([
            RolesAndPermissions::class,
        ], 'roleaopravnenia');

        $this->app->booted(function () {
            $this->breadcrumbs();
        });

        $this->publishAssets();
        $this->publishConfig();
    }

    protected function breadcrumbs(): void
    {
        if (class_exists('Breadcrumbs')) {
            // BACKEND
            require __DIR__ . '/../../routes/backend/breadcrumbs.php';

            // FRONTEND
            require __DIR__ . '/../../routes/frontend/breadcrumbs.php';
        }
    }

    /**
     * @throws \Illuminate\Contracts\Container\BindingResolutionException
     */
    private function publishAssets(): void
    {
        $this->publishes([
            __DIR__ . '/../../public/assets' => public_path('vendor/seka/terminy'),
        ], 'assets');
    }

    /**
     * Publish Config
     *
     * @throws \Illuminate\Contracts\Container\BindingResolutionException
     */
    public function publishConfig(): void
    {
        if ($this->app->runningInConsole()) {
            $this->publishes([
                __DIR__ . '/../../config/terminy.php' => config_path('terminy.php'),
            ], 'config');
        }
    }

    /**
     * @throws \Illuminate\Contracts\Container\BindingResolutionException
     */
    public function register(): void
    {
        $this->registerConfiguration();
    }

    /**
     * @throws \Illuminate\Contracts\Container\BindingResolutionException
     */
    private function registerConfiguration(): void
    {
        $configPathTerminy = __DIR__ . '/../../config/terminy.php';
        $configPathGeocoder = __DIR__ . '/../../config/geocoder.php';

        $this->mergeConfigFrom($configPathTerminy, 'terminy');
        $this->mergeConfigFrom($configPathGeocoder, 'geocoder');
    }
}
