package vui

import (
	"bytes"
	"netease_control_center/interfaces/log"
	"strings"
	"text/template"
	"time"
)

var (
	funmap = template.FuncMap{"title": strings.Title, "join": strings.Join, "hour": TempFunc_Hour, "minute": TempFunc_Minute,
		"second": TempFunc_Second, "month": TempFunc_Month, "day": TempFunc_Day, "now": TempFunc_Now, "timename": TempFunc_CurTimeName,
		"week": TempFunc_Week}
	ntemplate = template.New("ihw").Funcs(funmap)
)

//	Mon Jan 2 15:04:05 -0700 MST 2006
// 2006-01-02 15:04:05.999999999 -0700 MST
func TempFunc_Now(format string) string {
	return time.Now().Local().Format(format)
}

func TempFunc_Hour() int {
	return time.Now().Local().Hour()
}

func TempFunc_Minute() int {
	return time.Now().Local().Minute()
}

func TempFunc_Second() int {
	return time.Now().Local().Second()
}

func TempFunc_Month() int {
	return int(time.Now().Local().Month())
}

func TempFunc_Day() int {
	return int(time.Now().Local().Day())
}

func TempFunc_CurTimeName() string {
	tmp := GetCurDayIntervalName()
	return tmp.String()
}

var TempVal_Week = [...]string{
	"星期日",
	"星期一",
	"星期二",
	"星期三",
	"星期四",
	"星期五",
	"星期六",
}

func TempFunc_Week() string {
	return TempVal_Week[int(time.Now().Local().Weekday())]
}

func TempFunc_Year() int {
	return time.Now().Local().Year()
}

type Ntemplate interface {
	GetSceneName() string
}

func ParseTemplate(val interface{}, pattern string) string {
	defer func() {
		if err := recover(); err != nil {
			log.Println(log.ErrorLog, "err:", err)
		}
	}()

	buf := new(bytes.Buffer)
	template.Must(ntemplate.Parse(pattern)).Execute(buf, val)

	return buf.String()
}

func ParseDefaultTemplate(pattern string) string {
	defer func() {
		if err := recover(); err != nil {
			log.Println(log.ErrorLog, "err:", err)
		}
	}()

	buf := new(bytes.Buffer)
	template.Must(ntemplate.Parse(pattern)).Execute(buf, nil)

	return buf.String()
}
