<?php

namespace Guru\Product\Providers;

use Illuminate\Foundation\Support\Providers\RouteServiceProvider as ServiceProvider;
use Illuminate\Routing\Router;
use Guru\Product\Models\Product;
use Request;

class RouteServiceProvider extends ServiceProvider
{
    /**
     * This namespace is applied to the controller routes in your routes file.
     *
     * In addition, it is set as the URL generator's root namespace.
     *
     * @var string
     */
    protected $namespace = 'Guru\Product\Http\Controllers';

    /**
     * Define your route model bindings, pattern filters, etc.
     *
     * @param   \Illuminate\Routing\Router  $router
     * @return void
     */
    public function boot(Router $router)
    {
        parent::boot($router);

        if (Request::is('*/product/product/*')) {
            $router->bind('product', function ($id) {
                $product = $this->app->make('\Guru\Product\Interfaces\ProductRepositoryInterface');
                return $product->findorNew($id);
            });
        }if (Request::is('*/product/category/*')) {
            $router->bind('category', function ($id) {
                $category = $this->app->make('\Guru\Product\Interfaces\CategoryRepositoryInterface');
                return $category->findorNew($id);
            });
        }if (Request::is('*/product/price/*')) {
            $router->bind('price', function ($id) {
                $price = $this->app->make('\Guru\Product\Interfaces\PriceRepositoryInterface');
                return $price->findorNew($id);
            });
        }

    }

    /**
     * Define the routes for the application.
     *
     * @param \Illuminate\Routing\Router $router
     *
     * @return void
     */
    public function map(Router $router)
    {
        $router->group(['namespace' => $this->namespace], function ($router) {
            require __DIR__ . '/../Http/routes.php';
        });
    }
}