<?php
namespace Support;

use Tymon\JWTAuth\Providers\JWTAuthServiceProvider;

class JWTBandServiceProvider extends JWTAuthServiceProvider {

    /**
     * Boot the service provider.
     */
    public function boot()
    {
        $this->bootBindings();

        // register the filter
        $this->app['router']->filter('jwt-band-auth', 'jwt.band.filter');
    }

    /**
     * Bind some Interfaces and implementations
     */
    protected function bootBindings()
    {
        $this->app['JWTBandAuth'] = function ($app) {
            return $app['jwt.band.auth'];
        };

        $this->app['Support\JWTBandUserInterface'] = function ($app) {
            return $app['jwt.band.provider.user'];
        };
    }

     /**
     * Register the service provider.
     *
     * @return void
     */
    public function register()
    {
        // register providers
        $this->registerJWTAuth();
        $this->registerUserProvider();
        $this->registerJWTAuthFilter();
    }


    /**
     * Register the bindings for the main JWTAuth class
     */
    protected function registerJWTAuth()
    {
        $this->app['jwt.band.auth'] = $this->app->share(function ($app) {

            $auth = new JWTBandAuth(
                $app['tymon.jwt.manager'],
                $app['jwt.band.provider.user'],
                $app['tymon.jwt.provider.auth'],
                $app['request']
            );

            return $auth->setIdentifier($this->config('identifier'));
        });
    }

    /**
     * Register the bindings for the User provider
     */
    protected function registerUserProvider()
    {
        $this->app['jwt.band.provider.user'] = $this->app->share(function ($app) {
            return $app->make(JWTBandEloquentUserAdapter::class, [$app->make(\Band::class)]);
        });
    }

    /**
     * Register the bindings for the 'jwt-auth' filter
     */
    protected function registerJWTAuthFilter()
    {
        $this->app['jwt.band.filter'] = $this->app->share(function ($app) {
            return new JWTBandAuthFilter($app['events'], $app['jwt.band.auth']);
        });
    }
}
