<?php

namespace App\Http\Requests;

use Illuminate\Validation\Rule;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Request;
use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Validation\ValidationException;
use Illuminate\Contracts\Validation\Validator;
use Illuminate\Http\Exceptions\HttpResponseException;

/**
 * @property mixed page
 */
class PageRequest extends FormRequest
{
    /**
     * Determine if the user is authorized to make this request.
     *
     * @return bool
     */
    public function authorize()
    {
        if (! Auth::check()) {
            return false;
        }
        if(in_array(Auth::user()->role, ['super_admin', 'admin', 'sub_admin']))
        {
            return true;
        }

        if(Auth::user()->hasModule( 'pages' ) )
        {
            return true;
        }
        return Auth::check();
    }

    /**
     * Get the validation rules that apply to the request.
     *
     * @return array
     */
    public function rules()
    {
        $rules = [];

        if ($this->page->id == 1){
            $rules['why_us_experience_years'] = 'required|numeric|gt:0';
            foreach (languages() as $lang) {
                $lang_rules=[
                    'why_us_title_'.$lang->local=> 'required|min:3|max:100',
                    'why_us_experience_years_title_'.$lang->local=> 'required|min:3|max:100',
                    'why_us_description_'.$lang->local=> 'required|min:3',
                ];
                $rules= array_merge($rules, $lang_rules);
            }

            $rules['why_us_image'] = 'required';
        }
        if ($this->page->id == 2){
            foreach (languages() as $lang) {
                $lang_rules=[
                    'about_us_overview_title_'.$lang->local=> 'required|min:3|max:100',
                    'about_us_overview_description_'.$lang->local=> 'required|min:3',
                ];
                $rules= array_merge($rules, $lang_rules);
            }

            $rules['about_us_overview_image'] = 'required';
        }
        if ($this->page->id == 3){
            foreach (languages() as $lang) {
                $lang_rules=[
                    'about_us_social_responsibility_title_'.$lang->local=> 'required|min:3|max:100',
                    'about_us_social_responsibility_description_'.$lang->local=> 'required|min:3',
                ];
                $rules= array_merge($rules, $lang_rules);
            }

            $rules['about_us_social_responsibility_image'] = 'required';
        }
        if ($this->page->id == 4){
            foreach (languages() as $lang) {
                $lang_rules=[
                    'about_us_company_strategy_title_'.$lang->local=> 'required|min:3|max:100',
                    'about_us_company_strategy_description_'.$lang->local=> 'required|min:3',
                ];
                $rules= array_merge($rules, $lang_rules);
            }

            $rules['about_us_company_strategy_image'] = 'required';
        }
        if ($this->page->id == 5){
            foreach (languages() as $lang) {
                $lang_rules=[
                    'vision_title_'.$lang->local=> 'required|min:3|max:100',
                    'message_title_'.$lang->local=> 'required|min:3|max:100',
                    'values_title_'.$lang->local=> 'required|min:3|max:100',
                    'vision_description_'.$lang->local=> 'required|min:3',
                    'message_description_'.$lang->local=> 'required|min:3',
                    'values_description_'.$lang->local=> 'required|min:3',
                ];
                $rules= array_merge($rules, $lang_rules);
            }

            $rules['vision_icon'] = 'required';
            $rules['message_icon'] = 'required';
            $rules['values_icon'] = 'required';
        }
        if ($this->page->id == 6){
            foreach (languages() as $lang) {
                $lang_rules=[
                    'culture_values_title_'.$lang->local=> 'required|min:3|max:100',
                    'culture_values_description_'.$lang->local=> 'required|min:3',
                ];
                $rules= array_merge($rules, $lang_rules);
            }

            $rules['culture_values_image'] = 'required';
        }
        if ($this->page->id == 7){
            foreach (languages() as $lang) {
                $lang_rules=[
                    'performance_development_title_'.$lang->local=> 'required|min:3|max:100',
                    'performance_development_description_'.$lang->local=> 'required|min:3',
                ];
                $rules= array_merge($rules, $lang_rules);
            }

            $rules['performance_development_image'] = 'required';
        }

        return $rules;
    }

