<?php

use Models\Character;

/**
 * Render the logout button's form.
 */
function c_logout_button(): string
{
	return render('components/logout_button');
}

/**
 * Render the character bar. Relies on there being a character in the session. Without one, this will return an empty
 * string.
 */
function c_char_bar(): string
{
	if (char() === false) return '';
	return render('components/char_bar', ['char' => char()]);
}

/**
 * Render the left sidebar navigation menu. Provide the active tab to highlight it. Will retrieve the current
 * tab from the GLOBALS.
 */
function c_left_nav(): string
{
	return render('components/left_nav');
}

/**
 * Render the right sidebar menu.
 */
function c_right_nav(): string
{
	return render('components/right_nav', ['c' => char()]);
}

/**
 * Render the debug query log.
 */
function c_debug_query_log(): string
{
	return render('components/debug_query_log');
}

/**
 * Render the character select radio buttons.
 */
function c_char_select_box(int $id, array $info): string
{
	return render('components/char_select_box', ['id' => $id, 'c' => $info]);
}

/**
 * Render an alert with a given type and message.
 */
function c_alert(string $type, string $message): string
{
	$a = $type !== 'danger' ? ' auto-close="5000"' : '';
	return "<div class=\"alert $type\"$a><div>$message</div> <a alert-close>&times;</a></div>";
}

/**
 * Renders a danger alert with form errors, if there are any. Add an optional placement id.
 */
function c_form_errors(string $placement = ''): string
{
	$errors = $GLOBALS[($placement !== '' ? "form-errors-$placement" : 'form-errors')] ?? false;
	if ($errors === false) return '';
	$html = '';
	foreach ($errors as $field)
		if (!empty($field))
			foreach ($field as $message) $html .= !empty($message) ? "<p>$message</p>" : '';
	return c_alert('danger', $html);
}

/**
 * Generate a text form field. This component will automatically add the error class if there are errors for this field,
 * depending on the form-errors GLOBAL. Pass an optional form ID to target a specific form GLOBAL.
 */
function c_form_field(
	string $type,
	string $name,
	string $placeholder,
	bool $required = false,
	string $autocomplete = "off",
	string $formId = ''
) {
	$errors = $GLOBALS[($formId !== '' ? "form-errors-$formId" : 'form-errors')] ?? false;
	$html = "<input type=\"$type\" name=\"$name\" id=\"$name\" placeholder=\"$placeholder\"";
	if ($required) $html .= ' required';
	if (isset($_POST[$name]) && $type !== 'password') $html .= ' value="' . $_POST[$name] . '"';
	$html .= $errors !== false && !empty($errors[$name]) ? ' class="form control error"' : ' class="form control"';
	return $html . " autocomplete=\"$autocomplete\">";
}

/**
 * Render the stopwatch debug component.
 */
function c_debug_stopwatch()
{
	return render('components/debug_stopwatch');
}

/**
 * Render a profile's stat grid using a character array.
 */
function c_profile_stats(Character $char): string
{
	return render('components/profile_stats', ['char' => $char]);
}

/**
 * Render a character's equipped gear for their profile.
 */
function c_equipped_gear(Character $char): string
{
	return render('components/equipped_gear', ['char' => $char]);
}

/**
 * Render a front-end UI window with the given title and content. The WindowManager on the front end will handle
 * the rest.
 */
function c_ui_window(string $title = '', string $content = ''): string
{
	return render('components/ui_window', ['title' => $title, 'content' => $content]);
}
