package mcclprofile

import (
	"encoding/json"
	"os"
	"path"
)

const ProfileFileName = "mccl_profile.json"

type Profile struct {
	Username string `json:"username"`
	Uuid     string `json:"uuid"`
	JavaXmx  string `json:"java-Xmx"`
}

func (p *Profile) Store(rootDir string) error {
	pf, err := os.OpenFile(path.Join(rootDir, ProfileFileName), os.O_CREATE|os.O_RDWR, 0666)
	if err != nil {
		return err
	}
	defer pf.Close()

	je := json.NewEncoder(pf)
	je.SetIndent("", "\t")
	if err := je.Encode(p); err != nil {
		return err
	}

	return nil
}

func Load(rootDir string) (*Profile, error) {
	p := &Profile{}
	pf, err := os.Open(path.Join(rootDir, ProfileFileName))
	if err != nil {
		return nil, err
	}
	defer pf.Close()

	jd := json.NewDecoder(pf)
	if err := jd.Decode(p); err != nil {
		return nil, err
	}

	return p, nil
}