package main

import (
	"context"
	"fmt"
	"io"
	"net/http"
	"encoding/json"
)

// App struct
type App struct {
	ctx context.Context
}

type Modpack struct {
		Name string
		Nd string
		Last_updated string
}

// NewApp creates a new App application struct
func NewApp() *App {
	return &App{}
}

// startup is called when the app starts. The context is saved
// so we can call the runtime methods
func (a *App) startup(ctx context.Context) {
	a.ctx = ctx
	a.GetModpacks()
}

// Greet returns a greeting for the given name
func (a *App) Greet(name string) string {
	return fmt.Sprintf("Hello %s, It's show time!", name)
}

func (a *App) GetModpacks() []string {
		res, err := http.Get("https://gitea.piwalker.net/fclauncher/modpacks.json")
		if err != nil {
				fmt.Println(err.Error())
		}
		defer res.Body.Close()
		body , err := io.ReadAll(res.Body)
		var modpacks []Modpack
		err = json.Unmarshal([]byte(body), &modpacks)
		if err != nil {
				fmt.Println(err.Error())
				return nil
		}
		fmt.Printf("Json String: %s\n", body)
		fmt.Printf("Json: %+v\n", modpacks)
		var names []string
		for _, pack := range modpacks {
				names = append(names, pack.Name)
				fmt.Printf("Pack Found: %s\n", pack.Name)
		}
		return names
}
