<?php
/**
 * Nexora V6 Custom — Twitch / TikTok dock (AdminCP modules twitch_streamer, tiktok_streamer)
 */

if (!defined('access') || !access) {
	die();
}

@loadModuleConfigs('twitch_streamer');
$twitch_active = function_exists('mconfig') && (int) mconfig('active') === 1 && function_exists('check_value') && check_value(mconfig('channel'));
$twitch_channel = $twitch_active ? preg_replace('/[^a-zA-Z0-9_]/', '', (string) mconfig('channel')) : '';
$twitch_label = (function_exists('check_value') && check_value(mconfig('button_label'))) ? (string) mconfig('button_label') : 'Twitch';

@loadModuleConfigs('tiktok_streamer');
$tiktok_active = function_exists('mconfig') && (int) mconfig('active') === 1 && function_exists('check_value') && check_value(mconfig('username'));
$tiktok_username = $tiktok_active ? preg_replace('/[^a-zA-Z0-9_.]/', '', (string) mconfig('username')) : '';
$tiktok_label = (function_exists('check_value') && check_value(mconfig('button_label'))) ? (string) mconfig('button_label') : 'TikTok';

if (!$twitch_active && !$tiktok_active) {
	return;
}

$streamer_parent_domain = isset($_SERVER['HTTP_HOST']) ? (string) $_SERVER['HTTP_HOST'] : '';
$streamer_parent_domain = preg_replace('/^www\./', '', $streamer_parent_domain);
if ($streamer_parent_domain === '') {
	$streamer_parent_domain = 'localhost';
}

$lbl_close = function_exists('nexora_lang') ? nexora_lang('nexora_stream_close', 'Cerrar') : 'Cerrar';
?>
<div class="nex-stream-dock" id="nex-stream-dock">
	<div class="nex-stream-dock__rail">
		<?php if ($twitch_active) { ?>
			<button type="button" class="nex-stream-dock__btn nex-stream-dock__btn--twitch" data-nex-stream-panel="twitch" aria-expanded="false" aria-controls="nex-stream-panel-twitch"
				aria-label="<?php echo htmlspecialchars($twitch_label, ENT_QUOTES, 'UTF-8'); ?>">
				<svg width="22" height="22" viewBox="0 0 24 24" fill="currentColor" aria-hidden="true"><path d="M11.571 4.714h1.715v5.143H11.57zm4.715 0H18v5.143h-1.714zM6 0L1.714 4.286v15.428h5.143V24l4.286-4.286h3.428L22.286 12V0zm14.571 11.143l-3.428 3.428h-3.429l-3 3v-3H6.857V1.714h13.714Z"/></svg>
				<span><?php echo htmlspecialchars($twitch_label, ENT_QUOTES, 'UTF-8'); ?></span>
			</button>
		<?php } ?>
		<?php if ($tiktok_active) { ?>
			<button type="button" class="nex-stream-dock__btn nex-stream-dock__btn--tiktok" data-nex-stream-panel="tiktok" aria-expanded="false" aria-controls="nex-stream-panel-tiktok"
				aria-label="<?php echo htmlspecialchars($tiktok_label, ENT_QUOTES, 'UTF-8'); ?>">
				<svg width="22" height="22" viewBox="0 0 24 24" fill="currentColor" aria-hidden="true"><path d="M19.59 6.69a4.83 4.83 0 0 1-3.77-4.25V2h-3.45v13.67a2.89 2.89 0 0 1-5.2 1.74 2.89 2.89 0 0 1 2.31-4.64 2.93 2.93 0 0 1 .88.13V9.4a6.84 6.84 0 0 0-1-.05A6.33 6.33 0 0 0 5 20.1a6.34 6.34 0 0 0 10.86-4.43v-7a8.16 8.16 0 0 0 4.77 1.52v-3.4a4.85 4.85 0 0 1-1-.1z"/></svg>
				<span><?php echo htmlspecialchars($tiktok_label, ENT_QUOTES, 'UTF-8'); ?></span>
			</button>
		<?php } ?>
	</div>

	<?php if ($twitch_active) {
		$twitch_embed = 'https://player.twitch.tv/?channel=' . rawurlencode($twitch_channel) . '&parent=' . rawurlencode($streamer_parent_domain) . '&muted=false';
		?>
		<div class="nex-stream-dock__panel nex-stream-dock__panel--twitch" id="nex-stream-panel-twitch" role="dialog" aria-modal="false" aria-hidden="true" aria-label="<?php echo htmlspecialchars($twitch_label, ENT_QUOTES, 'UTF-8'); ?>">
			<div class="nex-stream-dock__panel-head">
				<span class="nex-stream-dock__panel-title"><?php echo htmlspecialchars($twitch_label, ENT_QUOTES, 'UTF-8'); ?></span>
				<button type="button" class="nex-stream-dock__panel-close" data-nex-stream-close="twitch" aria-label="<?php echo htmlspecialchars($lbl_close, ENT_QUOTES, 'UTF-8'); ?>">&times;</button>
			</div>
			<div class="nex-stream-dock__panel-body">
				<div class="nex-stream-dock__mount" data-nex-stream-mount data-stream-src="<?php echo htmlspecialchars($twitch_embed, ENT_QUOTES, 'UTF-8'); ?>" data-stream-title="<?php echo htmlspecialchars($twitch_label, ENT_QUOTES, 'UTF-8'); ?>">
					<span class="nex-stream-dock__loading">La transmisión se carga al abrir este panel.</span>
				</div>
			</div>
		</div>
	<?php } ?>

	<?php if ($tiktok_active) {
		$tiktok_live_url = 'https://www.tiktok.com/@' . rawurlencode($tiktok_username) . '/live';
		?>
		<div class="nex-stream-dock__panel nex-stream-dock__panel--tiktok" id="nex-stream-panel-tiktok" role="dialog" aria-modal="false" aria-hidden="true" aria-label="<?php echo htmlspecialchars($tiktok_label, ENT_QUOTES, 'UTF-8'); ?>">
			<div class="nex-stream-dock__panel-head">
				<span class="nex-stream-dock__panel-title"><?php echo htmlspecialchars($tiktok_label, ENT_QUOTES, 'UTF-8'); ?></span>
				<button type="button" class="nex-stream-dock__panel-close" data-nex-stream-close="tiktok" aria-label="<?php echo htmlspecialchars($lbl_close, ENT_QUOTES, 'UTF-8'); ?>">&times;</button>
			</div>
			<div class="nex-stream-dock__panel-body">
				<div class="nex-stream-dock__mount" data-nex-stream-mount data-stream-src="<?php echo htmlspecialchars($tiktok_live_url, ENT_QUOTES, 'UTF-8'); ?>" data-stream-title="<?php echo htmlspecialchars($tiktok_label, ENT_QUOTES, 'UTF-8'); ?>">
					<span class="nex-stream-dock__loading">La transmisión se carga al abrir este panel.</span>
				</div>
			</div>
		</div>
	<?php } ?>
</div>
