package webui

import (
	"strings"

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

func syslogPage(ctx *macaron.Context) {
	ctx.Data["host"] = viper.GetString("Syslog.Host")
	ctx.Data["port"] = viper.GetInt("Syslog.Port")
	ctx.Data["protocol"] = viper.GetString("Syslog.Protocol")

	if syslog.Connected {
		ctx.Data["status"] = "Connected"
	} else {
		ctx.Data["status"] = "Disconnected"
	}

	ctx.HTML(200, "syslog")
}

type SyslogPostForm struct {
	Host     string `form:"host" binding:"Required"`
	Port     int    `form:"port" binding:"Required"`
	Protocol string `form:"protocol" binding:"Required"`
}

func syslogPost(ctx *macaron.Context, f SyslogPostForm) {
	// TODO: check protocol, it should be 'udp' or 'tcp'
	viper.Set("Syslog.Host", strings.ToLower(strings.TrimSpace(f.Host)))
	viper.Set("Syslog.Port", f.Port)
	viper.Set("Syslog.Protocol", strings.TrimSpace(f.Protocol))
	viper.WriteConfig()

	syslogPage(ctx)
}
