//go:build js && wasm
// +build js,wasm

package main

import (
	"crypto/rand"
	"encoding/hex"
	"fmt"
	"log"
	"syscall/js"
)

// loadOrCreatePlayerCode lädt oder erstellt einen eindeutigen Spieler-Code (WebAssembly Version)
func (g *Game) loadOrCreatePlayerCode() {
	const storageKey = "escape_from_teacher_player_code"

	// Versuche aus LocalStorage zu laden
	if jsGlobal := js.Global(); !jsGlobal.IsUndefined() {
		localStorage := jsGlobal.Get("localStorage")
		if !localStorage.IsUndefined() {
			stored := localStorage.Call("getItem", storageKey)
			if !stored.IsNull() && stored.String() != "" {
				g.playerCode = stored.String()
				log.Printf("🔑 Player-Code aus LocalStorage geladen: %s", g.playerCode)
				return
			}
		}
	}

	// Erstelle neuen Code
	bytes := make([]byte, 16)
	if _, err := rand.Read(bytes); err != nil {
		log.Fatal("Fehler beim Generieren des Player-Codes:", err)
	}
	g.playerCode = hex.EncodeToString(bytes)

	// In LocalStorage speichern
	if jsGlobal := js.Global(); !jsGlobal.IsUndefined() {
		localStorage := jsGlobal.Get("localStorage")
		if !localStorage.IsUndefined() {
			localStorage.Call("setItem", storageKey, g.playerCode)
			log.Printf("🆕 Neuer Player-Code in LocalStorage erstellt: %s", g.playerCode)
		}
	}
}

// loadPlayerName lädt gespeicherten Spielernamen (WebAssembly Version)
func (g *Game) loadPlayerName() string {
	const storageKey = "escape_from_teacher_player_name"

	if jsGlobal := js.Global(); !jsGlobal.IsUndefined() {
		localStorage := jsGlobal.Get("localStorage")
		if !localStorage.IsUndefined() {
			stored := localStorage.Call("getItem", storageKey)
			if !stored.IsNull() && stored.String() != "" {
				log.Printf("👤 Spielername aus LocalStorage geladen: %s", stored.String())
				return stored.String()
			}
		}
	}
	return ""
}

// savePlayerName speichert Spielernamen (WebAssembly Version)
func (g *Game) savePlayerName(name string) {
	const storageKey = "escape_from_teacher_player_name"

	if jsGlobal := js.Global(); !jsGlobal.IsUndefined() {
		localStorage := jsGlobal.Get("localStorage")
		if !localStorage.IsUndefined() {
			localStorage.Call("setItem", storageKey, name)
			log.Printf("💾 Spielername in LocalStorage gespeichert: %s", name)
		}
	}
}

func (g *Game) loadHighscore() int {
	const storageKey = "escape_from_teacher_highscore"
	if jsGlobal := js.Global(); !jsGlobal.IsUndefined() {
		localStorage := jsGlobal.Get("localStorage")
		if !localStorage.IsUndefined() {
			stored := localStorage.Call("getItem", storageKey)
			if !stored.IsNull() && stored.String() != "" {
				var score int
				if _, err := fmt.Sscanf(stored.String(), "%d", &score); err == nil {
					return score
				}
			}
		}
	}
	return 0
}

func (g *Game) saveHighscore(score int) {
	const storageKey = "escape_from_teacher_highscore"
	if jsGlobal := js.Global(); !jsGlobal.IsUndefined() {
		localStorage := jsGlobal.Get("localStorage")
		if !localStorage.IsUndefined() {
			localStorage.Call("setItem", storageKey, fmt.Sprintf("%d", score))
		}
	}
}
