package main

import (
	"crypto/rsa"
	encrypt "fckeuspy-go/lib"
	"html/template"
	"log"
	"net/http"
	"os"
	"time"
)

var (
	priv    *rsa.PrivateKey
	pubPEM  []byte
	certPEM []byte // self-signed cert jen pro sdílení identity (volitelné)
	tmpl    *template.Template
)

type envelope struct {
	// Encrypted AES key, Nonce, Ciphertext (GCM)
	EK string `json:"ek"` // base64(RSA-OAEP(aesKey))
	N  string `json:"n"`  // base64(nonce 12B)
	CT string `json:"ct"` // base64(GCM(ciphertext||tag))
}

func main() {
	if err := rootCmd.Execute(); err != nil {
		log.Printf("Chyba: %v", err)
		os.Exit(1)
	}
	// Malé zpoždění pro případné async logy
	time.Sleep(50 * time.Millisecond)
}

func runFyne() {
	storageDir, w := NewUI()
	svc, err := encrypt.NewService(storageDir)
	if err != nil {
		log.Fatal(err)
	}
	parts := buildEntries()
	/*
		onDecrypt := func(pl string) {
			go func(js string) {
				res, err := svc.Decrypt(js)
				if err != nil {
					fyne.Do(func() { parts.plainOut.SetText(""); parts.showToast("Chyba") })
					return
				}
				fyne.Do(func() { parts.plainOut.SetText(res); parts.showToast("Dešifrováno") })
			}(pl)
		}
	*/
	root := buildTabbedUI(parts, svc)
	w.SetContent(root)
	w.ShowAndRun()
}

func RunWebApp() {
	var err error
	// 2) inicializuj šifrovací službu
	_, err = encrypt.NewService("")
	if err != nil {
		log.Fatal(err)
	}

	// 2) šablony
	tmpl = template.Must(template.ParseGlob("templates/*.html"))

	muxServer := NewServer()
	log.Fatal(http.ListenAndServe(":8080", muxServer))
}
