/* {{{ MongoCollection::setReadPreference(int read_preference [, array tags ])
 * Sets a read preference to be used for all read queries.*/
PHP_METHOD(MongoCollection, setReadPreference)
{
	long read_preference;
	mongo_collection *c;
	HashTable  *tags = NULL;

	if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "l|H", &read_preference, &tags) == FAILURE) {
		return;
	}

	PHP_MONGO_GET_COLLECTION(getThis());

	if (read_preference >= MONGO_RP_FIRST && read_preference <= MONGO_RP_LAST) { 
		c->read_pref.type = read_preference;
	} else {
		php_error_docref(NULL TSRMLS_CC, E_WARNING, "The value %ld is not valid as read preference type", read_preference);
		RETVAL_FALSE;
	}
	if (tags) {
		if (read_preference == MONGO_RP_PRIMARY) {
			php_error_docref(NULL TSRMLS_CC, E_WARNING, "You can't use read preference tags with a read preference of PRIMARY");
			RETURN_FALSE;
		}

		if (!php_mongo_use_tagsets(&c->read_pref, tags TSRMLS_CC)) {
			RETURN_FALSE;
		}
	}
	RETURN_TRUE;
}
PHP_METHOD(MongoCollection, getReadPreference)
{
	mongo_collection *c;
	PHP_MONGO_GET_COLLECTION(getThis());

	array_init(return_value);
	add_assoc_long(return_value, "type", c->read_pref.type);
	add_assoc_string(return_value, "type_string", mongo_read_preference_type_to_name(c->read_pref.type), 1);
	php_mongo_add_tagsets(return_value, &c->read_pref);
}
PHP_METHOD(MongoCollection, setSlaveOkay) {
  zend_bool slave_okay = 1;
  mongo_collection *c;

  if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|b", &slave_okay) == FAILURE) {
    return;
  }

  PHP_MONGO_GET_COLLECTION(getThis());

  RETVAL_BOOL(c->slave_okay);
  c->slave_okay = slave_okay;
}
PHP_METHOD(MongoCollection, setSlaveOkay)
{
	zend_bool slave_okay = 1;
	mongo_collection *c;

	if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|b", &slave_okay) == FAILURE) {
		return;
	}

	PHP_MONGO_GET_COLLECTION(getThis());

	RETVAL_BOOL(c->read_pref.type != MONGO_RP_PRIMARY);
	c->read_pref.type = slave_okay ? MONGO_RP_SECONDARY_PREFERRED : MONGO_RP_PRIMARY;
}
PHP_METHOD(MongoCollection, drop) {
  zval *data;
  mongo_collection *c;

  PHP_MONGO_GET_COLLECTION(getThis());

  MAKE_STD_ZVAL(data);
  array_init(data);
  add_assoc_zval(data, "drop", c->name);
  zval_add_ref(&c->name);

  MONGO_CMD(return_value, c->parent);

  zval_ptr_dtor(&data);
}
PHP_METHOD(MongoCollection, validate) {
  zval *data;
  zend_bool scan_data = 0;
  mongo_collection *c;

  if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|b", &scan_data) == FAILURE) {
    return;
  }

  PHP_MONGO_GET_COLLECTION(getThis());

  MAKE_STD_ZVAL(data);
  array_init(data);
  add_assoc_string(data, "validate", Z_STRVAL_P(c->name), 1);
  add_assoc_bool(data, "full", scan_data);

  MONGO_CMD(return_value, c->parent);

  zval_ptr_dtor(&data);
}
PHP_METHOD(MongoCollection, getSlaveOkay)
{
	mongo_collection *c;
	PHP_MONGO_GET_COLLECTION(getThis());
	RETURN_BOOL(c->read_pref.type != MONGO_RP_PRIMARY);
}
PHP_METHOD(MongoCollection, getName) {
  mongo_collection *c;
  PHP_MONGO_GET_COLLECTION(getThis());
  RETURN_ZVAL(c->name, 1, 0);
}
PHP_METHOD(MongoCollection, __toString) {
  mongo_collection *c;
  PHP_MONGO_GET_COLLECTION(getThis());
  RETURN_ZVAL(c->ns, 1, 0);
}
PHP_METHOD(MongoCollection, getSlaveOkay) {
  mongo_collection *c;
  PHP_MONGO_GET_COLLECTION(getThis());
  RETURN_BOOL(c->slave_okay);
}