    /**
     * Get custom attributes for validator errors.
     *
     * @return array
     */
    public function attributes()
    {
        $field = metaFields('pages');
        $local = getCurrentLocale();

        $attributes=[
            'why_us_image' => $field[$local]['why_us_image'] ?? __('pages.why_us_image'),
            'about_us_overview_image' => $field[$local]['about_us_overview_image'] ?? __('pages.about_us_overview_image'),
            'about_us_social_responsibility_image' => $field[$local]['about_us_social_responsibility_image'] ?? __('pages.about_us_social_responsibility_image'),
            'about_us_company_strategy_image' => $field[$local]['about_us_company_strategy_image'] ?? __('pages.about_us_company_strategy_image'),
            'culture_values_image' => $field[$local]['culture_values_image'] ?? __('pages.culture_values_image'),
            'performance_development_image' => $field[$local]['performance_development_image'] ?? __('pages.performance_development_image'),
            'vision_icon' => $field[$local]['vision_icon'] ?? __('pages.vision_icon'),
            'message_icon' => $field[$local]['message_icon'] ?? __('pages.message_icon'),
            'values_icon' => $field[$local]['values_icon'] ?? __('pages.values_icon'),
            'why_us_experience_years' => $field[$local]['why_us_experience_years'] ?? __('pages.why_us_experience_years'),
        ];
        foreach (languages() as $lang) {
            $lang_attributes=[
                'why_us_title_'.$lang->local=> $field[$lang->local]['why_us_title'] ?? __('pages.why_us_title'),
                'why_us_experience_years_title_'.$lang->local=>  $field[$lang->local]['why_us_experience_years_title'] ?? __('pages.why_us_experience_years_title'),
                'about_us_overview_title_'.$lang->local=> $field[$lang->local]['about_us_overview_title'] ?? __('pages.about_us_overview_title'),
                'about_us_social_responsibility_title_'.$lang->local=> $field[$lang->local]['about_us_social_responsibility_title'] ?? __('pages.about_us_social_responsibility_title'),
                'about_us_company_strategy_title_'.$lang->local=> $field[$lang->local]['about_us_company_strategy_title'] ?? __('pages.about_us_company_strategy_title'),
                'vision_title_'.$lang->local=> $field[$lang->local]['vision_title'] ?? __('pages.vision_title'),
                'message_title_'.$lang->local=> $field[$lang->local]['message_title'] ?? __('pages.message_title'),
                'values_title_'.$lang->local=> $field[$lang->local]['values_title'] ?? __('pages.values_title'),
                'culture_values_title_'.$lang->local=> $field[$lang->local]['culture_values_title'] ?? __('pages.culture_values_title'),
                'performance_development_title_'.$lang->local=> $field[$lang->local]['performance_development_title'] ?? __('pages.performance_development_title'),
                'why_us_description_'.$lang->local=> $field[$lang->local]['why_us_description'] ?? __('pages.why_us_description'),
                'about_us_overview_description_'.$lang->local=> $field[$lang->local]['about_us_overview_description'] ?? __('pages.about_us_overview_description'),
                'about_us_social_responsibility_description_'.$lang->local=> $field[$lang->local]['about_us_social_responsibility_description'] ?? __('pages.about_us_social_responsibility_description'),
                'about_us_company_strategy_description_'.$lang->local=> $field[$lang->local]['about_us_company_strategy_description'] ?? __('pages.about_us_company_strategy_description'),
                'vision_description_'.$lang->local=> $field[$lang->local]['vision_description'] ?? __('pages.vision_description'),
                'message_description_'.$lang->local=> $field[$lang->local]['message_description'] ?? __('pages.message_description'),
                'values_description_'.$lang->local=> $field[$lang->local]['values_description'] ?? __('pages.values_description'),
                'culture_values_description_'.$lang->local=> $field[$lang->local]['culture_values_description'] ?? __('pages.culture_values_description'),
                'performance_development_description_'.$lang->local=> $field[$lang->local]['performance_development_description'] ?? __('pages.performance_development_description'),
            ];
            $attributes= array_merge($attributes, $lang_attributes);
        }
        return $attributes;
    }
    /**
     * Handle a failed validation attempt.
     *
     * Override the parent method action if the request is for AJAX
     *
     * @param Validator $validator
     * @return void
     *
     * @throws ValidationException
     */
    protected function failedValidation(Validator $validator)
    {
        // Change response attitude if the request done via Ajax requests like API requests
        if(Request::wantsJson()){
            $errors = (new ValidationException($validator))->errors();
            throw new HttpResponseException(response()->json(['errors' => $errors], 422));
        } else {
            // Do the original action of the Form request class
            parent::failedValidation($validator);
        }
    }
}
