
#ifdef HAVE_CONFIG_H
#include "../../../../ext_config.h"
#endif

#include <php.h>
#include "../../../../php_ext.h"
#include "../../../../ext.h"

#include <Zend/zend_operators.h>
#include <Zend/zend_exceptions.h>
#include <Zend/zend_interfaces.h>

#include "kernel/main.h"
#include "kernel/object.h"
#include "kernel/memory.h"
#include "kernel/fcall.h"
#include "kernel/operators.h"


/**
 * This file is part of the Phalcon Framework.
 *
 * (c) Phalcon Team <team@phalcon.io>
 *
 * For the full copyright and license information, please view the LICENSE.txt
 * file that was distributed with this source code.
 *
 * Implementation of this component has been inspired by the queue-interop and
 * enqueue projects.
 *
 * @link    https://github.com/queue-interop/queue-interop
 * @license https://github.com/queue-interop/queue-interop/blob/master/LICENSE
 *
 * @link    https://github.com/php-enqueue/enqueue-dev
 * @license https://github.com/php-enqueue/enqueue-dev/blob/master/LICENSE
 */
/**
 * Sends messages to a Beanstalkd tube. Delivery delay (rounded down to whole
 * seconds) and message priority are supported natively; Beanstalkd has no
 * message expiry, so time to live is not (the default from AbstractProducer
 * rejects it).
 */
ZEPHIR_INIT_CLASS(Phalcon_Queue_Adapter_Beanstalk_BeanstalkProducer)
{
	ZEPHIR_REGISTER_CLASS_EX(Phalcon\\Queue\\Adapter\\Beanstalk, BeanstalkProducer, phalcon, queue_adapter_beanstalk_beanstalkproducer, phalcon_queue_adapter_abstractproducer_ce, phalcon_queue_adapter_beanstalk_beanstalkproducer_method_entry, 0);

	{
		zval _zc0;
		ZVAL_UNDEF(&_zc0);
		zephir_declare_typed_property(phalcon_queue_adapter_beanstalk_beanstalkproducer_ce, SL("context"), &_zc0, ZEND_ACC_PROTECTED, 0, SL("Phalcon\\Queue\\Adapter\\Beanstalk\\BeanstalkContext"));
	}

	/**
	 * Delivery delay in milliseconds, or null when not set.
	 */
	{
		zval _zc0;
		ZVAL_NULL(&_zc0);
		zephir_declare_typed_property(phalcon_queue_adapter_beanstalk_beanstalkproducer_ce, SL("deliveryDelay"), &_zc0, ZEND_ACC_PROTECTED, MAY_BE_LONG|MAY_BE_NULL, NULL, 0);
	}

	/**
	 * Job priority, or null when not set.
	 */
	{
		zval _zc0;
		ZVAL_NULL(&_zc0);
		zephir_declare_typed_property(phalcon_queue_adapter_beanstalk_beanstalkproducer_ce, SL("priority"), &_zc0, ZEND_ACC_PROTECTED, MAY_BE_LONG|MAY_BE_NULL, NULL, 0);
	}

	/**
	 * Default Beanstalkd priority (0 = most urgent).
	 *
	 * @var int
	 */
	zephir_declare_class_constant_long(phalcon_queue_adapter_beanstalk_beanstalkproducer_ce, SL("DEFAULT_PRIORITY"), 100);

	return SUCCESS;
}

PHP_METHOD(Phalcon_Queue_Adapter_Beanstalk_BeanstalkProducer, __construct)
{
	zval *context, context_sub;
	zval *this_ptr = getThis();

	ZVAL_UNDEF(&context_sub);
	static zend_string *_zephir_prop_0 = NULL;
	if (UNEXPECTED(!_zephir_prop_0)) {
		_zephir_prop_0 = zend_string_init("context", 7, 1);
	}

	ZEND_PARSE_PARAMETERS_START(1, 1)
		Z_PARAM_OBJECT_OF_CLASS(context, phalcon_queue_adapter_beanstalk_beanstalkcontext_ce)
	ZEND_PARSE_PARAMETERS_END();
	zephir_fetch_params_without_memory_grow(1, 0, &context);
	zephir_update_property_zval_cached(this_ptr, _zephir_prop_0, 1264, context);
}

PHP_METHOD(Phalcon_Queue_Adapter_Beanstalk_BeanstalkProducer, getDeliveryDelay)
{

	RETURN_MEMBER(getThis(), "deliveryDelay");
}

PHP_METHOD(Phalcon_Queue_Adapter_Beanstalk_BeanstalkProducer, getPriority)
{

	RETURN_MEMBER(getThis(), "priority");
}

