<?php
/**
 * Created by PhpStorm.
 * User: radko
 * Date: 12.11.18
 * Time: 15:33
 */

namespace Seka\Vzdelavanie;

use Livewire\Livewire;
use ConsoleTVs\Charts\Registrar;
use Illuminate\Support\Facades\Blade;
use Illuminate\Support\ServiceProvider;
use Seka\Vzdelavanie\View\Components\Badge;
use Seka\Vzdelavanie\Services\SekciaService;
use Seka\Vzdelavanie\Models\Kniznica\Sekcia;
use Seka\Vzdelavanie\Models\Testovanie\Test;
use Seka\Vzdelavanie\Services\UcebnicaService;
use Seka\Vzdelavanie\Models\Kniznica\Ucebnica;
use Seka\Vzdelavanie\Services\KategoriaService;
use Seka\Vzdelavanie\Commands\Console\ImportTestov;
use Seka\Vzdelavanie\Http\Livewire\Components\Stav;
use Illuminate\Database\Eloquent\Relations\Relation;
use Seka\Vzdelavanie\Services\SekciaServiceInterface;
use Seka\Vzdelavanie\Services\UcebnicaServiceInterface;
use Seka\Vzdelavanie\Modul\VzdelavanieRolesPermissions;
use Seka\Vzdelavanie\Services\KategoriaServiceInterface;
use Seka\Vzdelavanie\Http\Livewire\Components\Pracoviska;
use Seka\Vzdelavanie\Http\Livewire\Components\SearchText;
use Seka\Vzdelavanie\Repositories\Eloquent\SekciaRepository;
use Seka\Vzdelavanie\Charts\UspesnostRieseniaTestovNaPekChart;
use Seka\Vzdelavanie\Repositories\Eloquent\UcebnicaRepository;
use Seka\Vzdelavanie\Repositories\Eloquent\KategoriaRepository;
use Seka\Vzdelavanie\Composers\Kniznica\ZoznamKategoriiComposer;
use Seka\Vzdelavanie\Repositories\Contracts\SekciaRepositoryInterface;
use Seka\Vzdelavanie\Repositories\Contracts\UcebnicaRepositoryInterface;
use Seka\Vzdelavanie\Repositories\Contracts\KategoriaRepositoryInterface;
use Seka\Vzdelavanie\Composers\Testovanie\ZoznamTestovPodlaPopularnostiCompose;
use Seka\Vzdelavanie\Composers\Kniznica\ZoznamOblubenychPoloziekPouzivatelaComposer;
use Seka\Vzdelavanie\Http\Livewire\Pages\Sprava\Testovanie\Pouzivatelia\Pages\Index;
use Seka\Vzdelavanie\Composers\Kniznica\ZoznamUcebnychMaterialovPodlaKategoriiComposer;
use Seka\Vzdelavanie\Http\Livewire\Pages\Sprava\Testovanie\Pouzivatelia\Components\Zoznam;

/**
 * Class VzdelavanieServiceProvider
 * @package Seka\Vzdelavanie
 */
