<?php

namespace App\Rules;

use Illuminate\Contracts\Validation\ImplicitRule;
use Illuminate\Foundation\Http\FormRequest;
// use Illuminate\Contracts\Validation\Rule;

class LoadedMedia implements ImplicitRule
{

    public $request;

    public function __construct(FormRequest $request)
    {

        $this->request = $request;
    }
    /**
     * Determine if the validation rule passes.
     *
     * @param  string  $attribute
     * @param  mixed  $value
     * @return bool
     */
    public function passes($attribute, $value)
    {

        if(is_array($value)){
            return true;
        }

        $total = $this->request->totalItems ?? -1;
        $removeCount = $this->request->removedItems ?? [];


        if($total === count($removeCount)){
            return false;
        }

        if($total < 0 && $value === null){
            return false;
        }

        return true;

    }

    /**
     * Get the validation error message.
     *
     * @return string
     */
    public function message()
    {
        return 'Поле медиа обязательно для заполнения!';
    }
}