PHP_METHOD(Phalcon_Queue_Adapter_Beanstalk_BeanstalkProducer, send)
{
	zephir_method_globals *ZEPHIR_METHOD_GLOBALS_PTR = NULL;
	zend_long ZEPHIR_LAST_CALL_STATUS, priority = 0, delay = 0;
	zval *destination, destination_sub, *message, message_sub, payload, _0, _1, _2, _3, _4, _5, _6, _7, _8, _9, _10, _11, _12;
	zval *this_ptr = getThis();

	ZVAL_UNDEF(&destination_sub);
	ZVAL_UNDEF(&message_sub);
	ZVAL_UNDEF(&payload);
	ZVAL_UNDEF(&_0);
	ZVAL_UNDEF(&_1);
	ZVAL_UNDEF(&_2);
	ZVAL_UNDEF(&_3);
	ZVAL_UNDEF(&_4);
	ZVAL_UNDEF(&_5);
	ZVAL_UNDEF(&_6);
	ZVAL_UNDEF(&_7);
	ZVAL_UNDEF(&_8);
	ZVAL_UNDEF(&_9);
	ZVAL_UNDEF(&_10);
	ZVAL_UNDEF(&_11);
	ZVAL_UNDEF(&_12);
	static zend_string *_zephir_prop_0 = NULL;
	static zend_string *_zephir_prop_1 = NULL;
	static zend_string *_zephir_prop_2 = NULL;
	if (UNEXPECTED(!_zephir_prop_0)) {
		_zephir_prop_0 = zend_string_init("priority", 8, 1);
	}
	if (UNEXPECTED(!_zephir_prop_1)) {
		_zephir_prop_1 = zend_string_init("deliveryDelay", 13, 1);
	}
	if (UNEXPECTED(!_zephir_prop_2)) {
		_zephir_prop_2 = zend_string_init("context", 7, 1);
	}

	ZEND_PARSE_PARAMETERS_START(2, 2)
		Z_PARAM_OBJECT_OF_CLASS(destination, phalcon_contracts_queue_destination_ce)
		Z_PARAM_OBJECT_OF_CLASS(message, phalcon_contracts_queue_message_ce)
	ZEND_PARSE_PARAMETERS_END();
	ZEPHIR_METHOD_GLOBALS_PTR = pecalloc(1, sizeof(zephir_method_globals), 0);
	zephir_memory_grow_stack(ZEPHIR_METHOD_GLOBALS_PTR, __func__);
	zephir_fetch_params(1, 2, 0, &destination, &message);
	ZEPHIR_INIT_VAR(&_0);
	ZVAL_STRING(&_0, "send to");
	ZEPHIR_CALL_CE_STATIC(NULL, phalcon_queue_adapter_queuedestinationguard_ce, "assertqueue", NULL, 0, destination, &_0);
	zephir_check_call_status();
	ZEPHIR_CALL_CE_STATIC(&payload, phalcon_queue_adapter_messageenvelope_ce, "encode", NULL, 0, message);
	zephir_check_call_status();
	ZEPHIR_INIT_VAR(&_1);
	zephir_read_property_cached(&_2, this_ptr, _zephir_prop_0, 1265, PH_NOISY_CC | PH_READONLY);
	if (Z_TYPE_P(&_2) == IS_NULL) {
		ZEPHIR_INIT_NVAR(&_1);
		ZVAL_LONG(&_1, 100);
	} else {
		zephir_memory_observe(&_3);
		zephir_read_property_cached(&_3, this_ptr, _zephir_prop_0, 1265, PH_NOISY_CC);
		ZEPHIR_INIT_NVAR(&_1);
		ZVAL_LONG(&_1, zephir_get_intval(&_3));
	}
	priority = zephir_get_numberval(&_1);
	ZEPHIR_INIT_VAR(&_4);
	zephir_read_property_cached(&_5, this_ptr, _zephir_prop_1, 1266, PH_NOISY_CC | PH_READONLY);
	if (Z_TYPE_P(&_5) == IS_NULL) {
		ZEPHIR_INIT_NVAR(&_4);
		ZVAL_LONG(&_4, 0);
	} else {
		zephir_read_property_cached(&_6, this_ptr, _zephir_prop_1, 1266, PH_NOISY_CC | PH_READONLY);
		ZEPHIR_INIT_NVAR(&_4);
		ZVAL_LONG(&_4, (int) (zephir_safe_div_zval_long(&_6, 1000)));
	}
	delay = zephir_get_numberval(&_4);
	zephir_read_property_cached(&_7, this_ptr, _zephir_prop_2, 1264, PH_NOISY_CC | PH_READONLY);
	ZEPHIR_CALL_METHOD(&_8, destination, "getqueuename", NULL, 0);
	zephir_check_call_status();
	zephir_read_property_cached(&_9, this_ptr, _zephir_prop_2, 1264, PH_NOISY_CC | PH_READONLY);
	ZEPHIR_CALL_METHOD(&_10, &_9, "getttr", NULL, 0);
	zephir_check_call_status();
	ZVAL_LONG(&_11, priority);
	ZVAL_LONG(&_12, delay);
	ZEPHIR_CALL_METHOD(NULL, &_7, "putmessage", NULL, 0, &_8, &payload, &_11, &_12, &_10);
	zephir_check_call_status();
	ZEPHIR_MM_RESTORE();
}