class VzdelavanieServiceProvider extends ServiceProvider
{
    /**
     * @throws \Illuminate\Contracts\Container\BindingResolutionException
     */
    public function boot(): void
    {
        $this->app->bind(VzdelavanieRolesPermissions::class, function () {
            return new VzdelavanieRolesPermissions();
        });

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

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

            Relation::morphMap([
                Test::type() => Test::class,
                Sekcia::type() => Sekcia::class,
                Ucebnica::type() => Ucebnica::class,
            ]);
        });

        if ($this->app->runningInConsole()) {
            $this->commands([
                ImportTestov::class,
            ]);
        }

        $this->app->get(Registrar::class)->register([
            UspesnostRieseniaTestovNaPekChart::class,
        ]);

        $this->publish();
        $this->migrations();
        $this->databaseFactories();
        $this->databaseSeeders();
        $this->loadViewsFrom(__DIR__.'/../resources/views', 'Vzdelavanie');

        Blade::component(Badge::class, 'badge');

        $this->loadLivewireComponents();
    }

    /**
     * @throws \Illuminate\Contracts\Container\BindingResolutionException
     */
    public function register(): void
    {
        $this->registerRepositoryBindings();
        $this->registerServiceBindings();
        $this->registerConfig();
        $this->registerComposers();
        $this->registerEvents();
        $this->registerAuths();
        $this->registerRoutes();
    }

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

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

    /**
     *
     */
    protected function registerRepositoryBindings(): void
    {
        $this->app->bind(
            KategoriaRepositoryInterface::class,
            KategoriaRepository::class
        );
        $this->app->bind(
            UcebnicaRepositoryInterface::class,
            UcebnicaRepository::class
        );
        $this->app->bind(
            SekciaRepositoryInterface::class,
            SekciaRepository::class
        );
    }

    /**
     *
     */
    protected function registerServiceBindings(): void
    {
        $this->app->bind(
            KategoriaServiceInterface::class,
            KategoriaService::class
        );
        $this->app->bind(
            UcebnicaServiceInterface::class,
            UcebnicaService::class
        );
        $this->app->bind(
            SekciaServiceInterface::class,
            SekciaService::class
        );
    }

    /**
     * @throws \Illuminate\Contracts\Container\BindingResolutionException
     */
    protected function registerConfig(): void
    {
        $this->mergeConfigFrom(
            __DIR__.'/../config/vzdelavanie.php',
            'vzdelavanie'
        );
    }

    /**
     * @throws \Illuminate\Contracts\Container\BindingResolutionException
     */
    public function registerComposers(): void
    {
        // frontend
        view()->composer(
            ['Vzdelavanie::pages.kniznica.index'],
            ZoznamKategoriiComposer::class
        );
        view()->composer(
            ['Vzdelavanie::pages.kniznica.index'],
            ZoznamUcebnychMaterialovPodlaKategoriiComposer::class
        );
        view()->composer(
            ['Vzdelavanie::pages.testovanie.index'],
            ZoznamTestovPodlaPopularnostiCompose::class
        );
        view()->composer(
            ['Vzdelavanie::pages.kniznica.index'],
            ZoznamOblubenychPoloziekPouzivatelaComposer::class
        );

        // backend
        view()->composer(
            ['Vzdelavanie::pages.sprava.kniznica.index'],
            ZoznamKategoriiComposer::class
        );
        view()->composer(
            ['Vzdelavanie::pages.sprava.kniznica.index'],
            ZoznamOblubenychPoloziekPouzivatelaComposer::class
        );
    }

    /**
     *
     */
    public function registerRoutes(): void
    {
        $this->app->register(RouteServiceProvider::class);
    }

    /**
     *
     */
    public function registerEvents(): void
    {
        $this->app->register(EventServiceProvider::class);
    }

    /**
     *
     */
    public function registerAuths(): void
    {
        $this->app->register(AuthServiceProvider::class);
    }

    /**
     * @throws \Illuminate\Contracts\Container\BindingResolutionException
     */
    private function publish(): void
    {
        $this->publishes([
            __DIR__.'/../config/vzdelavanie.php' => config_path('vzdelavanie.php'),
        ], 'config');

        $this->publishes([
            __DIR__.'/../resources/views' => resource_path('views/vendor/seka/vzdelavanie'),
        ], 'views');

        $this->publishes([
            __DIR__.'/../public/assets' => public_path('vendor/seka/vzdelavanie'),
            __DIR__.'/../resources/assets/klientapp/dist' => public_path('vendor/seka/vzdelavanie/js/klientapp'),
        ], 'assets');
    }

    /**
     *
     */
    private function migrations(): void
    {
        if ($this->app->runningInConsole()) {
            if (!class_exists('CreateVzdelavanieKniznicaKategorieTable')) {
                $this->publishes([
                    __DIR__ . '/../database/migrations/create_kniznica_kategorie_table.php.stub' => database_path('migrations/2020_09_06_010201_create_vzdelavanie_kniznica_kategorie_table.php'),
                ], 'migrations');
            }

            if (!class_exists('CreateVzdelavanieKniznicaUcebniceTable')) {
                $this->publishes([
                    __DIR__ . '/../database/migrations/create_kniznica_ucebnice_table.php.stub' => database_path('migrations/2020_09_06_010202_create_vzdelavanie_kniznica_ucebnice_table.php'),
                ], 'migrations');
            }

            if (!class_exists('CreateVzdelavanieKniznicaSekcieTable')) {
                $this->publishes([
                    __DIR__ . '/../database/migrations/create_kniznica_sekcie_table.php.stub' => database_path('migrations/2020_09_06_010203_create_vzdelavanie_kniznica_sekcie_table.php'),
                ], 'migrations');
            }

            if (!class_exists('CreateVzdelavanieTestovanieTestyTable')) {
                $this->publishes([
                    __DIR__ . '/../database/migrations/create_testovanie_testy_table.php.stub' => database_path('migrations/2020_09_06_010204_create_vzdelavanie_testovanie_testy_table.php'),
                ], 'migrations');
            }

            if (!class_exists('CreateVzdelavanieTestovanieOtazkyTable')) {
                $this->publishes([
                    __DIR__ . '/../database/migrations/create_testovanie_otazky_table.php.stub' => database_path('migrations/2020_09_06_010205_create_vzdelavanie_testovanie_otazky_table.php'),
                ], 'migrations');
            }

            if (!class_exists('CreateVzdelavanieTestovanieOdpovedeTable')) {
                $this->publishes([
                    __DIR__ . '/../database/migrations/create_testovanie_odpovede_table.php.stub' => database_path('migrations/2020_09_06_010206_create_vzdelavanie_testovanie_odpovede_table.php'),
                ], 'migrations');
            }

            if (!class_exists('CreateVzdelavaniePouzivatelTestovanieTestyTable')) {
                $this->publishes([
                    __DIR__ . '/../database/migrations/create_pouzivatel_testovanie_testy_table.php.stub' => database_path('migrations/2020_09_06_010207_create_vzdelavanie_pouzivatel_testovanie_testy_table.php'),
                ], 'migrations');
            }

            if (!class_exists('CreateVzdelavaniePouzivatelTestovanieOtazkyTable')) {
                $this->publishes([
                    __DIR__ . '/../database/migrations/create_pouzivatel_testovanie_otazky_table.php.stub' => database_path('migrations/2020_09_06_010208_create_vzdelavanie_pouzivatel_testovanie_otazky_table.php'),
                ], 'migrations');
            }

            if (!class_exists('CreateVzdelavaniePouzivatelTestovanieOdpovedeTable')) {
                $this->publishes([
                    __DIR__ . '/../database/migrations/create_pouzivatel_testovanie_odpovede_table.php.stub' => database_path('migrations/2020_09_06_010209_create_vzdelavanie_pouzivatel_testovanie_odpovede_table.php'),
                ], 'migrations');
            }

            if (!class_exists('CreateVzdelavaniePouzivatelKniznicaOblubeneTable')) {
                $this->publishes([
                    __DIR__ . '/../database/migrations/create_pouzivatel_kniznica_oblubene_table.php.stub' => database_path('migrations/2020_09_06_010210_create_vzdelavanie_pouzivatel_kniznica_oblubene_table.php'),
                ], 'migrations');
            }

            if (!class_exists('CreateVzdelavaniePopularitaTable')) {
                $this->publishes([
                    __DIR__ . '/../database/migrations/create_popularita_table.php.stub' => database_path('migrations/2020_09_06_010211_create_vzdelavanie_popularita_table.php'),
                ], 'migrations');
            }

            if (!class_exists('UpdateVzdelavanieKniznicaUcebniceTable')) {
                $this->publishes([
                    __DIR__ . '/../database/migrations/update_kniznica_ucebnice_table.php.stub' => database_path('migrations/2020_09_06_010212_update_vzdelavanie_kniznica_ucebnice_table.php'),
                ], 'migrations');
            }

            if (!class_exists('UpdateVzdelavanieTestovanieTestyTable')) {
                $this->publishes([
                    __DIR__ . '/../database/migrations/update_testovanie_testy_table.php.stub' => database_path('migrations/2020_09_06_010213_update_vzdelavanie_testovanie_testy_table.php'),
                ], 'migrations');
            }

            if (!class_exists('UpdateVzdelavanieTestovanieOtazkyTable')) {
                $this->publishes([
                    __DIR__ . '/../database/migrations/update_testovanie_otazky_table.php.stub' => database_path('migrations/2020_09_06_010214_update_vzdelavanie_testovanie_otazky_table.php'),
                ], 'migrations');
            }

            if (!class_exists('UpdateVzdelavanieTestovanieOdpovedeTable')) {
                $this->publishes([
                    __DIR__ . '/../database/migrations/update_testovanie_odpovede_table.php.stub' => database_path('migrations/2020_09_06_010215_update_vzdelavanie_testovanie_odpovede_table.php'),
                ], 'migrations');
            }

            if (!class_exists('OdstranenyNazovKategorieVzdelavaniePouzivatelTestovanieTestyTable')) {
                $this->publishes([
                    __DIR__ . '/../database/migrations/odstraneny_nazov_kategorie_pouzivatel_testovanie_testy_table.php.stub' => database_path('migrations/2020_09_11_110000_odstraneny_nazov_kategorie_vzdelavanie_pouzivatel_testovanie_testy_table.php'),
                ], 'migrations');
            }

            if (!class_exists('PremenovanyStlpecDruhVzdelavaniePouzivatelTestovanieOtazkyTable')) {
                $this->publishes([
                    __DIR__ . '/../database/migrations/premenovany_stlpec_druh_pouzivatel_testovanie_otazky_table.php.stub' => database_path('migrations/2020_09_11_111000_premenovany_stlpec_druh_vzdelavanie_pouzivatel_testovanie_otazky_table.php'),
                ], 'migrations');
            }

            if (!class_exists('PremenovanyStlpecSpravnaVzdelavaniePouzivatelTestovanieOdpovedeTable')) {
                $this->publishes([
                    __DIR__ . '/../database/migrations/premenovany_stlpec_spravna_pouzivatel_testovanie_odpovede_table.php.stub' => database_path('migrations/2020_09_11_112000_premenovany_stlpec_spravna_vzdelavanie_pouzivatel_testovanie_odpovede_table.php'),
                ], 'migrations');
            }

            if (!class_exists('PridaneStlpceVyhodnoteniaVzdelavaniePouzivatelTestovanieTestyTable')) {
                $this->publishes([
                    __DIR__ . '/../database/migrations/pridane_stlpce_vyhodnotenia_pouzivatel_testovanie_testy_table.php.stub' => database_path('migrations/2020_09_11_114000_pridane_stlpce_vyhodnotenia_vzdelavanie_pouzivatel_testovanie_testy_table.php'),
                ], 'migrations');
            }

            if (!class_exists('PridanyStlpecCasovejZnackySpusteniaTestuVzdelavaniePouzivatelTestovanieTestyTable')) {
                $this->publishes([
                    __DIR__ . '/../database/migrations/pridany_stlpec_casovej_znacky_spustenia_testu_pouzivatel_testovanie_testy_table.php.stub' => database_path('migrations/2020_09_21_084000_pridany_stlpec_casovej_znacky_spustenia_testu_vzdelavanie_pouzivatel_testovanie_testy_table.php'),
                ], 'migrations');
            }

            if (!class_exists('PridanyStlpecStavuVyhodnoteniaTestuVzdelavaniePouzivatelTestovanieTestyTable')) {
                $this->publishes([
                    __DIR__ . '/../database/migrations/pridany_stlpec_stavu_vyhodnotenia_testu_pouzivatel_testovanie_testy_table.php.stub' => database_path('migrations/2021_01_29_142000_pridany_stlpec_stavu_vyhodnotenia_testu_vzdelavanie_pouzivatel_testovanie_testy_table.php'),
                ], 'migrations');
            }

            if (!class_exists('PridaneIndexyPreStlpceStavUpdatedAtPouzivatelTestovanieTestyTable')) {
                $this->publishes([
                    __DIR__ . '/../database/migrations/20210917_100000_pridane_indexy_pre_stlpce_stav_updated_at_pouzivatel_testovanie_testy_table.php.stub' => database_path('migrations/20210917_100000_pridane_indexy_pre_stlpce_stav_updated_at_pouzivatel_testovanie_testy_table.php'),
                ], 'migrations');
            }
        }
    }

    /**
     *
     */
    private function databaseFactories(): void
    {
        if (! class_exists('VzdelavanieKniznicaKategoriaFactory')) {
            $this->publishes([
                __DIR__ . '/../database/factories/KniznicaKategoriaFactory.php.stub' => database_path('factories/VzdelavanieKniznicaKategoriaFactory.php'),
            ], 'factories');
        }

        if (! class_exists('VzdelavanieKniznicaUcebnicaFactory')) {
            $this->publishes([
                __DIR__ . '/../database/factories/KniznicaUcebnicaFactory.php.stub' => database_path('factories/VzdelavanieKniznicaUcebnicaFactory.php'),
            ], 'factories');
        }

        if (! class_exists('VzdelavanieKniznicaSekciaFactory')) {
            $this->publishes([
                __DIR__ . '/../database/factories/KniznicaSekciaFactory.php.stub' => database_path('factories/VzdelavanieKniznicaSekciaFactory.php'),
            ], 'factories');
        }

        if (! class_exists('VzdelavanieTestovanieTestFactory')) {
            $this->publishes([
                __DIR__ . '/../database/factories/TestovanieTestFactory.php.stub' => database_path('factories/VzdelavanieTestovanieTestFactory.php'),
            ], 'factories');
        }

        if (! class_exists('VzdelavanieTestovanieOtazkaFactory')) {
            $this->publishes([
                __DIR__ . '/../database/factories/TestovanieOtazkaFactory.php.stub' => database_path('factories/VzdelavanieTestovanieOtazkaFactory.php'),
            ], 'factories');
        }

        if (! class_exists('VzdelavanieTestovanieOdpovedFactory')) {
            $this->publishes([
                __DIR__ . '/../database/factories/TestovanieOdpovedFactory.php.stub' => database_path('factories/VzdelavanieTestovanieOdpovedFactory.php'),
            ], 'factories');
        }

        if (! class_exists('VzdelavaniePouzivatelKniznicaOblubeneFactory')) {
            $this->publishes([
                __DIR__ . '/../database/factories/PouzivatelKniznicaOblubeneFactory.php.stub' => database_path('factories/VzdelavaniePouzivatelKniznicaOblubeneFactory.php'),
            ], 'factories');
        }
    }

    /**
     *
     */
    private function databaseSeeders(): void
    {
        $this->publishes([
            __DIR__ . '/../database/seeds/VzdelavaniePouzivatelRoleOpravneniaDatabaseSeeder.php.stub' => database_path('seeds/VzdelavaniePouzivatelRoleOpravneniaDatabaseSeeder.php'),
        ], 'seeds');

        $this->publishes([
            __DIR__ . '/../database/seeds/VzdelavanieKniznicaDatabaseSeeder.php.stub' => database_path('seeds/VzdelavanieKniznicaDatabaseSeeder.php'),
        ], 'seeds');

        $this->publishes([
            __DIR__ . '/../database/seeds/VzdelavanieTestovanieDatabaseSeeder.php.stub' => database_path('seeds/VzdelavanieTestovanieDatabaseSeeder.php'),
        ], 'seeds');
    }

    /**
     *
     */
    private function loadLivewireComponents(): void
    {
        Livewire::component('Vzdelavanie::components.stav', Stav::class);
        Livewire::component('Vzdelavanie::components.pracoviska', Pracoviska::class);
        Livewire::component('Vzdelavanie::components.searchtext', SearchText::class);

        Livewire::component('Vzdelavanie::pages.sprava.testovanie.pouzivatelia.pages.index', Index::class);
        Livewire::component('Vzdelavanie::pages.sprava.testovanie.pouzivatelia.components.zoznam', Zoznam::class);
    }
}
