package gophertype

import (
	"html/template"
	"net/http"

	"git.kirsle.net/apps/gophertype/pkg/cache"
	"git.kirsle.net/apps/gophertype/pkg/console"
	"git.kirsle.net/apps/gophertype/pkg/controllers"
	"git.kirsle.net/apps/gophertype/pkg/models"
	"git.kirsle.net/apps/gophertype/pkg/responses"
	"git.kirsle.net/apps/gophertype/pkg/settings"
	"github.com/jinzhu/gorm"
)

// Site is the master struct for the Gophertype server.
type Site struct {
	mux http.Handler
}

// NewSite initializes the Site.
func NewSite(pubroot string) *Site {
	// Initialize the settings.json inside the user root.
	if err := settings.SetFilename(pubroot); err != nil {
		panic(err)
	}

	site := &Site{}

	// Register blog global template functions.
	responses.ExtraFuncs = template.FuncMap{
		"BlogIndex":         controllers.PartialBlogIndex,
		"RenderComments":    controllers.RenderComments,
		"RenderCommentsRO":  controllers.RenderCommentsRO,
		"RenderComment":     controllers.RenderComment,
		"RenderCommentForm": controllers.RenderCommentForm,
	}

	return site
}

// UseDB specifies the database to use.
func (s *Site) UseDB(driver string, path string) error {
	db, err := gorm.Open(driver, path)
	if err != nil {
		return err
	}

	console.Info("Using database driver '%s'", driver)
	models.UseDB(db)
	return nil
}

// SetupRedis connects to the Redis instance if available.
func (s *Site) SetupRedis() error {
	return cache.Connect()
}

// ListenAndServe starts the HTTP service.
func (s *Site) ListenAndServe(addr string) error {
	console.Info("Listening on %s", addr)
	server := http.Server{
		Addr:    addr,
		Handler: s.mux,
	}
	return server.ListenAndServe()
}
