<?php

namespace App\Providers;

use Illuminate\Support\ServiceProvider;
use Illuminate\Support\Facades\Schema;
use Illuminate\Support\Facades\Validator;
use Illuminate\Support\Facades\Input;
use DB;

use Jenssegers\Date\Date;


class AppServiceProvider extends ServiceProvider
{
    /**
     * Bootstrap any application services.
     *
     * @return void
     */
    public function boot()
    {
        //
        Schema::defaultStringLength(191);

        Date::setLocale('en');

        /*DB::listen(function ($query) {
            var_dump([
                $query->sql,
                $query->bindings,
                $query->time
            ]);
        });*/


        // greater_or_equal_than
        Validator::extend('greater_or_equal_than', function($attribute, $value, $params, $validator){
            $other = Input::get($params[0]);
            return intval($value) >= intval($other);
        });


        Validator::replacer('greater_or_equal_than', function($message, $attribute, $rule, $params) {
            return str_replace('_', ' ' , $attribute .' mora biti veca ili jednaka od vrednosti ' .$params[0]);
        });

        // ifsystemslug - provera da li je slug unosa sistemska putana

        Validator::extend('ifsystemslug', function($attribute, $value, $params, $validator){
            //$other = Input::get($params[0]);
            $reservedslugs = ['cart','checkout','compare','contact', 'login','logout','makeorder','panel','register','search','en','sr','sk', 'time', 'user'];
            return !in_array($value,$reservedslugs);
        });

        Validator::replacer('ifsystemslug', function($message, $attribute, $rule, $params) {
            return str_replace('_', ' ' , 'Uneta '.$attribute .' je sistemska putanja, promenite je');
        });

        // ifslugstartswith - Provera da li slug pocinje kao sistemske putanje
        Validator::extend('ifslugstartswith', function($attribute, $value, $params, $validator){
            $ifvalid = 1;
            $forbiden = ['ajax-','art-','cartremove-','currency-','panel/','password/','user-','users/','cisco'];
            foreach($forbiden as $prefix){
                if (strncmp($value, $prefix, strlen($prefix)) === 0){
                    $ifvalid = 0;
                    break;
                }
            }

            return $ifvalid;
        });

        /*Validator::replacer('ifslugstartswith', function($message, $attribute, $rule, $params, $value) {
            // za ovo message je validation.ifurladresastartswith
            return $attribute;//'Uneta '.$attribute .' pocinje kao sistemska putanja, uklonite "'$value.'" sa pocetka';
        });*/

        // ifslugsafe - provera da li slug sadrzi nedozvoljene karaktere

        Validator::extend('ifslugsafe', function($attribute, $value, $params, $validator){
            $other = Input::get($params[0]);
            return intval($value) >= intval($other);
        });


        Validator::replacer('ifslugsafe', function($message, $attribute, $rule, $params) {
            return str_replace('_', ' ' , $attribute .' mora biti veca ili jednaka od vrednosti ' .$params[0]);
        });

    }

    /**
     * Register any application services.
     *
     * @return void
     */
    public function register()
    {
        //
    }
}
