<?php

namespace Bixo\Account\Providers;

use Illuminate\Foundation\Support\Providers\RouteServiceProvider as ServiceProvider;
use Bixo\Account\Models\Invoice;
use Bixo\Account\Models\Coa;
use Bixo\Account\Models\Payrec;
use Bixo\Account\Models\PayrecDetail;
use Bixo\Account\Models\PayrecReceipts;
use Bixo\Account\Models\InvoiceDetail;
use Bixo\Account\Models\Product;
use Bixo\Account\Models\Costcenter;
use Bixo\Account\Models\Account;
use Bixo\Account\Models\Drcrnote;
use Bixo\Account\Models\DrcrnoteDetail;
use Bixo\Account\Models\Bank;
use Bixo\Account\Models\Customer;
use Bixo\Account\Models\Acknowledgement;
use Bixo\Account\Models\AcknowledgementDetail;
use Bixo\Account\Models\Allocation;
use Bixo\Account\Models\Journal;
use Bixo\Account\Models\JournalDetail;
use Bixo\Account\Models\Ledger;
use Bixo\Account\Models\Transaction;
use Bixo\Account\Models\TransactionsDetail;

use Request;
use Route;

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 = 'Bixo\Account\Http\Controllers';

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

        if (Request::is('*/account/invoice/*')) {
            Route::bind('invoice', function ($invoice) {
                return Invoice::findorNew($invoice);
            });
        }
        if (Request::is('*/account/coa/*')) {
            Route::bind('coa', function ($coa) {
                return Coa::findorNew($coa);
            });
        }
        if (Request::is('*/account/payrec/*')) {
            Route::bind('payrec', function ($payrec) {
                return Payrec::findorNew($payrec);
            });
        }
        if (Request::is('*/account/payrec_detail/*')) {
            Route::bind('payrec_detail', function ($payrec_detail) {
                return PayrecDetail::findorNew($payrec_detail);
            });
        }
        if (Request::is('*/account/payrec_receipts/*')) {
            Route::bind('payrec_receipts', function ($payrec_receipts) {
                return PayrecReceipts::findorNew($payrec_receipts);
            });
        }
        if (Request::is('*/account/invoice_detail/*')) {
            Route::bind('invoice_detail', function ($invoice_detail) {
                return InvoiceDetail::findorNew($invoice_detail);
            });
        }
        if (Request::is('*/account/product/*')) {
            Route::bind('product', function ($product) {
                return Product::findorNew($product);
            });
        }
        if (Request::is('*/account/costcenter/*')) {
            Route::bind('costcenter', function ($costcenter) {
                return Costcenter::findorNew($costcenter);
            });
        }
        if (Request::is('*/account/account/*')) {
            Route::bind('account', function ($account) {
                return Account::findorNew($account);
            });
        }
        if (Request::is('*/account/drcrnote/*')) {
            Route::bind('drcrnote', function ($drcrnote) {
                return Drcrnote::findorNew($drcrnote);
            });
        }
        if (Request::is('*/account/drcrnote_detail/*')) {
            Route::bind('drcrnote_detail', function ($drcrnote_detail) {
                return DrcrnoteDetail::findorNew($drcrnote_detail);
            });
        }
        if (Request::is('*/account/bank/*')) {
            Route::bind('bank', function ($bank) {
                return Bank::findorNew($bank);
            });
        }
        if (Request::is('*/account/customer/*')) {
            Route::bind('customer', function ($customer) {
                return Customer::findorNew($customer);
            });
        }
        if (Request::is('*/account/acknowledgement/*')) {
            Route::bind('acknowledgement', function ($acknowledgement) {
                return Acknowledgement::findorNew($acknowledgement);
            });
        }
        if (Request::is('*/account/acknowledgement_detail/*')) {
            Route::bind('acknowledgement_detail', function ($acknowledgement_detail) {
                return AcknowledgementDetail::findorNew($acknowledgement_detail);
            });
        }
        if (Request::is('*/account/allocation/*')) {
            Route::bind('allocation', function ($allocation) {
                return Allocation::findorNew($allocation);
            });
        }
        if (Request::is('*/account/journal/*')) {
            Route::bind('journal', function ($journal) {
                return Journal::findorNew($journal);
            });
        }
        if (Request::is('*/account/journal_detail/*')) {
            Route::bind('journal_detail', function ($journal_detail) {
                return JournalDetail::findorNew($journal_detail);
            });
        }
        if (Request::is('*/account/ledger/*')) {
            Route::bind('ledger', function ($ledger) {
                return Ledger::findorNew($ledger);
            });
        }
        if (Request::is('*/account/transaction/*')) {
            Route::bind('transaction', function ($transaction) {
                return Transaction::findorNew($transaction);
            });
        }
        if (Request::is('*/account/transactions_detail/*')) {
            Route::bind('transactions_detail', function ($transactions_detail) {
                return TransactionsDetail::findorNew($transactions_detail);
            });
        }

    }

    /**
     * Define the routes for the package.
     *
     * @return void
     */
    public function map()
    {
        $this->mapWebRoutes();

        $this->mapApiRoutes();
    }

    /**
     * Define the "web" routes for the package.
     *
     * These routes all receive session state, CSRF protection, etc.
     *
     * @return void
     */
    protected function mapWebRoutes()
    {   
        Route::group([
            'middleware' => 'web',
            'namespace'  => $this->namespace,
        ], function ($router) {
            require (__DIR__ . '/../../routes/web.php');
        });
    }

    /**
     * Define the "api" routes for the package.
     *
     * These routes are typically stateless.
     *
     * @return void
     */
    protected function mapApiRoutes()
    {
        Route::group([
            'middleware' => 'api',
            'namespace'  => $this->namespace,
            'prefix'     => 'api',
        ], function ($router) {
            require (__DIR__ . '/../../routes/api.php');
        });
    }

}