<?php

namespace App\Core\Validator;


use Illuminate\Support\MessageBag;
use Illuminate\Validation\Factory;
use Illuminate\Contracts\Container\Container;
use Illuminate\Contracts\Translation\Translator;


class BaseValidator extends Factory
{

    private $keyMapping;

    public function __construct(Translator $translator, Container $container = null)
    {
        parent::__construct($translator, $container);
        $this->keyMapping = [
            'phone' => "enw-input-mobile_phone",
            'photo' => "enw-input-photo",
            'room_rate_id' => "enw-input-rate_select",
            'room_id' => "enw-input-room",
            'description' => "enw-input-description",
            'included_occupancy' => "enw-input-included_occupancy",
            'max_room_occupancy' => "enw-input-max_room_occupancy",
        ];

    }

    public function messages()
    {
        $messages = [];
        $rules = $this->rules();
        foreach ($rules as $key => $rule) {
            $ruleArray = explode('|', $rule);
            $langKey = isset($this->keyMapping[$key]) ? $this->keyMapping[$key] : $key;
            foreach ($ruleArray as $ruleItem) {
                $ruleArray = explode(':', $ruleItem) ;
                $rule = $ruleArray[0] ;
                $value = isset($ruleArray[1]) ? $ruleArray[1] : null ;
                switch ($rule) {
                    case 'required':
                        $messages[$key . '.' . $rule] = __('error-input-required', ['input' => __($langKey)]);
                        break;
                    case 'required_with':
                        $messages[$key . '.' . $rule] = __('error-input-required', ['input' => __($langKey)]);
                        break;
                    case 'required_without':
                        $messages[$key . '.' . $rule] = __('error-input-required', ['input' => __($langKey)]);
                        break;
                    case 'numeric':
                        $messages[$key . '.' . $rule] = __('error-input-numeric', ['input' => __($langKey)]);
                        break;
                    case 'array':
                        $messages[$key . '.' . $rule] = __('error-input-array', ['input' => __($langKey)]);
                        break;
                    case 'date':
                        $messages[$key . '.' . $rule] = __('error-input-date', ['input' => __($langKey)]);
                        break;
                    case 'email':
                        $messages[$key . '.' . $rule] = __('error-input-email', ['input' => __($langKey)]);
                        break;
                    case 'integer':
                        $messages[$key . '.' . $rule] = __('error-input-integer', ['input' => __($langKey)]);
                        break;
                    case 'max':
                        $messages[$key . '.' . $rule] = __('error-input-max', ['input' => __($langKey), 'max' => $value]);
                        break;
                    case 'min':
                        $messages[$key . '.' . $rule] = __('error-input-min', ['input' => __($langKey), 'min' => $value]);
                        break;
                    case 'string':
                        $messages[$key . '.' . $rule] = __('error-input-string', ['input' => __($langKey)]);
                        break;
                    case 'boolean':
                        $messages[$key . '.' . $rule] = __('error-input-boolean', ['input' => __($langKey)]);
                        break;
                    case 'date_format':
                        $messages[$key . '.' . $rule] = __('error-input-date-format', ['input' => __($langKey), 'format' => $value] );
                        break;
                    case 'in':
                        $messages[$key . '.' . $rule] = __('error-input-in-array', ['input' => __($langKey), 'value' => $value]);
                        break;
                    case 'image':
                        $messages[$key . '.' . $rule] = __('error-input-image', ['input' => __($langKey)]);
                        break;
                    case 'mimes':
                        $messages[$key . '.' . $rule] = __('error-input-mimes', ['input' => __($langKey)]);
                        break;
                    case 'confirmed':
                        $messages[$key . '.' . $rule] = __('error-input-confirmed', ['input' => __($langKey)]);
                        break;
                    case 'accepted':
                        $messages[$key . '.' . $rule] = __('error-input-accepted', ['input' => __($langKey)]);
                        break;
                    case 'present':
                        $messages[$key . '.' . $rule] = __('error-input-present', ['input' => __($langKey)]);
                        break;
                    default :
                        $messages[$key . '.' . $rule] = __('error-input-unknown-error', ['input' => __($langKey), 'rule' => $ruleItem]);
                        break;
                }
            }
        }
        return $messages;
    }
}
