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

namespace Seka\Vzdelavanie;

use Illuminate\Support\ServiceProvider;
use Seka\Vzdelavanie\Services\SekciaService;
use Seka\Vzdelavanie\Models\Kniznica\Sekcia;
use Seka\Vzdelavanie\Services\UcebnicaService;
use Seka\Vzdelavanie\Models\Kniznica\Ucebnica;
use Seka\Vzdelavanie\Services\KategoriaService;
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\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\Kniznica\ZoznamUcebnychMaterialovPodlaKategoriiComposer;

/**
 * Class VzdelavanieServiceProvider
 * @package Seka\Vzdelavanie
 */
class VzdelavanieServiceProvider extends ServiceProvider
{
    /**
     *
     */
    public function boot(): void
    {
        $this->app->booted(function () {
            $this->routes();

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

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

    /**
     *
     */
    protected function routes(): void
    {
        $this->loadRoutesFrom(__DIR__ . '/../routes/web.php');
        $this->loadRoutesFrom(__DIR__ . '/../routes/api.php');
    }

    /**
     *
     */
    public function register(): void
    {
        $this->registerRepositoryBindings();
        $this->registerServiceBindings();
        $this->registerConfig();
        $this->registerComposers();
    }

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

    /**
     *
     */
    protected function registerConfig(): void
    {
        $this->mergeConfigFrom(
            __DIR__.'/../config/vzdelavanie.php',
            'vzdelavanie'
        );
    }

    /**
     *
     */
    public function registerComposers(): void
    {
        view()->composer(
            ['Vzdelavanie::pages.kniznica.index'],
            ZoznamKategoriiComposer::class
        );
        view()->composer(
            ['Vzdelavanie::pages.kniznica.index'],
            ZoznamUcebnychMaterialovPodlaKategoriiComposer::class
        );
    }

    /**
     *
     */
    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
    {
        $index = 0;

        if ($this->app->runningInConsole()) {
            $this->loadMigrationsFrom(__DIR__.'/../database/migrations');
        }

        if (! class_exists('CreateVzdelavanieKniznicaKategorieTable')) {
            $timestamp = date('Y_m_d_His', time() + ++$index);
            $this->publishes([
                __DIR__ . '/../database/migrations/create_kniznica_kategorie_table.php.stub' => database_path('migrations/'.$timestamp.'_create_vzdelavanie_kniznica_kategorie_table.php'),
            ], 'migrations');
        }

        if (! class_exists('CreateVzdelavanieKniznicaUcebniceTable')) {
            $timestamp = date('Y_m_d_His', time() + ++$index);
            $this->publishes([
                __DIR__ . '/../database/migrations/create_kniznica_ucebnice_table.php.stub' => database_path('migrations/'.$timestamp.'_create_vzdelavanie_kniznica_ucebnice_table.php'),
            ], 'migrations');
        }

        if (! class_exists('CreateVzdelavanieKniznicaSekcieTable')) {
            $timestamp = date('Y_m_d_His', time() + ++$index);
            $this->publishes([
                __DIR__ . '/../database/migrations/create_kniznica_sekcie_table.php.stub' => database_path('migrations/'.$timestamp.'_create_vzdelavanie_kniznica_sekcie_table.php'),
            ], 'migrations');
        }

        if (! class_exists('CreateVzdelavanieTestovanieKategorieTable')) {
            $timestamp = date('Y_m_d_His', time() + ++$index);
            $this->publishes([
                __DIR__.'/../database/migrations/create_testovanie_kategorie_table.php.stub' => database_path('migrations/'.$timestamp.'_create_vzdelavanie_testovanie_kategorie_table.php'),
            ], 'migrations');
        }

        if (! class_exists('CreateVzdelavanieTestovanieTestyTable')) {
            $timestamp = date('Y_m_d_His', time() + ++$index);
            $this->publishes([
                __DIR__.'/../database/migrations/create_testovanie_testy_table.php.stub' => database_path('migrations/'.$timestamp.'_create_vzdelavanie_testovanie_testy_table.php'),
            ], 'migrations');
        }

        if (! class_exists('CreateVzdelavanieTestovanieOtazkyTable')) {
            $timestamp = date('Y_m_d_His', time() + ++$index);
            $this->publishes([
                __DIR__.'/../database/migrations/create_testovanie_otazky_table.php.stub' => database_path('migrations/'.$timestamp.'_create_vzdelavanie_testovanie_otazky_table.php'),
            ], 'migrations');
        }

        if (! class_exists('CreateVzdelavanieTestovanieOdpovedeTable')) {
            $timestamp = date('Y_m_d_His', time() + ++$index);
            $this->publishes([
                __DIR__.'/../database/migrations/create_testovanie_odpovede_table.php.stub' => database_path('migrations/'.$timestamp.'_create_vzdelavanie_testovanie_odpovede_table.php'),
            ], 'migrations');
        }

        if (! class_exists('CreateVzdelavaniePouzivatelTestovanieTestyTable')) {
            $timestamp = date('Y_m_d_His', time() + ++$index);
            $this->publishes([
                __DIR__.'/../database/migrations/create_pouzivatel_testovanie_testy_table.php.stub' => database_path('migrations/'.$timestamp.'_create_vzdelavanie_pouzivatel_testovanie_testy_table.php'),
            ], 'migrations');
        }

        if (! class_exists('CreateVzdelavaniePouzivatelTestovanieOtazkyTable')) {
            $timestamp = date('Y_m_d_His', time() + ++$index);
            $this->publishes([
                __DIR__.'/../database/migrations/create_pouzivatel_testovanie_otazky_table.php.stub' => database_path('migrations/'.$timestamp.'_create_vzdelavanie_pouzivatel_testovanie_otazky_table.php'),
            ], 'migrations');
        }

        if (! class_exists('CreateVzdelavaniePouzivatelTestovanieOdpovedeTable')) {
            $timestamp = date('Y_m_d_His', time() + ++$index);
            $this->publishes([
                __DIR__.'/../database/migrations/create_pouzivatel_testovanie_odpovede_table.php.stub' => database_path('migrations/'.$timestamp.'_create_vzdelavanie_pouzivatel_testovanie_odpovede_table.php'),
            ], 'migrations');
        }

        if (! class_exists('CreateVzdelavaniePouzivatelKniznicaOblubeneTable')) {
            $timestamp = date('Y_m_d_His', time() + ++$index);
            $this->publishes([
                __DIR__.'/../database/migrations/create_pouzivatel_kniznica_oblubene_table.php.stub' => database_path('migrations/'.$timestamp.'_create_vzdelavanie_pouzivatel_kniznica_oblubene_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('VzdelavanieTestovanieKategoriaFactory')) {
            $this->publishes([
                __DIR__ . '/../database/factories/TestovanieKategoriaFactory.php.stub' => database_path('factories/VzdelavanieTestovanieKategoriaFactory.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.php')) {
            $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');
        }
    }
}
