package admin

import (
    "arimelody-web/log"
    "fmt"
    "html/template"
    "path/filepath"
    "strings"
    "time"
)

var indexTemplate = template.Must(template.ParseFiles(
    filepath.Join("admin", "views", "layout.html"),
    filepath.Join("view", "prideflag.html"),
    filepath.Join("admin", "components", "release", "release-list-item.html"),
    filepath.Join("admin", "views", "index.html"),
))

var loginTemplate = template.Must(template.ParseFiles(
    filepath.Join("admin", "views", "layout.html"),
    filepath.Join("view", "prideflag.html"),
    filepath.Join("admin", "views", "login.html"),
))
var loginTOTPTemplate = template.Must(template.ParseFiles(
    filepath.Join("admin", "views", "layout.html"),
    filepath.Join("view", "prideflag.html"),
    filepath.Join("admin", "views", "login-totp.html"),
))
var registerTemplate = template.Must(template.ParseFiles(
    filepath.Join("admin", "views", "layout.html"),
    filepath.Join("view", "prideflag.html"),
    filepath.Join("admin", "views", "register.html"),
))
var logoutTemplate = template.Must(template.ParseFiles(
    filepath.Join("admin", "views", "layout.html"),
    filepath.Join("view", "prideflag.html"),
    filepath.Join("admin", "views", "logout.html"),
))
var accountTemplate = template.Must(template.ParseFiles(
    filepath.Join("admin", "views", "layout.html"),
    filepath.Join("view", "prideflag.html"),
    filepath.Join("admin", "views", "edit-account.html"),
))
var totpSetupTemplate = template.Must(template.ParseFiles(
    filepath.Join("admin", "views", "layout.html"),
    filepath.Join("view", "prideflag.html"),
    filepath.Join("admin", "views", "totp-setup.html"),
))
var totpConfirmTemplate = template.Must(template.ParseFiles(
    filepath.Join("admin", "views", "layout.html"),
    filepath.Join("view", "prideflag.html"),
    filepath.Join("admin", "views", "totp-confirm.html"),
))

var logsTemplate = template.Must(template.New("layout.html").Funcs(template.FuncMap{
    "parseLevel": func(level log.LogLevel) string {
        switch level {
        case log.LEVEL_INFO:
            return "INFO"
        case log.LEVEL_WARN:
            return "WARN"
        }
        return fmt.Sprintf("%d?", level)
    },
    "titleCase": func(logType string) string {
        runes := []rune(logType)
        for i, r := range runes {
            if (i == 0 || runes[i - 1] == ' ') && r >= 'a' && r <= 'z' {
                runes[i] = r + ('A' - 'a')
            }
        }
        return string(runes)
    },
    "lower": func(str string) string { return strings.ToLower(str) },
    "prettyTime": func(t time.Time) string {
        // return t.Format("2006-01-02 15:04:05")
        // return t.Format("15:04:05, 2 Jan 2006")
        return t.Format("02 Jan 2006, 15:04:05")
    },
}).ParseFiles(
    filepath.Join("admin", "views", "layout.html"),
    filepath.Join("view", "prideflag.html"),
    filepath.Join("admin", "views", "logs.html"),
))

var releaseTemplate = template.Must(template.ParseFiles(
    filepath.Join("admin", "views", "layout.html"),
    filepath.Join("view", "prideflag.html"),
    filepath.Join("admin", "views", "edit-release.html"),
))
var artistTemplate = template.Must(template.ParseFiles(
    filepath.Join("admin", "views", "layout.html"),
    filepath.Join("view", "prideflag.html"),
    filepath.Join("admin", "views", "edit-artist.html"),
))
var trackTemplate = template.Must(template.ParseFiles(
    filepath.Join("admin", "views", "layout.html"),
    filepath.Join("view", "prideflag.html"),
    filepath.Join("admin", "components", "release", "release-list-item.html"),
    filepath.Join("admin", "views", "edit-track.html"),
))

var editCreditsTemplate = template.Must(template.ParseFiles(
    filepath.Join("admin", "components", "credits", "editcredits.html"),
))
var addCreditTemplate = template.Must(template.ParseFiles(
    filepath.Join("admin", "components", "credits", "addcredit.html"),
))
var newCreditTemplate = template.Must(template.ParseFiles(
    filepath.Join("admin", "components", "credits", "newcredit.html"),
))

var editLinksTemplate = template.Must(template.ParseFiles(
    filepath.Join("admin", "components", "links", "editlinks.html"),
))

var editTracksTemplate = template.Must(template.ParseFiles(
    filepath.Join("admin", "components", "tracks", "edittracks.html"),
))
var addTrackTemplate = template.Must(template.ParseFiles(
    filepath.Join("admin", "components", "tracks", "addtrack.html"),
))
var newTrackTemplate = template.Must(template.ParseFiles(
    filepath.Join("admin", "components", "tracks", "newtrack.html"),
))
