<?php

namespace Seka\Portal\Modul\Terminy\Providers;

use Illuminate\Support\ServiceProvider;
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
    {
        $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
    {

    }

    /**
     *
     */
    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');
            }
        }
    }

    /**
     * @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
    {
        $configPathTerminy = __DIR__ . '/../Config/terminy.php';
        $configPathGeocoder = __DIR__ . '/../Config/geocoder.php';

        if ($this->app->runningInConsole()) {
            $this->publishes([
                $configPathTerminy => config_path('terminy.php'),
            ], 'config');
        }

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