<?php

namespace App\Http\Requests;

use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Http\Request;
use Illuminate\Http\Exceptions\HttpResponseException;
use Illuminate\Contracts\Validation\Validator;
use Illuminate\Validation\Rule;
use App\Models\DailyAccidentSettings;

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

    public function getDailyAccidentSettings()
    {
        $temp = [];
        foreach (DailyAccidentSettings::where('type', 'accident_type')->get() as $key => $value) {
            $temp[] = $value->key;
        }

        return $temp;
    }

    /**
     * Get the validation rules that apply to the request.
     *
     * @return array
     */
    public function rules(Request $request)
    {
        $rules['province_id'] = 'required';
        $rules['axis_name'] = 'required';
        $rules['axis_type'] = 'required';
        $rules['axis_start'] = 'required';
        $rules['axis_end'] = 'required';
        $rules['distance_axis_start'] = 'required';
        $rules['local_name'] = 'required';
        $rules['lat'] = 'required';
        $rules['lng'] = 'required';
        $rules['way_geometri'] = 'required';
        $rules['police_serial'] = 'required';
        $rules['avarage_speed'] = 'required';
        $rules['legal_speed'] = 'required';
        $rules['accident_date'] = 'required';
        $rules['accident_time'] = 'required';
        $rules['day_of_the_week'] = 'required';
        $rules['direction'] = 'required';
        $rules['weather_condition'] = 'required';
        $rules['collision_type'] = 'required';
        $rules['accident_reason_dor_monaseb_dar_qus'] = 'required';
        $rules['accident_reason_saraziri_tond'] = 'required';
        $rules['accident_reason_nabod_dar_khat_sebqat_dar_sarbalaye'] = 'required';
        $rules['accident_reason_mantaqe_ba_rizesh_az_divar'] = 'required';
        $rules['accident_reason_mantaqe_ba_afzayesh_nagahani'] = 'required';
        $rules['accident_reason_kahesh_na_monaseb_tedad'] = 'required';
        $rules['accident_reason_mantaqe_ba_obor_heyvanat_ahli'] = 'required';
        $rules['accident_reason_mantaqe_ba_tadakhol_terafiki'] = 'required';
        $rules['accident_reason_arze_kam_savare_ro'] = 'required';
        $rules['accident_reason_mantaqe_meh_gir'] = 'required';
        $rules['accident_reason_adam_roshanaye_zarori_dar_noqte'] = 'required';
        $rules['accident_reason_tabdil_na_monaseb_noe_rah'] = 'required';
        $rules['accident_reason_oftadegi_dar_labe_asphalt'] = 'required';
        $rules['accident_reason_naqs_dar_ashkarsazi_navahi'] = 'required';
        $rules['accident_reason_mahal_na_monaseb_dastrasi'] = 'required';
        $rules['accident_reason_roye_na_monaseb'] = 'required';
        $rules['accident_reason_naqs_dar_ashkar_sazi_shrayet_khas'] = 'required';
        $rules['accident_reason_hendese_na_monaseb_vorodi_va_khoroji'] = 'required';
        $rules['accident_reason_adam_rayat_fasele_tooli'] = 'required';
        $rules['accident_reason_naqs_dar_alayem_ekhtari_va_entezami'] = 'required';
        $rules['accident_reason_qus_ofoqi_dar_saraziri_tond'] = 'required';
        $rules['accident_reason_adam_rayat_obor'] = 'required';
        $rules['accident_reason_naqs_dar_alayem_pish_agahi'] = 'required';
        $rules['accident_reason_qarar_gire_qus_ofoqi'] = 'required';
        $rules['accident_reason_adam_rayat_sebghat_mojaz'] = 'required';
        $rules['accident_reason_did_na_kafi'] = 'required';
        $rules['accident_reason_qus_tond_qaem'] = 'required';
        $rules['accident_reason_adam_rayat_sorat_mojaz'] = 'required';
        $rules['accident_reason_ebham_dar_masir'] = 'required';
        $rules['accident_reason_qus_tond_ofoghi'] = 'required';

        $rules['accident_type'] = ['required', Rule::in($this->getDailyAccidentSettings())];
       
        // "first_image":null,
        // "second_image":null,
        // "police_file":null,
        
        // $rules['number_of_injured']  = ['required_if:accident_type,2'];

        $rules['number_of_injured']  = ['required_if:accident_type,2'];
        $rules['number_of_died']  = ['required_if:accident_type,3'];
        $rules['increase_damage_reasons_feqdan_jodakonnande_dar_miyane_rah']  = ['required_if:accident_type,3'];
        $rules['increase_damage_reasons_naqs_ya_adam_kefayat_hefaze_miyani']  = ['required_if:accident_type,3'];
        $rules['increase_damage_reasons_naqs_ya_adam_kefayat_narde_pol']  = ['required_if:accident_type,3'];
        $rules['increase_damage_reasons_naqs_ya_adam_kefayat']  = ['required_if:accident_type,3'];
        $rules['increase_damage_reasons_naqs_dar_naheye_enteqali']  = ['required_if:accident_type,3'];
        $rules['increase_damage_reasons_mane_solb_bedone_hefaz']  = ['required_if:accident_type,3'];
        $rules['increase_damage_reasons_sar_azad_hefaz']  = ['required_if:accident_type,3'];
        $rules['increase_damage_reasons_shirvani_tond_ya_partgah']  = ['required_if:accident_type,3'];

        return $rules;
    }

    public function attributes()
    {
        return [
            'province_id' => 'اداره کل/استان',
            'axis_name' => 'نام محور',
            'axis_type' => 'نوع راه',
            'axis_start' => 'ابتدای محور',
            'axis_end' => 'انتهای محور',
            'distance_axis_start' => 'فاصله از محور',
            'local_name' => 'نام محلی تصادف',
            'police_file' => 'شماره سریال کروکی',
            'date' => 'تاریخ تصادف',
            'week_of_day' => 'روز هفته',
            'hour' => 'ساعت',
            'direction' => 'جهت حرکت',
            'type' => 'نوع تصادف',
            'number_of_injured' => 'تعداد مجروح شده',
            'number_of_died' => 'تعداد متوفی',
            'damage' => 'خسارت وارده',
            'weather_condition' => 'شرایط رویه راه و جوی',
            'collision_type' => 'نوع برخورد',
            
            'national_credit' => 'عوامل درگیر در تصادف',
            'follower_person' => 'مقام پیگیری کننده',
            'follower_person_fa' => 'مقام پیگیری کننده',
        ];
    }

    protected function failedValidation(Validator $validator)
    {
        throw new HttpResponseException(response()->json([
            'erorr' => $validator->errors()
        ], 400));
    }
}
