package main

import (
	"context"
	"errors"
	"fmt"
	"galaxy/client"
	"galaxy/client/appmeta"
	"galaxy/client/loader"
	"galaxy/connector/http"
	"galaxy/storage/fs"
	"os"
	"os/signal"

	"fyne.io/fyne/v2/app"
	"fyne.io/fyne/v2/lang"
)

func main() {
	var err error
	defer func() {
		if err == nil {
			if r := recover(); r != nil {
				err = errors.Join(err, fmt.Errorf("panic: %v", r))
			}
		}
		if err != nil {
			fmt.Fprintln(os.Stderr, err)
			os.Exit(1)
		}
	}()

	ctx, cancel := signal.NotifyContext(context.Background(), os.Interrupt)
	defer cancel()

	app := app.NewWithID(appmeta.AppID)
	if err = lang.AddTranslationsFS(client.Translations, "resource/lang"); err != nil {
		return
	}
	s, err := fs.NewFS(app.Storage().RootURI().Path())
	if err != nil {
		return
	}
	c, err := http.NewHttpConnector(ctx, appmeta.DefaultBackendURL)
	if err != nil {
		return
	}
	l, err := loader.NewLoader(s, c, app)
	if err != nil {
		return
	}
	err = l.Run(ctx)
}
