<?php

namespace Buyesrfolio\TransactionFile\Providers;

use Illuminate\Support\ServiceProvider;

class TransactionFileServiceProvider extends ServiceProvider
{
    /**
     * Indicates if loading of the provider is deferred.
     *
     * @var bool
     */
    protected $defer = false;

    /**
     * Bootstrap the application events.
     *
     * @return void
     */
    public function boot()
    {
        // Load view
        $this->loadViewsFrom(__DIR__ . '/../../resources/views', 'transaction_file');

        // Load translation
        $this->loadTranslationsFrom(__DIR__ . '/../../resources/lang', 'transaction_file');

        // Load migrations
        $this->loadMigrationsFrom(__DIR__ . '/../../database/migrations');

        // Call pblish redources function
        $this->publishResources();

    }

    /**
     * Register the service provider.
     *
     * @return void
     */
    public function register()
    {
        $this->mergeConfig();
        $this->registerTransactionFile();
        $this->registerFacade();
        $this->registerBindings();
        //$this->registerCommands();
    }


    /**
     * Register the application bindings.
     *
     * @return void
     */
    protected function registerTransactionFile()
    {
        $this->app->bind('transaction_file', function($app) {
            return new TransactionFile($app);
        });
    }

    /**
     * Register the vault facade without the user having to add it to the app.php file.
     *
     * @return void
     */
    public function registerFacade() {
        $this->app->booting(function()
        {
            $loader = \Illuminate\Foundation\AliasLoader::getInstance();
            $loader->alias('TransactionFile', 'Lavalite\TransactionFile\Facades\TransactionFile');
        });
    }

    /**
     * Register bindings for the provider.
     *
     * @return void
     */
    public function registerBindings() {
        // Bind facade
        $this->app->bind('buyesrfolio.transaction_file', function ($app) {
            return $this->app->make('Buyesrfolio\TransactionFile\TransactionFile');
        });

                // Bind TransactionFile to repository
        $this->app->bind(
            'Buyesrfolio\TransactionFile\Interfaces\TransactionFileRepositoryInterface',
            \Buyesrfolio\TransactionFile\Repositories\Eloquent\TransactionFileRepository::class
        );

        $this->app->register(\Buyesrfolio\TransactionFile\Providers\AuthServiceProvider::class);
        
        $this->app->register(\Buyesrfolio\TransactionFile\Providers\RouteServiceProvider::class);
            }

    /**
     * Merges user's and transaction_file's configs.
     *
     * @return void
     */
    protected function mergeConfig()
    {
        $this->mergeConfigFrom(
            __DIR__ . '/../../config/config.php', 'buyesrfolio.transaction_file'
        );
    }

    /**
     * Register scaffolding command
     */
    protected function registerCommands()
    {
        if ($this->app->runningInConsole()) {
            $this->commands([
                Commands\MakeTransactionFile::class,
            ]);
        }
    }
    /**
     * Get the services provided by the provider.
     *
     * @return array
     */
    public function provides()
    {
        return ['buyesrfolio.transaction_file'];
    }

    /**
     * Publish resources.
     *
     * @return void
     */
    private function publishResources()
    {
        // Publish configuration file
        $this->publishes([__DIR__ . '/../../config/config.php' => config_path('buyesrfolio/transaction_file.php')], 'config');

        // Publish admin view
        $this->publishes([__DIR__ . '/../../resources/views' => base_path('resources/views/vendor/transaction_file')], 'view');

        // Publish language files
        $this->publishes([__DIR__ . '/../../resources/lang' => base_path('resources/lang/vendor/transaction_file')], 'lang');

        // Publish public files and assets.
        $this->publishes([__DIR__ . '/public/' => public_path('/')], 'public');
    }
}