<?php
namespace Leadlion\AutogedalWarehouses\Model\Rule\Condition;

use Magento\Rule\Model\Condition\AbstractCondition;
use Magento\Catalog\Model\ProductFactory;

class GreutateAttribute extends AbstractCondition
{
    protected $_productFactory;

    public function __construct(
        \Magento\Rule\Model\Condition\Context $context,
        ProductFactory $productFactory,
        array $data = []
    ) {
        $this->_productFactory = $productFactory;
        parent::__construct($context, $data);
    }

    public function loadAttributeOptions()
    {
        $attributes = [
            'greutate' => __('Greutate')
        ];
        $this->setAttributeOption($attributes);
        return $this;
    }

    public function getInputType()
    {
        return 'numeric';
    }

    public function getOperatorOptions()
    {
        return [
            '=='  => __('is equal to'),
            '!='  => __('is not equal to'),
            '>='  => __('is greater than or equal to'),
            '<='  => __('is less than or equal to'),
            '>'   => __('is greater than'),
            '<'   => __('is less than'),
        ];
    }

    public function validate(\Magento\Framework\Model\AbstractModel $model)
    {
        $value = $model->getData($this->getAttribute());

        preg_match('/\d+(\.\d+)?/', $value, $matches);
        $numericValue = isset($matches[0]) ? (float)$matches[0] : 0;

        return $this->validateAttribute($numericValue);
    }
}
