<?php
require_once __DIR__ . '/../includes/header.php';
require_once __DIR__ . '/../api/_security_store.php';

$appUrl = '';
$https = false;

if (!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off') {
    $https = true;
}
if (!empty($_SERVER['HTTP_X_FORWARDED_PROTO']) && strtolower((string)$_SERVER['HTTP_X_FORWARDED_PROTO']) === 'https') {
    $https = true;
}

$scheme = $https ? 'https' : 'http';
$host = isset($_SERVER['HTTP_HOST']) ? (string)$_SERVER['HTTP_HOST'] : '';

if (isset($BASE_URL) && trim((string)$BASE_URL) !== '') {
    $basePath = trim((string)$BASE_URL);
    if (preg_match('/^https?:\/\//i', $basePath)) {
        $appUrl = rtrim($basePath, '/');
    } else {
        $appUrl = rtrim($scheme . '://' . $host . '/' . ltrim($basePath, '/'), '/');
    }
} else {
    $appUrl = rtrim($scheme . '://' . $host, '/');
}

function app_page_token_url(string $value): string
{
    $bytes = array_values(unpack('C*', $value) ?: []);
    $len = count($bytes);
    $out = '';
    for ($i = 0; $i < $len; $i++) {
        $src = $bytes[$len - 1 - $i];
        $dyn = (($i * 37 + 19) ^ ($len * 11)) & 0xFF;
        $out .= chr(($src ^ $dyn ^ 0xA7) & 0xFF);
    }
    return 'v3.' . rtrim(strtr(base64_encode($out), '+/', '-_'), '=');
}

function app_page_mobile_seed(string $value): string
{
    $bytes = array_values(unpack('C*', $value) ?: []);
    $out = '';
    foreach ($bytes as $b) {
        $out .= chr(($b ^ 90) & 0xFF);
    }
    return base64_encode($out);
}

$currentToken = security_store_token();
$currentTokenUrl = app_page_token_url($appUrl);
$currentMobileSeed = app_page_mobile_seed($appUrl);

$mobileJsonPayload = [
    'token_url' => $currentTokenUrl,
    'app_token' => $currentToken,
    'device_type' => 'mobile',
    'panel_api' => 'v3',
];

$tvJsonPayload = [
    'token_url' => $currentTokenUrl,
    'app_token' => $currentToken,
    'device_type' => 'tv',
    'panel_api' => 'v3',
];

$mobileJsonText = json_encode($mobileJsonPayload, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES | JSON_PRETTY_PRINT);
$tvJsonText = json_encode($tvJsonPayload, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES | JSON_PRETTY_PRINT);
?>
<div class="appjson-wrap">
    

    <div class="appjson-grid">
        <div class="card radius-10 appjson-card">
            <div class="card-header bg-transparent appjson-header">
                <div class="appjson-title">
                    <div class="ic"><i class="bi bi-phone"></i></div>
                    <div>
                        <h5>Configuração Celular</h5>
                     
                    </div>
                </div>
                <span class="badge-soft"><i class="bi bi-shield-check"></i> Mobile v3</span>
            </div>
            <div class="card-body">
                <label class="secure-label">Conteúdo para assets/config.json</label>
                <div id="mobileJsonBox" class="json-box mb-3"></div>
                <button type="button" data-copy-target="mobileJsonBox" data-copy-label="config.json do celular" class="btn btn-light copy-btn"><i class="bi bi-clipboard-check"></i> Copiar config.json</button>
               
            </div>
        </div>

        <div class="card radius-10 appjson-card">
            <div class="card-header bg-transparent appjson-header">
                <div class="appjson-title">
                    <div class="ic"><i class="bi bi-tv"></i></div>
                    <div>
                        <h5>Configuração TV</h5>
                      
                    </div>
                </div>
                <span class="badge-soft"><i class="bi bi-shield-check"></i> TV v3</span>
            </div>
            <div class="card-body">
                <label class="secure-label">Conteúdo para assets/app.json</label>
                <div id="tvJsonBox" class="json-box mb-3"></div>
                <button type="button" data-copy-target="tvJsonBox" data-copy-label="app.json da TV" class="btn btn-light copy-btn"><i class="bi bi-clipboard-check"></i> Copiar app.json</button>
               
            </div>
        </div>
    </div>
</div>

<textarea id="mobileJsonData" class="d-none" aria-hidden="true"><?= htmlspecialchars((string)$mobileJsonText, ENT_NOQUOTES | ENT_SUBSTITUTE, 'UTF-8') ?></textarea>
<textarea id="tvJsonData" class="d-none" aria-hidden="true"><?= htmlspecialchars((string)$tvJsonText, ENT_NOQUOTES | ENT_SUBSTITUTE, 'UTF-8') ?></textarea>

<?php require_once __DIR__ . '/../includes/footer.php'; ?>
