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

namespace Seka\Vzdelavanie;

use ConsoleTVs\Charts\Registrar;
use Illuminate\Support\ServiceProvider;
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 Illuminate\Database\Eloquent\Relations\Relation;
use Seka\Vzdelavanie\Services\SekciaServiceInterface;
use Seka\Vzdelavanie\Services\UcebnicaServiceInterface;
use Seka\Vzdelavanie\Services\KategoriaServiceInterface;
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\Composers\Kniznica\ZoznamUcebnychMaterialovPodlaKategoriiComposer;

/**
 * Class VzdelavanieServiceProvider
 * @package Seka\Vzdelavanie
 */
class VzdelavanieServiceProvider extends ServiceProvider
{
    /**
     * @throws \Illuminate\Contracts\Container\BindingResolutionException
     */
    public function boot(): void
    {
        $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');
    }

    /**
     * @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()) {
            $this->loadMigrationsFrom(__DIR__.'/../database/migrations');
        }

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

    /**
     *
     */
    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
    {
        if (! class_exists('VzdelavaniePouzivatelRoleOpravneniaDatabaseSeeder')) {
            $this->publishes([
                __DIR__ . '/../database/seeds/VzdelavaniePouzivatelRoleOpravneniaDatabaseSeeder.php.stub' => database_path('seeds/VzdelavaniePouzivatelRoleOpravneniaDatabaseSeeder.php'),
            ], 'seeds');
        }

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

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