<?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->publishMigrations();
        $this->publishSeeders();
        $this->publishAssets();
        $this->publishConfig();
    }

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

    /**
     *
     */
    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 publishSeeders(): void
    {
        $this->publishes([
            __DIR__ . '/../Database/Seeders/TerminyPouzivatelRoleOpravneniaDatabaseSeeder.php.stub' => database_path('seeds/TerminyPouzivatelRoleOpravneniaDatabaseSeeder.php'),
        ], 'seeds');
    }

    /**
     * @throws \Illuminate\Contracts\Container\BindingResolutionException
     */
    private function publishMigrations(): void
    {
        if ($this->app->runningInConsole()) {
            if (!class_exists('CreateTerminyUdalostUdalostiTable')) {
                $this->publishes([
                    __DIR__ . '/../Database/Migrations/create_terminy_udalost_udalosti_table.php.stub' => database_path('migrations/2020_12_09_135800_create_terminy_udalost_udalosti_table.php'),
                ], 'migrations');
            }

            if (!class_exists('CreateTerminyUdalostRegistracieTable')) {
                $this->publishes([
                    __DIR__ . '/../Database/Migrations/create_terminy_udalost_registracie_table.php.stub' => database_path('migrations/2020_12_09_142000_create_terminy_udalost_registracie_table.php'),
                ], 'migrations');
            }

            if (!class_exists('CreateTerminyUdalostMiestaKonaniaTable')) {
                $this->publishes([
                    __DIR__ . '/../Database/Migrations/create_terminy_udalost_miesta_konania_table.php.stub' => database_path('migrations/2021_02_02_125500_create_terminy_udalost_miesta_konania_table.php'),
                ], 'migrations');
            }

            if (!class_exists('UpdateMiestoKonaniaTerminyUdalostUdalostiTable')) {
                $this->publishes([
                    __DIR__ . '/../Database/Migrations/update_miesto_konania_terminy_udalost_udalosti_table.php.stub' => database_path('migrations/2021_03_07_002500_update_miesto_konania_terminy_udalost_udalosti_table.php'),
                ], 'migrations');
            }

            if (!class_exists('AddFieldUpozornenieTerminyUdalostUdalostiTable')) {
                $this->publishes([
                    __DIR__ . '/../Database/Migrations/add_field_upozornenie_terminy_udalost_udalosti_table.php.stub' => database_path('migrations/2022_09_26_135000_add_field_upozornenie_terminy_udalost_udalosti_table.php'),
                ], 'migrations');
            }

            if (!class_exists('CreateTerminyUdalostMoznostiUdalostiTable')) {
                $this->publishes([
                    __DIR__ . '/../Database/Migrations/create_terminy_udalost_moznosti_udalosti_table.php.stub' => database_path('migrations/2023_04_17_224200_create_terminy_udalost_moznosti_udalosti_table.php'),
                ], 'migrations');
            }

            if (!class_exists('AddFieldMoznostiTerminyUdalostUdalostiTable')) {
                $this->publishes([
                    __DIR__ . '/../Database/Migrations/add_field_moznosti_terminy_udalost_udalosti_table.php.stub' => database_path('migrations/2023_04_17_235900_add_field_moznosti_terminy_udalost_udalosti_table.php'),
                ], 'migrations');
            }

            if (!class_exists('AddFieldMoznostiTerminyUdalostRegistracieTable')) {
                $this->publishes([
                    __DIR__ . '/../Database/Migrations/add_field_moznosti_terminy_udalost_registracie_table.php.stub' => database_path('migrations/2023_04_18_234400_add_field_moznosti_terminy_udalost_registracie_table.php'),
                ], 'migrations');
            }
        }
    }

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

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

    /**
     * @return void
     * @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");
    }
}
