<?php
namespace App\Http\Requests\Admin;

use Illuminate\Foundation\Http\FormRequest;

class UpdateResidentialComplexesRequest extends FormRequest
{
    /**
     * Determine if the user is authorized to make this request.
     *
     * @return bool
     */
    public function authorize()
    {
        return true;
    }

    /**
     * Get the validation rules that apply to the request.
     *
     * @return array
     */
    public function rules()
    {
        return [
            'name' => 'max:191|required',
            'code' => 'max:191|required|unique:residential_complexes,code,' . $this->route('residential_complex'),
            'developer_id' => 'nullable',
            'site_link' => 'max:191',
            'meta_title' => 'max:191',
            'meta_keywords' => 'max:1000',
            'meta_description' => 'max:5000',
            'seo_text' => 'max:5000',
            'min_price' => 'nullable|max:191',
            'main_page_priority' => 'numeric|min:0|max:40|nullable',
            'res_com_page_priority' => 'numeric|min:0|max:40|nullable',
            'date_putting_into_operation' => 'nullable|date_format:'.config('app.date_year_format'),
            'building_count' => 'max:9999|required|numeric',
            'room_count' => 'max:5000|nullable|numeric',
            'room_count_date' => 'nullable|date_format:'.config('app.date_format'),
            'count_of_room_per_flat.*' => 'exists:room_counts,id',
            'room_type.*' => 'exists:room_types,id',
            'city_id' => 'required',
            'street_number' => 'max:192',
            'class.*' => 'exists:class_housings,id',
            'stage_of_construction.*' => 'exists:stage_of_constructions,id',
            'heating.*' => 'exists:heatings,id',
            'technology_building.*' => 'exists:technology_buildings,id',
            'other_option.*' => 'exists:other_options,id',
            'icon.*' => 'exists:residental_complex_icons,id',
            'recomended_residental_complex_id.*' => 'exists:residential_complexes,id',
            'latitude' => 'numeric|nullable',
            'longitude' => 'numeric|nullable',
            'residential_complex_characters.*.type' => 'max:192|required',
            'residential_complex_characters.*.value' => 'max:192|required',
        ];
    }

    /**
     * Configure the validator instance.
     *
     * @param  \Illuminate\Validation\Validator $validator
     * @return void
     */
    public function withValidator($validator)
    {
        $data = $this->all();
        $contactArray = [
            $data['is_show_whatsapp'],
            $data['is_show_skype'],
            $data['is_show_viber'],
            $data['is_show_telegram'],
            $data['is_show_presentation'],
            $data['is_show_consultation'],
        ];

        $counter = 0;
        foreach ($contactArray as $contact) {
            if($contact == 1) {
                $counter += $contact;
            }
        }

        $showError = false;
        if ($counter > 3) {
            $showError = true;
        }


        $validator->after(function ($validator) use ($showError) {
            if ($showError == true) {
                $validator->errors()->add('contact_error', 'Не може бути вибрано більше ніж 3 опції.');
            }
        });
    }

    public function messages()
    {
        return [
            'min_price.regex' =>
                'Поле "Мін. ціна за квартиру" має містити тільки цифри та 2 знаки після крапки. Приклад: 12.00',
        ];
    }
}
