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

    /**
     * Get the validation rules that apply to the request.
     *
     * @return array
     */
    public function rules()
    {
        $rules = [
            'package_type' => 'required|in:u,ut,uta',
            'package_date' => 'required|date',
            'price_per_person' => 'required|not_in:0|gt:0',
            'status' => 'nullable|in:0,1',
            'departure_city' => 'required_if:package_type,ut,uta',
            'arrival_city' => 'required_if:package_type,ut,uta',
//            'point_address.*' => 'required_if:package_type,ut,uta',
//            'lat' => 'required_if:package_type,ut,uta|array',
//            'lng' => 'required_if:package_type,ut,uta|array',
        ];

        if (in_array(request('package_type'), ['ut', 'uta'])){

            if (request('point_address') ){
                foreach (request('point_address') as $key => $address) {
                    if (!$address){
                        $rules["all_point_address"] = 'required';
                    }
                }
            }
            if (request('lat') ){
                foreach (request('lat') as $key => $address) {
                    if (!$address){
                        $rules["all_lat"] = 'required';
                    }
                }
            }
            if (request('lng') ){
                foreach (request('lng') as $key => $address) {
                    if (!$address){
                        $rules["all_lng"] = 'required';
                    }
                }
            }

        }


        foreach (languages() as $lang) {
            $lang_rules=[
                'name_'.$lang->local=> 'required|min:3|max:100',
                'description_'.$lang->local=> 'required|min:3',
            ];
            $rules= array_merge($rules, $lang_rules);
        }


        $rules['house_type'] = 'required_if:package_type,uta';
        $rules['image'] = 'required';


//        dd(request('added_service_price'));

//        dd(request('room_type'), request('room_type_price'));
//        dd($rules);
        return $rules;
    }

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

        $field = metaFields('packages');
        $local = getCurrentLocale();
        $attributes=[
            'package_type' => $field[$local]['package_type'] ?? __('packages.package_type'),
            'package_date' => $field[$local]['package_date'] ?? __('packages.package_date'),
            'price_per_person' => $field[$local]['price_per_person'] ?? __('packages.price_per_person'),
            'status' => $field[$local]['status'] ?? __('packages.status'),
            'departure_city' => $field[$local]['departure_city'] ?? __('packages.departure_city'),
            'arrival_city' => $field[$local]['arrival_city'] ?? __('packages.arrival_city'),
            'point_address' => $field[$local]['point_address'] ?? __('packages.point_address'),
            'all_point_address' => $field[$local]['point_address'] ?? __('packages.point_address'),
            'lat' => $field[$local]['lat'] ?? __('packages.lat'),
            'all_lat' => $field[$local]['lat'] ?? __('packages.lat'),
            'lng' => $field[$local]['lng'] ?? __('packages.lng'),
            'all_lng' => $field[$local]['lng'] ?? __('packages.lng'),
            'house_type' => $field[$local]['house_type'] ?? __('packages.house_type'),
        ];

        foreach (languages() as $lang) {
            $lang_attributes=[
                'name_'.$lang->local=> $field[$lang->local]['name'] ?? __('packages.name'),
                'description_'.$lang->local=> $field[$lang->local]['description'] ?? __('packages.description'),
            ];
            $attributes= array_merge($attributes, $lang_attributes);
        }
        return $attributes;
    }

    public function messages()
    {

        $messages = [
            'departure_city.required_if' => __('packages.departure_city_required_if'),
            'arrival_city.required_if' => __('packages.arrival_city_required_if'),
            'point_address.required_if' => __('packages.point_address_required_if'),
            'house_type.required_if' => __('packages.house_type_required_if'),
            'lat.required_if' => __('packages.lat_required_if'),
            'lng.required_if' => __('packages.lng_required_if'),
        ];

        foreach (request('point_address') as $key => $point) {
            $messages["point_address.$key.required_if"] = __('packages.point_address_required_if');
        }
        foreach (request('lat') as $key => $price) {
            $messages["lat.$key.required_if"] = __('packages.lat_required_if');
        }
        foreach (request('lng') as $key => $price) {
            $messages["lng.$key.required_if"] = __('packages.lng_required_if');
        }
        return $messages;
    }


    /**
     * 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);
	    }
    }
}
