<?php

namespace App\Http\Requests;

use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Contracts\Validation\Validator;

class FileUploadRequest extends FormRequest
{
    protected $stopOnFirstFailure = false;

    public function rules(): array
    {
        $uploadableTypes = uploadableTypes();
        $convertibleTypes = convertibleTypes();
        $maxSize = config('upload.MAX_FILE_SIZE');

        return [
            'file' => [
                'required', 
                'file', 
                "mimes:{$uploadableTypes}", 
                "max: {$maxSize}",
            ],
            'type' => [
                'nullable', 
                "in:{$convertibleTypes}", 
            ],
            'compress' => [
                'nullable', 
                'numeric',
                'min:0', 
                'max:100'
            ],
            'size.width' => [
                'nullable', 
                'integer', 
                'max:3840'
            ],
            'size.height' => [
                'nullable', 
                'integer', 
                'max:2160'
            ],
            'rotation-reflect' => [
                'nullable', 
                'in:reflect-h,reflect-v,rotate-l-90,rotate-l-180,'
                    . 'rotate-l-270,rotate-r-90,rotate-r-180,rotate-r-270'
            ],
            'watermark' => [
                'nullable', 
                'string', 
                'max:20'
            ],
            'preferences.brightness' => [
                'nullable', 
                'integer', 
                'min:-100', 
                'max:100'
            ],
            'preferences.contrast' => [
                'nullable', 
                'integer', 
                'min:-100', 
                'max:100'
            ],
            'preferences.gamma' => [
                'nullable', 
                'numeric',
                'between:0.8,2.3',
            ],
            'preferences.red' => [
                'nullable', 
                'integer', 
                'min:-100', 
                'max:100'
            ],
            'preferences.green' => [
                'nullable', 
                'integer', 
                'min:-100', 
                'max:100'
            ],
            'preferences.blue' => [
                'nullable', 
                'integer', 
                'min:-100', 
                'max:100'
            ],
            'effects.grayscale' => [
                'nullable', 
                'in:1'
            ],
            'effects.blur' => [
                'nullable', 
                'in:1'
            ],
            'effects.invert' => [
                'nullable', 
                'in:1'
            ],
            'effects.pixelation' => [
                'nullable', 
                'in:1'
            ],
            'effects.reduce' => [
                'nullable', 
                'in:1'
            ],
            'auto-remove' => [
                'nullable', 
                'in:5m,30m,1h,6h,12h,1d,1w,1m'
            ],
            'password' => [
                'nullable', 
                'min:3', 
                'max:15'
            ],
            'exif.basic' => [
                'nullable', 
                'in:1'
            ],
            'exif.deep' => [
                'nullable', 
                'in:1'
            ],
        ];
    }

    public function messages(): array
    {
        $maxSize = config('upload.MAX_FILE_SIZE');

        return [
            'file.required' => __("lang.file_not_selected"),
            'file.file' => __("lang.file_not_selected_non_files"),
            'file.mimes' => __("lang.file_cannot_upload"),
            'file.max' => __("lang.file_max_size",['var' => formatKiloBytes($maxSize)]),

            'type.mimes' => __("lang.file_cannot_convert"),

            'compress.min' => __("lang.compress_min"),
            'compress.max' => __("lang.compress_max"),

            'size.width.integer' => __("lang.width_format_incorrect"),
            'size.width.max' => __("lang.width_large"),

            'size.height.integer' => __("lang.height_format_incorrect"),
            'size.height.max' => __("lang.height_large"),

            'rotation-reflect.in' => __("lang.rotation_reflect_format_incorrect"),

            'watermark.string' => __("lang.watermark_format_incorrect"),
            'watermark.max' => __("lang.watermark_max"),

            'preferences.brightness.integer' => __("lang.brightness_format_incorrect"),
            'preferences.brightness.min' => __("lang.brightness_min"),
            'preferences.brightness.max' => __("lang.brightness_max"),

            'preferences.contrast.integer' => __("lang.contrast_format_incorrect"),
            'preferences.contrast.min' => __("lang.contrast_min"),
            'preferences.contrast.max' => __("lang.contrast_max"),

            'preferences.gamma.integer' => __("lang.gamma_format_incorrect"),
            'preferences.gamma.min' => __("lang.gamma_min"),
            'preferences.gamma.max' => __("lang.gamma_max"),

            'preferences.red.integer' => __("lang.red_format_incorrect"),
            'preferences.red.min' => __("lang.red_min"),
            'preferences.red.max' => __("lang.red_max"),

            'preferences.green.integer' => __("lang.green_format_incorrect"),
            'preferences.green.min' => __("lang.green_min"),
            'preferences.green.max' => __("lang.green_max"),

            'preferences.blue.integer' => __("lang.blue_format_incorrect"),
            'preferences.blue.min' => __("lang.blue_min"),
            'preferences.blue.max' => __("lang.blue_max"),

            'effects.grayscale.in' => __("lang.grayscale_format_incorrect"),

            'effects.blur.in' => __("lang.blur_format_incorrect"),

            'effects.invert.in' => __("lang.invert_colors_format_incorrect"),

            'effects.pixelation.in' => __("lang.pixelation_format_incorrect"),

            'effects.reduce.in' => __("lang.reduce_color_format_incorrect"),

            'auto-remove.in' => __("lang.auto_remove_format_incorrect"),

            'password.min' => __("lang.password_min"),
            'password.max' => __("lang.password_max"),

            'exif.basic.in' => __("lang.exif_format_incorrect"),

            'exif.deep.in' => __("lang.exif_deep_format_incorrect"),
        ];
    }

    public function failedValidation(Validator $validator)
    {
        return $validator->errors()->messages();
    }

    public function getValidator()
    {
        return $this->getValidatorInstance();
    }
}
