package main

import (
	"encoding/json"
	"io/ioutil"
)

// Configuration def
type Configuration struct {
	Hostname string            `json:"hostname"`
	Outlets  map[(uint)]Outlet `json:"outlets"`
	Boards   []*Board          `json:"boards"`
	MQTT     MQTTConfig        `json:"mqtt"`
}

// TheConfig def
var TheConfig Configuration

func loadConfig(filename string) (Configuration, error) {
	bytes, err := ioutil.ReadFile(filename)
	if err != nil {
		return Configuration{}, err
	}

	var c Configuration
	err = json.Unmarshal(bytes, &c)
	if err != nil {
		return Configuration{}, err
	}

	return c, nil
}

func saveConfig(c Configuration, filename string) error {
	bytes, err := json.MarshalIndent(c, "", "  ")
	if err != nil {
		return err
	}

	return ioutil.WriteFile(filename, bytes, 0644)
}

func writeConfig() error {
	return saveConfig(TheConfig, "t1.json")
}
