package main

import "github.com/br0xen/boltease"

var db *boltease.DB
var dbOpened int

func GetDefaultSiteConfig() *siteData {
	ret := new(siteData)
	ret.Title = "Static Site Management System"
	ret.Port = 8080
	ret.SessionName = "gossms"
	ret.ServerDir = "./"
	return ret
}

func openDatabase() error {
	dbOpened += 1
	if dbOpened == 1 {
		var err error
		db, err = boltease.Create(DbName, 0600, nil)
		if err != nil {
			return err
		}
	}
	return nil
}

func closeDatabase() error {
	dbOpened -= 1
	if dbOpened == 0 {
		return db.CloseDB()
	}
	return nil
}

func initDatabase() error {
	var err error
	if err = openDatabase(); err != nil {
		return err
	}
	defer closeDatabase()

	// Create the path to the bucket to store admin users
	if err := db.MkBucketPath([]string{"users"}); err != nil {
		return err
	}
	// Create the path to the bucket to store site config data
	return db.MkBucketPath([]string{"site"})
}

func dbGetSiteConfig() *siteData {
	var ret *siteData
	def := GetDefaultSiteConfig()
	var err error
	if err = openDatabase(); err != nil {
		return def
	}
	defer closeDatabase()

	ret = new(siteData)
	siteConf := []string{"site"}
	if ret.Title, err = db.GetValue(siteConf, "title"); err != nil {
		ret.Title = def.Title
	}
	if ret.Port, err = db.GetInt(siteConf, "port"); err != nil {
		ret.Port = def.Port
	}
	if ret.SessionName, err = db.GetValue(siteConf, "session-name"); err != nil {
		ret.SessionName = def.SessionName
	}
	if ret.ServerDir, err = db.GetValue(siteConf, "server-dir"); err != nil {
		ret.ServerDir = def.ServerDir
	}
	return ret
}

func dbSaveSiteConfig(dat *siteData) error {
	var err error
	if err = openDatabase(); err != nil {
		return err
	}
	defer closeDatabase()

	siteConf := []string{"site"}
	if err = db.SetValue(siteConf, "title", dat.Title); err != nil {
		return err
	}
	if err = db.SetInt(siteConf, "port", dat.Port); err != nil {
		return err
	}
	if err = db.SetValue(siteConf, "session-name", dat.SessionName); err != nil {
		return err
	}
	return db.SetValue(siteConf, "server-dir", dat.ServerDir)
}
