<?php

  /*
  |--------------------------------------------------------------------------
  | Register The Laravel Class Loader
  |--------------------------------------------------------------------------
  |
  | In addition to using Composer, you may use the Laravel class loader to
  | load your controllers and models. This is useful for keeping all of
  | your classes in the "global" namespace without Composer updating.
  |
  */

  ClassLoader::addDirectories([

    app_path() . '/commands',
    app_path() . '/controllers',
    app_path() . '/models',
    app_path() . '/database/seeds',

  ]);

  /*
  |--------------------------------------------------------------------------
  | Application Error Logger
  |--------------------------------------------------------------------------
  |
  | Here we will configure the error logger setup for the application which
  | is built on top of the wonderful Monolog library. By default we will
  | build a basic log file setup which creates a single file for logs.
  |
  */

  if (App::runningInConsole()) {
    $logFile = 'laravel-cli.log';
  } else {
    $logFile = 'laravel.log';
  }

  Log::useDailyFiles(storage_path() . '/logs/' . $logFile, 0, Config::get("app.log_level"));

  /*
  |--------------------------------------------------------------------------
  | Application Error Handler
  |--------------------------------------------------------------------------
  |
  | Here you may handle any errors that occur in your application, including
  | logging them or displaying custom views for specific errors. You may
  | even register several error handlers to handle different types of
  | exceptions. If nothing is returned, the default error view is
  | shown, which includes a detailed stack trace during debug.
  |
  */

  App::error(function ($exception, $code) {
    Log::error($exception);

//    if($exception instanceof Eae\Exception\ApiHttpException){
//        if( Request::wantsJson() || Request::ajax()){
//            return Response::json($exception->getApiMessage(),
//                $exception->getStatusCode());
//        }else{
//            return Response::view(
//                'frontend.errors.default',
//                $exception->getApiMessage(),
//                $exception->getStatusCode());
//        }
//    }
//
//    if(!Config::get("app.debug")){
//        return Response::view(
//                    'frontend.errors.default',
//                    array(
//                        "message" => $exception->getMessage(),
//                        "code" => $code,
//                    ),
//                    $code);
//    }

//    if (!Config::get('app.debug')) {
//        $obj = new stdClass();
//        $obj->id = null;
//        $website = Localization::getWebsite();
//        $view404 = $website->id == 2 ? 'frontend.cf.404' : 'frontend.404';
//        switch ($code)
//        {
//            case 403:
//                return Response::view($view404, array('object' => $obj, 'errorCode' => 403), 403);
//
//            case 404:
//                return Response::view($view404, array('object' => $obj, 'errorCode' => 404), 404);
//
//            case 500:
//                return Response::view($view404, array('object' => $obj, 'errorCode' => 500), 500);
//
//            default:
//                return Response::view($view404, array('object' => $obj, 'errorCode' => $code));
//        }
//    }

    if (!Config::get("app.debug")) {
      return Response::view(
        'frontend.pages.404', [
        'metaUser' => []
      ]);
    }

  });

  /*
  |--------------------------------------------------------------------------
  | Maintenance Mode Handler
  |--------------------------------------------------------------------------
  |
  | The "down" Artisan command gives you the ability to put an application
  | into maintenance mode. Here, you will define what is displayed back
  | to the user if maintenance mode is in effect for the application.
  |
  */

  App::down(function () {
    return Response::make("Be right back!", 503);
  });

  /*
  |--------------------------------------------------------------------------
  | Require The Filters File
  |--------------------------------------------------------------------------
  |
  | Next we will load the filters file for the application. This gives us
  | a nice separate location to store our route and application filter
  | definitions instead of putting them all in the main routes file.
  |
  */

  require app_path() . '/helpers/string_helper.php';
  require app_path() . '/helpers/array_helper.php';
  require app_path() . '/helpers/date_helper.php';
  require app_path() . '/helpers/database_helper.php';
  require app_path() . '/helpers/html_helper.php';
  require app_path() . '/helpers/language_helper.php';
  require app_path() . '/helpers/media_helper.php';
  require app_path() . '/filters.php';
  require app_path() . '/composers.php';
  require app_path() . '/observers.php';

  Validator::resolver(function ($translator, $data, $rules, $messages) {
    return new Eae\Support\CustomValidator($translator, $data, $rules, $messages);
  });

  Blade::extend(function ($value) {
    return preg_replace('/(\{\?)/', '<?php ', $value);
  });

  Blade::extend(function ($value) {
    return preg_replace('/(\?\})/', ' ?>', $value);
  });

  Blade::extend(function ($value) {
    return preg_replace('/(\s*)@(break|continue)(\s*)/', '$1<?php $2; ?>$3', $value);
  });
