<?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 LandingRequest 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('landings'))
        {
            return true;
        }
        return Auth::check();
    }

    /**
     * Get the validation rules that apply to the request.
     *
     * @return array
     */
    public function rules()
    {
        $rules = [
//            'app_store_link' => 'required|url|max:400',
//            'play_store_link' => 'required|url|max:400',
        ];

        foreach (languages() as $lang) {
            $lang_rules=[
                'banner_description_'.$lang->local=> 'required|min:3|max:700',
                'sec_2_description_'.$lang->local=> 'required|min:3|max:700',
                'sec_3_box_1_description_'.$lang->local=> 'required|min:3|max:200',
                'sec_3_box_2_description_'.$lang->local=> 'required|min:3|max:200',
                'sec_3_box_3_description_'.$lang->local=> 'required|min:3|max:200',
                'sec_3_box_4_description_'.$lang->local=> 'required|min:3|max:200',
                'sec_3_title_'.$lang->local=> 'required|min:3|max:50|max:1400',
                'sec_3_description_'.$lang->local=> 'required|min:3',
                'sec_4_title_'.$lang->local=> 'required|min:3|max:50',
                'sec_4_description_'.$lang->local=> 'required|min:3|max:110',
                'sec_4_box_1_description_'.$lang->local=> 'required|min:3|max:55',
                'sec_4_box_2_description_'.$lang->local=> 'required|min:3|max:55',
                'sec_4_box_3_description_'.$lang->local=> 'required|min:3|max:55',
                'sec_4_box_4_description_'.$lang->local=> 'required|min:3|max:55',
                'sec_5_description_'.$lang->local=> 'required|min:3|max:250',
            ];
            $rules= array_merge($rules, $lang_rules);
        }
        return $rules;
    }

    /**
     * Get custom attributes for validator errors.
     *
     * @return array
     */
    public function attributes()
    {

        $field = metaFields('landings');
//        $local = getCurrentLocale();
        $attributes=[
//            'app_store_link' => $field[$local]['app_store_link'] ?? __('landings.app_store_link'),
//            'play_store_link' => $field[$local]['play_store_link'] ?? __('landings.play_store_link'),
        ];

        foreach (languages() as $lang) {
            $lang_attributes=[
                'banner_description_'.$lang->local=> $field[$lang->local]['banner_description'] ?? __('landings.banner_description'),
//                'app_store_link_'.$lang->local=> $field[$lang->local]['app_store_link'] ?? __('landings.app_store_link'),
//                'play_store_link_'.$lang->local=> $field[$lang->local]['play_store_link'] ?? __('landings.play_store_link'),
                'sec_2_description_'.$lang->local=> $field[$lang->local]['sec_2_description'] ?? __('landings.sec_2_description'),
                'sec_3_box_1_description_'.$lang->local=> $field[$lang->local]['sec_3_box_1_description'] ?? __('landings.sec_3_box_1_description'),
                'sec_3_box_2_description_'.$lang->local=> $field[$lang->local]['sec_3_box_2_description'] ?? __('landings.sec_3_box_2_description'),
                'sec_3_box_3_description_'.$lang->local=> $field[$lang->local]['sec_3_box_3_description'] ?? __('landings.sec_3_box_3_description'),
                'sec_3_box_4_description_'.$lang->local=> $field[$lang->local]['sec_3_box_4_description'] ?? __('landings.sec_3_box_4_description'),
                'sec_3_title_'.$lang->local=> $field[$lang->local]['sec_3_title'] ?? __('landings.sec_3_title'),
                'sec_3_description_'.$lang->local=> $field[$lang->local]['sec_3_description'] ?? __('landings.sec_3_description'),
                'sec_4_title_'.$lang->local=> $field[$lang->local]['sec_4_title'] ?? __('landings.sec_4_title'),
                'sec_4_description_'.$lang->local=> $field[$lang->local]['sec_4_description'] ?? __('landings.sec_4_description'),
                'sec_4_box_1_description_'.$lang->local=> $field[$lang->local]['sec_4_box_1_description'] ?? __('landings.sec_4_box_1_description'),
                'sec_4_box_2_description_'.$lang->local=> $field[$lang->local]['sec_4_box_2_description'] ?? __('landings.sec_4_box_2_description'),
                'sec_4_box_3_description_'.$lang->local=> $field[$lang->local]['sec_4_box_3_description'] ?? __('landings.sec_4_box_3_description'),
                'sec_4_box_4_description_'.$lang->local=> $field[$lang->local]['sec_4_box_4_description'] ?? __('landings.sec_4_box_4_description'),
                'sec_5_description_'.$lang->local=> $field[$lang->local]['sec_5_description'] ?? __('landings.sec_5_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::ajax()){
	        $errors = $validator->errors()->first();
	        throw new HttpResponseException(response()->json(['errors' => $errors]));
	    } else {
	    	// Do the original action of the Form request class
	    	parent::failedValidation($validator);
	    }
    }
}
