<?php

namespace App\Providers;

use Illuminate\Http\Resources\Json\JsonResource;
use Illuminate\Support\Facades\Schema;
use Illuminate\Support\Facades\Validator;
use Illuminate\Support\ServiceProvider;
use App\Models\Add;
use App\Services\AddDataService;
use App\Facades\Services\ConfigurationService;
use Illuminate\Http\Client\Request;

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

    /**
     * Bootstrap any application services.
     *
     * @return void
     */
    public function boot(AddDataService $addDataService)
    {

        ConfigurationService::loadConfigs();
        Schema::defaultStringLength(191);

        Validator::extend('htmlMax', function ($attribute, $htmlValue, $parameters, $validator) {
            $validator->requireParameterCount(1, $parameters, 'htmlMax');
            $value = strip_tags($htmlValue);
            return $validator->validateMax($attribute, $value, $parameters);
        });

        Validator::replacer('htmlMax', function ($message, $attribute, $rule, $parameters) {
            return str_replace(':max', $parameters[0], $message);
        });

        Add::saving(function ($add) use ($addDataService) {

            $entryScreen = Request()->get('entry_screen');
            if ($entryScreen) {
                //  reset all other adds entry screen option
                $addDataService->resetEntryScreen($add->id);
            }
        });
    }
}
