// Einfacher HTML-Generator für nuscore lokale Kopie (iframe-basiert)
export function generateSimpleNuscoreHTML(match) {
  const pin = match.homePin || match.guestPin || '';
  const code = match.code || '';
  
  return `<!DOCTYPE html>
<html lang="de">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>nuscore - Lokale Kopie</title>
    <style>
        body { margin: 0; padding: 0; font-family: Arial, sans-serif; }
        .header { background: #f0f0f0; padding: 10px; border-bottom: 1px solid #ccc; }
        .controls { margin: 10px 0; }
        .controls button { margin-right: 10px; padding: 5px 10px; }
        .iframe-container { width: 100%; height: calc(100vh - 100px); }
        .iframe-container iframe { width: 100%; height: 100%; border: none; }
        .status { padding: 10px; background: #e8f5e8; border: 1px solid #4caf50; margin: 10px 0; }
    </style>
</head>
<body>
    <div class="header">
        <h1>nuscore - Lokale Kopie</h1>
        <div class="controls">
            <button onclick="insertPin()">📌 PIN einfügen</button>
            <button onclick="copyPin()">📋 PIN kopieren</button>
            <input type="password" id="pinInput" placeholder="PIN eingeben" style="margin-left: 10px;">
        </div>
        <div class="status" id="status">Bereit für PIN-Einfügung</div>
    </div>
    <div class="iframe-container">
        <iframe id="nuscoreFrame" src="https://ttde-apps.liga.nu/nuliga/nuscore-tt/meetings-list"></iframe>
    </div>
    
    <script>
        let currentPin = "";
        
        // PIN aus Match-Objekt laden
        const pin = "${pin}";
        const code = "${code}";
        
        if (pin) {
            currentPin = pin;
            document.getElementById("pinInput").value = pin;
            document.getElementById("status").textContent = "PIN aus Match geladen: " + pin;
        }
        
        function insertPin() {
            const pin = document.getElementById("pinInput").value || currentPin;
            if (!pin) {
                document.getElementById("status").textContent = "Keine PIN eingegeben";
                return;
            }
            
            const iframe = document.getElementById("nuscoreFrame");
            const message = {
                action: "fillPin",
                pin: pin,
                timestamp: Date.now(),
                source: "nuscore-local"
            };
            
            try {
                iframe.contentWindow.postMessage(message, "https://ttde-apps.liga.nu");
                document.getElementById("status").textContent = "PIN gesendet: " + pin;
            } catch (error) {
                document.getElementById("status").textContent = "Fehler beim Senden der PIN: " + error.message;
            }
        }
        
        function copyPin() {
            const pin = document.getElementById("pinInput").value || currentPin;
            if (!pin) {
                document.getElementById("status").textContent = "Keine PIN zum Kopieren";
                return;
            }
            
            navigator.clipboard.writeText(pin).then(() => {
                document.getElementById("status").textContent = "PIN kopiert: " + pin;
            }).catch(() => {
                // Fallback
                const textArea = document.createElement("textarea");
                textArea.value = pin;
                document.body.appendChild(textArea);
                textArea.select();
                document.execCommand("copy");
                document.body.removeChild(textArea);
                document.getElementById("status").textContent = "PIN kopiert (Fallback): " + pin;
            });
        }
        
        // PostMessage Listener für Antworten vom iframe
        window.addEventListener("message", function(event) {
            if (event.origin !== "https://ttde-apps.liga.nu") return;
            
            if (event.data && event.data.action === "pinInserted") {
                document.getElementById("status").textContent = "PIN erfolgreich eingefügt!";
            } else if (event.data && event.data.action === "pinError") {
                document.getElementById("status").textContent = "PIN-Einfügung fehlgeschlagen: " + event.data.error;
            }
        });
        
        console.log("nuscore lokale Kopie gestartet");
        if (code) console.log("Code aus Match:", code);
        if (pin) console.log("PIN aus Match:", pin);
    </script>
</body>
</html>`;
}

