package webui

import (
	"net/http"

	"git.openpdu.org/OpenPDU/openpdu/syslog"
	"github.com/go-macaron/binding"
	"github.com/go-macaron/pongo2"
	"github.com/spf13/viper"
	"gopkg.in/macaron.v1"
)

// Dictionary aaa
type Dictionary map[string]interface{}

func init() {
	viper.SetDefault("system.listeningport", 4000)
}

func StartServer() {
	m := macaron.Classic()
	m.Use(pongo2.Pongoer())
	m.Use(macaron.Static("static"))

	m.Get("/", statusPage)
	m.Get("/outlets", outletsPage)
	m.Get("/outlet/:num", outletEditPage)
	m.Post("/outlet/:num", binding.Bind(OutletPostForm{}), outletEditPost)
	m.Get("/lan", lanPage)
	m.Get("/mqtt", mqttPage)
	m.Post("/mqtt", binding.Bind(MQTTPostForm{}), mqttPost)
	m.Get("/ups", upsPage)
	m.Post("/ups", binding.Bind(UPSPostForm{}), upsPost)
	m.Get("/syslog", syslogPage)
	m.Post("/syslog", binding.Bind(SyslogPostForm{}), syslogPost)
	m.Get("/backup", backupPage)
	m.Post("/backup", backupDownload)
	m.Get("/json/status", jsonStatus)
	m.Post("/json/outlet/:id/toggle", jsonOutletToggle)

	syslog.Info("Web interface ready")
	http.ListenAndServe("0.0.0.0:"+viper.GetString("system.listeningport"), m)
}