PHP_METHOD(Phalcon_Queue_Adapter_Beanstalk_BeanstalkProducer, setDeliveryDelay)
{
	zephir_method_globals *ZEPHIR_METHOD_GLOBALS_PTR = NULL;
	zval *deliveryDelay = NULL, deliveryDelay_sub, __$null, _0;
	zval *this_ptr = getThis();

	ZVAL_UNDEF(&deliveryDelay_sub);
	ZVAL_NULL(&__$null);
	ZVAL_UNDEF(&_0);
	static zend_string *_zephir_prop_0 = NULL;
	if (UNEXPECTED(!_zephir_prop_0)) {
		_zephir_prop_0 = zend_string_init("deliveryDelay", 13, 1);
	}

	bool is_null_true = 1;
	ZEND_PARSE_PARAMETERS_START(0, 1)
		Z_PARAM_OPTIONAL
		Z_PARAM_ZVAL_OR_NULL(deliveryDelay)
	ZEND_PARSE_PARAMETERS_END();
	ZEPHIR_METHOD_GLOBALS_PTR = pecalloc(1, sizeof(zephir_method_globals), 0);
	zephir_memory_grow_stack(ZEPHIR_METHOD_GLOBALS_PTR, __func__);
	zephir_fetch_params(1, 0, 1, &deliveryDelay);
	if (!deliveryDelay) {
		deliveryDelay = &deliveryDelay_sub;
		deliveryDelay = &__$null;
	}
	ZEPHIR_INIT_VAR(&_0);
	if (Z_TYPE_P(deliveryDelay) == IS_NULL) {
		ZEPHIR_INIT_NVAR(&_0);
		ZVAL_NULL(&_0);
	} else {
		ZEPHIR_INIT_NVAR(&_0);
		ZVAL_LONG(&_0, zephir_get_intval(deliveryDelay));
	}
	zephir_update_property_zval_cached(this_ptr, _zephir_prop_0, 1266, &_0);
	RETURN_THIS();
}

PHP_METHOD(Phalcon_Queue_Adapter_Beanstalk_BeanstalkProducer, setPriority)
{
	zephir_method_globals *ZEPHIR_METHOD_GLOBALS_PTR = NULL;
	zval *priority = NULL, priority_sub, __$null, _0;
	zval *this_ptr = getThis();

	ZVAL_UNDEF(&priority_sub);
	ZVAL_NULL(&__$null);
	ZVAL_UNDEF(&_0);
	static zend_string *_zephir_prop_0 = NULL;
	if (UNEXPECTED(!_zephir_prop_0)) {
		_zephir_prop_0 = zend_string_init("priority", 8, 1);
	}

	bool is_null_true = 1;
	ZEND_PARSE_PARAMETERS_START(0, 1)
		Z_PARAM_OPTIONAL
		Z_PARAM_ZVAL_OR_NULL(priority)
	ZEND_PARSE_PARAMETERS_END();
	ZEPHIR_METHOD_GLOBALS_PTR = pecalloc(1, sizeof(zephir_method_globals), 0);
	zephir_memory_grow_stack(ZEPHIR_METHOD_GLOBALS_PTR, __func__);
	zephir_fetch_params(1, 0, 1, &priority);
	if (!priority) {
		priority = &priority_sub;
		priority = &__$null;
	}
	ZEPHIR_INIT_VAR(&_0);
	if (Z_TYPE_P(priority) == IS_NULL) {
		ZEPHIR_INIT_NVAR(&_0);
		ZVAL_NULL(&_0);
	} else {
		ZEPHIR_INIT_NVAR(&_0);
		ZVAL_LONG(&_0, zephir_get_intval(priority));
	}
	zephir_update_property_zval_cached(this_ptr, _zephir_prop_0, 1265, &_0);
	RETURN_THIS();
}

