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

namespace Seka\Vzdelavanie;

use Illuminate\Support\Facades\Gate;
use Illuminate\Support\ServiceProvider;
use Seka\Vzdelavanie\Services\SekciaService;
use Seka\Vzdelavanie\Services\UcebnicaService;
use Seka\Vzdelavanie\Services\KategoriaService;
use Seka\Vzdelavanie\Services\SekciaServiceInterface;
use Seka\Vzdelavanie\Services\UcebnicaServiceInterface;
use Seka\Vzdelavanie\Composers\ZoznamKategoriiComposer;
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\Repositories\Contracts\SekciaRepositoryInterface;
use Seka\Vzdelavanie\Repositories\Contracts\UcebnicaRepositoryInterface;
use Seka\Vzdelavanie\Repositories\Contracts\KategoriaRepositoryInterface;
use Seka\Vzdelavanie\Composers\ZoznamUcebnychMaterialovPodlaKategoriiComposer;

/**
 * Class VzdelavanieServiceProvider
 * @package Seka\Vzdelavanie
 */
class VzdelavanieServiceProvider extends ServiceProvider
{
    /**
     *
     */
    public function boot(): void
    {
        $this->app->booted(function () {
            $this->gates();
            $this->routes();
        });
        $this->publish();
        $this->migrations();
        $this->databaseFactories();
        $this->databaseSeeders();
        $this->loadViewsFrom(__DIR__.'/../resources/views', 'Vzdelavanie');
    }

    /**
     *
     */
    protected function gates(): void
    {
        Gate::define('edit', function ($user) {
            /** @var \Seka\Vzdelavanie\Models\PouzivatelInterface $user */
            return $user->isAdmin();
        });

        Gate::define('read', function ($user) {
            /** @var \Seka\Vzdelavanie\Models\PouzivatelInterface $user */
            return $user->isStudent() || $user->isAdmin();
        });
    }

    /**
     *
     */
    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.ucebne_materialy.partials.zoznam_kategorii'],
            ZoznamKategoriiComposer::class
        );
        view()->composer(
            ['Vzdelavanie::pages.ucebne_materialy.partials.zoznam_ucebnych_materialov_podla_kategorii'],
            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'),
        ], 'resources');

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

    /**
     *
     */
    private function migrations(): void
    {
        if ($this->app->runningInConsole()) {
            $this->loadMigrationsFrom(__DIR__.'/../database/migrations');
        }

        if (! class_exists('CreateKategorieTable')) {
            $timestamp = date('Y_m_d_His', time());
            $this->publishes([
                __DIR__.'/../database/migrations/create_kategorie_table.php.stub' => database_path('migrations/'.$timestamp.'_create_kategorie_table.php'),
            ], 'migrations');
        }

        if (! class_exists('CreateUcebniceTable')) {
            $timestamp = date('Y_m_d_His', time() + 1);
            $this->publishes([
                __DIR__.'/../database/migrations/create_ucebnice_table.php.stub' => database_path('migrations/'.$timestamp.'_create_ucebnice_table.php'),
            ], 'migrations');
        }

        if (! class_exists('CreateSekcieTable')) {
            $timestamp = date('Y_m_d_His', time() + 2);
            $this->publishes([
                __DIR__.'/../database/migrations/create_sekcie_table.php.stub' => database_path('migrations/'.$timestamp.'_create_sekcie_table.php'),
            ], 'migrations');
        }
    }

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

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

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