/**
 * Create new output component
 */
static struct rayo_component *create_output_component(struct rayo_actor *actor, const char *type, iks *output, const char *client_jid)
{
	switch_memory_pool_t *pool;
	struct output_component *output_component = NULL;

	switch_core_new_memory_pool(&pool);
	output_component = switch_core_alloc(pool, sizeof(*output_component));
	rayo_component_init((struct rayo_component *)output_component, pool, type, "output", NULL, actor, client_jid);

	output_component->document = iks_copy(output);
	output_component->repeat_interval = iks_find_int_attrib(output, "repeat-interval");
	output_component->repeat_times = iks_find_int_attrib(output, "repeat-times");
	output_component->max_time = iks_find_int_attrib(output, "max-time");
	output_component->start_paused = iks_find_bool_attrib(output, "start-paused");

	return (struct rayo_component *)output_component;
}
/**
 * Create new output component
 */
static struct rayo_component *create_output_component(struct rayo_actor *actor, const char *type, iks *output, const char *client_jid)
{
	switch_memory_pool_t *pool;
	struct output_component *output_component = NULL;

	switch_core_new_memory_pool(&pool);
	output_component = switch_core_alloc(pool, sizeof(*output_component));
	output_component = OUTPUT_COMPONENT(rayo_component_init((struct rayo_component *)output_component, pool, type, "output", NULL, actor, client_jid));
	if (output_component) {
		output_component->document = iks_copy(output);
		output_component->start_offset_ms = iks_find_int_attrib(output, "start-offset");
		output_component->repeat_interval_ms = iks_find_int_attrib(output, "repeat-interval");
		output_component->repeat_times = iks_find_int_attrib(output, "repeat-times");
		output_component->max_time_ms = iks_find_int_attrib(output, "max-time");
		output_component->start_paused = iks_find_bool_attrib(output, "start-paused");
		output_component->renderer = switch_core_strdup(RAYO_POOL(output_component), iks_find_attrib_soft(output, "renderer"));
		/* get custom headers */
		{
			switch_stream_handle_t headers = { 0 };
			iks *header = NULL;
			int first = 1;
			SWITCH_STANDARD_STREAM(headers);
			for (header = iks_find(output, "header"); header; header = iks_next_tag(header)) {
				if (!strcmp("header", iks_name(header))) {
					const char *name = iks_find_attrib_soft(header, "name");
					const char *value = iks_find_attrib_soft(header, "value");
					if (!zstr(name) && !zstr(value)) {
						headers.write_function(&headers, "%s%s=%s", first ? "{" : ",", name, value);
						first = 0;
					}
				}
			}
			if (headers.data) {
				headers.write_function(&headers, "}");
				output_component->headers = switch_core_strdup(RAYO_POOL(output_component), (char *)headers.data);
				free(headers.data);
			}
		}
	} else {
		switch_core_destroy_memory_pool(&pool);
	}

	return RAYO_COMPONENT(output_component);
}
/**
 * Create a record component
 */
static struct rayo_component *record_component_create(struct rayo_actor *actor, const char *client_jid, iks *record)
{
	switch_memory_pool_t *pool;
	struct record_component *record_component = NULL;
	char *local_file_path;
	char *fs_file_path;
	switch_bool_t start_paused;

	/* validate record attributes */
	if (!VALIDATE_RAYO_RECORD(record)) {
		return NULL;
	}

	start_paused = iks_find_bool_attrib(record, "start-paused");

	/* create record filename from session UUID and ref */
	/* for example: prefix/1234-1234-1234-1234-30.wav */
	local_file_path = switch_mprintf("%s%s-%i.%s",
		globals.record_file_prefix,
		actor->id, rayo_actor_seq_next(actor), iks_find_attrib(record, "format"));

	fs_file_path = switch_mprintf("{pause=%s}fileman://%s",
		start_paused ? "true" : "false",
		local_file_path);

	switch_core_new_memory_pool(&pool);
	record_component = switch_core_alloc(pool, sizeof(*record_component));
	rayo_component_init(RAYO_COMPONENT(record_component), pool, "record", fs_file_path, actor, client_jid);
	record_component->max_duration = iks_find_int_attrib(record, "max-duration");
	record_component->initial_timeout = iks_find_int_attrib(record, "initial-timeout");
	record_component->final_timeout = iks_find_int_attrib(record, "final-timeout");
	record_component->direction = switch_core_strdup(RAYO_POOL(record_component), iks_find_attrib_soft(record, "direction"));
	record_component->mix = iks_find_bool_attrib(record, "mix");
	record_component->start_beep = iks_find_bool_attrib(record, "start-beep");
	record_component->stop_beep = iks_find_bool_attrib(record, "stop-beep");
	record_component->start_time = start_paused ? 0 : switch_micro_time_now();
	record_component->local_file_path = switch_core_strdup(RAYO_POOL(record_component), local_file_path);

	switch_safe_free(local_file_path);
	switch_safe_free(fs_file_path);

	return RAYO_COMPONENT(record_component);
}
/**
 * Seek output component
 */
static iks *seek_output_component(struct rayo_actor *component, struct rayo_message *msg, void *data)
{
	iks *iq = msg->payload;
	iks *seek = iks_find(iq, "seek");

	if (VALIDATE_RAYO_OUTPUT_SEEK(seek)) {
		int is_forward = !strcmp("forward", iks_find_attrib(seek, "direction"));
		int amount_ms = iks_find_int_attrib(seek, "amount");
		char *command = switch_mprintf("%s seek:%s%i", RAYO_JID(component),
			is_forward ? "+" : "-", amount_ms);
		switch_stream_handle_t stream = { 0 };
		SWITCH_STANDARD_STREAM(stream);

		switch_api_execute("fileman", command, NULL, &stream);

		switch_safe_free(stream.data);
		switch_safe_free(command);

		return iks_new_iq_result(iq);
	}
	return iks_new_error(iq, STANZA_ERROR_BAD_REQUEST);
}