package tmpl

import (
	"git.wanbits.cc/sin/flytalk/config"
	"git.wanbits.cc/sin/flytalk/g"
	"git.wanbits.cc/sin/flytalk/tools"
	"github.com/gin-gonic/gin"
	"html/template"
	"net/http"
)

type CommonHtml struct {
	Header template.HTML
	Nav    template.HTML
	Left   template.HTML
	Bottom template.HTML
	Rw     http.ResponseWriter
}

func NewRender(rw http.ResponseWriter) *CommonHtml {
	obj := new(CommonHtml)
	obj.Rw = rw
	header := tools.FileGetContent("html/header.html")
	nav := tools.FileGetContent("html/nav.html")
	obj.Header = template.HTML(header)
	obj.Nav = template.HTML(nav)
	return obj
}

func (obj *CommonHtml) SetLeft(file string) {
	leftStr := tools.FileGetContent("html/" + file + ".html")
	obj.Left = template.HTML(leftStr)
}

func (obj *CommonHtml) SetBottom(file string) {
	str := tools.FileGetContent("html/" + file + ".html")
	obj.Bottom = template.HTML(str)
}

func (obj *CommonHtml) Display(file string, data interface{}) {
	if data == nil {
		data = obj
	}
	main := tools.FileGetContent("html/" + file + ".html")
	t, _ := template.New(file).Parse(main)
	t.Execute(obj.Rw, data)
}

//首页
func PageIndex(c *gin.Context) {
	if c.Request.RequestURI == "/favicon.ico" {
		return
	}

	ilang, _ := c.Get("lang")
	lang := ilang.(string)
	c.HTML(http.StatusOK, "index.html", gin.H{
		"OnlineChat":    g.T("chat_entry", lang),
		"Notice":        g.T("notice", lang),
		"NowAsk":        g.T("start_chat", lang),
		"LaterAsk":      g.T("chat_later", lang),
		"Lang":          lang,
		"Title":         g.T("title", lang),
		"Keywords":      g.T("keywords", lang),
		"Desc":          g.T("description", lang),
		"Copyright":     g.T("copyright", lang),
		"MainTech":      g.T("main_tech", lang),
		"DocEntry":      g.T("doc_entry", lang),
		"VisitorsEntry": g.T("visitors_entry", lang),
		"AgentsEntry":   g.T("agents_entry", lang),
		"ChatEntry":     g.T("chat_entry", lang),
		"ProjectTitle":  g.T("project_title", lang),
		"ProjectDesc":   g.T("project_desc", lang),
		"Version":       config.VERSION,
	})
}

//登陆界面
func PageMain(c *gin.Context) {
	nav := tools.FileGetContent("html/nav.html")
	c.HTML(http.StatusOK, "main.html", gin.H{
		"Nav": template.HTML(nav),
	})
}

//客服界面
func PageChatMain(c *gin.Context) {
	c.HTML(http.StatusOK, "chat_main.html", nil)
}
