package main

import (
	"bytes"
	"encoding/json"
	"fmt"
	"time"
)

type Modpack struct {
	Name         string
	Id           string
	Last_updated string
	Versions     []Version
}

type Version struct {
	Version string
	Data    time.Time
	File    string
}

type ModpackManager struct {
	app      *App
	Modpacks []Modpack
}

func (m *ModpackManager) QuerryModpacks() {
	m.Modpacks = []Modpack{}
	buff := new(bytes.Buffer)
	err := HttpDownload("modpacks.json", buff, nil)
	if err != nil {
		fmt.Printf("HTTP error: %s\n", err)
		return
	}
	err = json.Unmarshal(buff.Bytes(), &m.Modpacks)
	if err != nil {
		return
	}

	for ind, pack := range m.Modpacks {
		buff = new(bytes.Buffer)
		err = HttpDownload(pack.Id+"/versions.json", buff, nil)
		if err != nil {
			continue
		}
		json.Unmarshal(buff.Bytes(), &pack.Versions)
		m.Modpacks[ind] = pack
	}
}

func (m *ModpackManager) GetModpacks() []Modpack {
	return m.Modpacks
}

func (m *ModpackManager) GetModpack(id string) Modpack {
	for _, pack := range m.Modpacks {
		if pack.Id == id {
			return pack
		}
	}
	return Modpack{}
}
