<?php

namespace ModulesGarden\HostingQuotaNotifications\App\UI\Validators;

use ModulesGarden\HostingQuotaNotifications\App\Libs\Groups\Constants;
use \ModulesGarden\HostingQuotaNotifications\Core\UI\Widget\Forms\Validators\BaseValidator;
use \ModulesGarden\HostingQuotaNotifications\App\Libs\GroupConfiguration;
use \ModulesGarden\HostingQuotaNotifications\App\Helper\Request;


/**
 * StepWithThatValue form data validator
 *
 * @author Artur Pilch <artur.pi@modulesgarden.com>
 */
class StepWithThatValue extends BaseValidator
{
    protected function validate($data, $additionalData = null)
    {
        $formData = Request::getValue('formData');
        $groupId  = Request::getValue('groupid');
        $kind     = $formData['id'];

        $result = GroupConfiguration\Manage::getConfigurationForGroupByType($kind, $groupId);

        foreach ($result as $config)
        {
            if ($config['value'] == $data && $config['unit'] == $formData['unit'])
            {
                $this->addValidationError('stepWithSuchValueAndUnitExist');
                return false;
            }

            if(($data < 0 || $data > 100) && $formData['unit'] == Constants::PERCENTAGE)
            {
                $this->addValidationError('valueForPercentage');
                return false;
            }
        }
        return true;
    }
}
