<?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;

class HomeInfoRequest 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( 'homeInfos' ) )
        {
            return true;
        }
        return Auth::check();
    }

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

        foreach (languages() as $lang) {
            $lang_rules=[
                'advantage_box_1_title_'.$lang->local=> 'required|min:3|max:200',
                'advantage_box_2_title_'.$lang->local=> 'required|min:3|max:200',
                'advantage_box_3_title_'.$lang->local=> 'required|min:3|max:200',
                'advantage_box_4_title_'.$lang->local=> 'required|min:3|max:200',
                'advantage_box_5_title_'.$lang->local=> 'required|min:3|max:200',
                'advantage_box_6_title_'.$lang->local=> 'required|min:3|max:100',
                'social_service_box_1_title_'.$lang->local=> 'required|min:3|max:200',
                'social_service_box_2_title_'.$lang->local=> 'required|min:3|max:200',
                'social_service_box_3_title_'.$lang->local=> 'required|min:3|max:200',
                'social_service_box_4_title_'.$lang->local=> 'required|min:3|max:200',
                'contact_box_title_'.$lang->local=> 'required|min:3|max:200',
                'advantage_box_1_description_'.$lang->local=> 'required|min:3|max:500',
                'advantage_box_2_description_'.$lang->local=> 'required|min:3|max:500',
                'advantage_box_3_description_'.$lang->local=> 'required|min:3|max:500',
                'advantage_box_4_description_'.$lang->local=> 'required|min:3|max:500',
                'advantage_box_5_description_'.$lang->local=> 'required|min:3|max:500',
                'advantage_box_6_description_'.$lang->local=> 'required|min:3|max:500',
                'social_service_box_1_description_'.$lang->local=> 'required|min:3|max:500',
                'social_service_box_2_description_'.$lang->local=> 'required|min:3|max:500',
                'social_service_box_3_description_'.$lang->local=> 'required|min:3|max:500',
                'social_service_box_4_description_'.$lang->local=> 'required|min:3|max:500',
                'contact_box_description_'.$lang->local=> 'required|min:3|max:500',
            ];
            $rules= array_merge($rules, $lang_rules);
        }


        if (request('first_box_points')){
            foreach (languages() as $language) {
                if (in_array(null, request('first_box_points')[$language->local])){
                    $rules['all_first_box_points'] = 'required';
                }
            }
        }
        if (request('second_box_points')){
            foreach (languages() as $language) {
                if (in_array(null, request('second_box_points')[$language->local])){
                    $rules['all_second_box_points'] = 'required';
                }
            }
        }
        if (request('third_box_points')){
            foreach (languages() as $language) {
                if (in_array(null, request('third_box_points')[$language->local])){
                    $rules['all_third_box_points'] = 'required';
                }
            }
        }
        if (request('fourth_box_points')){
            foreach (languages() as $language) {
                if (in_array(null, request('fourth_box_points')[$language->local])){
                    $rules['all_fourth_box_points'] = 'required';
                }
            }
        }

        $rules['advantage_image'] = 'required';
        $rules['social_service_image'] = 'required';

        return $rules;
    }

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

        $attributes=[
            'advantage_image' => $field[$local]['advantage_image'] ?? __('homeInfos.advantage_image'),
            'social_service_image' => $field[$local]['social_service_image'] ?? __('homeInfos.social_service_image'),
            'all_first_box_points' => __('homeInfos.first_box_points'),
            'all_second_box_points' => __('homeInfos.second_box_points'),
            'all_third_box_points' => __('homeInfos.third_box_points'),
            'all_fourth_box_points' => __('homeInfos.fourth_box_points'),
        ];
        foreach (languages() as $lang) {
            $lang_attributes=[
                'advantage_box_1_title_'.$lang->local=> $field[$lang->local]['advantage_box_1_title'] ?? __('homeInfos.advantage_box_1_title'),
                'advantage_box_2_title_'.$lang->local=>  $field[$lang->local]['advantage_box_2_title'] ?? __('homeInfos.advantage_box_2_title'),
                'advantage_box_3_title_'.$lang->local=> $field[$lang->local]['advantage_box_3_title'] ?? __('homeInfos.advantage_box_3_title'),
                'advantage_box_4_title_'.$lang->local=> $field[$lang->local]['advantage_box_4_title'] ?? __('homeInfos.advantage_box_4_title'),
                'advantage_box_5_title_'.$lang->local=> $field[$lang->local]['advantage_box_5_title'] ?? __('homeInfos.advantage_box_5_title'),
                'advantage_box_6_title_'.$lang->local=> $field[$lang->local]['advantage_box_6_title'] ?? __('homeInfos.advantage_box_6_title'),
                'social_service_box_1_title_'.$lang->local=> $field[$lang->local]['social_service_box_1_title'] ?? __('homeInfos.social_service_box_1_title'),
                'social_service_box_2_title_'.$lang->local=> $field[$lang->local]['social_service_box_2_title'] ?? __('homeInfos.social_service_box_2_title'),
                'social_service_box_3_title_'.$lang->local=> $field[$lang->local]['social_service_box_3_title'] ?? __('homeInfos.social_service_box_3_title'),
                'social_service_box_4_title_'.$lang->local=> $field[$lang->local]['social_service_box_4_title'] ?? __('homeInfos.social_service_box_4_title'),
                'advantage_box_1_description_'.$lang->local=> $field[$lang->local]['advantage_box_1_description'] ?? __('homeInfos.advantage_box_1_description'),
                'advantage_box_2_description_'.$lang->local=> $field[$lang->local]['advantage_box_2_description'] ?? __('homeInfos.advantage_box_2_description'),
                'advantage_box_3_description_'.$lang->local=> $field[$lang->local]['advantage_box_3_description'] ?? __('homeInfos.advantage_box_3_description'),
                'advantage_box_4_description_'.$lang->local=> $field[$lang->local]['advantage_box_4_description'] ?? __('homeInfos.advantage_box_4_description'),
                'advantage_box_5_description_'.$lang->local=> $field[$lang->local]['advantage_box_5_description'] ?? __('homeInfos.advantage_box_5_description'),
                'advantage_box_6_description_'.$lang->local=> $field[$lang->local]['advantage_box_6_description'] ?? __('homeInfos.advantage_box_6_description'),
                'social_service_box_1_description_'.$lang->local=> $field[$lang->local]['social_service_box_1_description'] ?? __('homeInfos.social_service_box_1_description'),
                'social_service_box_2_description_'.$lang->local=> $field[$lang->local]['social_service_box_2_description'] ?? __('homeInfos.social_service_box_2_description'),
                'social_service_box_3_description_'.$lang->local=> $field[$lang->local]['social_service_box_3_description'] ?? __('homeInfos.social_service_box_3_description'),
                'social_service_box_4_description_'.$lang->local=> $field[$lang->local]['social_service_box_4_description'] ?? __('homeInfos.social_service_box_4_description'),
                'contact_box_title_'.$lang->local=> $field[$lang->local]['contact_box_title'] ?? __('homeInfos.contact_box_title'),
                'contact_box_description_'.$lang->local=> $field[$lang->local]['contact_box_description'] ?? __('homeInfos.contact_box_description'),
            ];
            $attributes= array_merge($attributes, $lang_attributes);
        }
        return $attributes;
    }

    public function messages()
    {
        return [];
    }

    /**
     * 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::ajax()){
            if ($validator->fails()){
                $errors = $validator->errors()->first();
                throw new HttpResponseException(response()->json(['errors' => $errors]));
            }

        } else {
            // Do the original action of the Form request class
            parent::failedValidation($validator);
        }
    }
}
