package main

import (
	// External
	werr "git.gibonuddevalla.se/go/wrappederror"

	// Standard
	"database/sql"
)

type Configuration struct {
	Settings map[string]string
}

var smonConfig Configuration

func SmonConfigInit() (err error) {
	smonConfig.Settings = make(map[string]string, 8)

	var rows *sql.Rows
	rows, err = service.Db.Conn.Query(`SELECT * FROM public.configuration`)
	if err != nil {
		err = werr.Wrap(err)
		return
	}
	defer rows.Close()

	for rows.Next() {
		var setting, value string
		err = rows.Scan(&setting, &value)
		if err != nil {
			err = werr.Wrap(err)
			return
		}

		smonConfig.Settings[setting] = value
	}

	return
}

func (cfg *Configuration) SetTheme(theme string) (err error) {
	cfg.Settings["THEME"] = theme
	_, err = service.Db.Conn.Exec(`UPDATE public.configuration SET value=$1 WHERE setting='THEME'`, theme)
	return
}
