package templates

import (
    "arimelody-web/log"
    "fmt"
    "html/template"
    "strings"
    "time"
    _ "embed"
)

//go:embed "html/layout.html"
var layoutHTML string
//go:embed "html/prideflag.html"
var prideflagHTML string

//go:embed "html/index.html"
var indexHTML string

//go:embed "html/register.html"
var registerHTML string
//go:embed "html/login.html"
var loginHTML string
//go:embed "html/login-totp.html"
var loginTotpHTML string
//go:embed "html/totp-confirm.html"
var totpConfirmHTML string
//go:embed "html/totp-setup.html"
var totpSetupHTML string
//go:embed "html/logout.html"
var logoutHTML string

//go:embed "html/logs.html"
var logsHTML string

//go:embed "html/edit-account.html"
var editAccountHTML string
//go:embed "html/edit-artist.html"
var editArtistHTML string
//go:embed "html/edit-release.html"
var editReleaseHTML string
//go:embed "html/edit-track.html"
var editTrackHTML string

//go:embed "html/components/credits/newcredit.html"
var componentNewCreditHTML string
//go:embed "html/components/credits/addcredit.html"
var componentAddCreditHTML string
//go:embed "html/components/credits/editcredits.html"
var componentEditCreditsHTML string

//go:embed "html/components/links/editlinks.html"
var componentEditLinksHTML string

//go:embed "html/components/release/release-list-item.html"
var componentReleaseListItemHTML string

//go:embed "html/components/tracks/newtrack.html"
var componentNewTrackHTML string
//go:embed "html/components/tracks/addtrack.html"
var componentAddTrackHTML string
//go:embed "html/components/tracks/edittracks.html"
var componentEditTracksHTML string

var BaseTemplate = template.Must(template.New("base").Parse(
    strings.Join([]string{ layoutHTML, prideflagHTML }, "\n"),
))

var IndexTemplate = template.Must(template.Must(BaseTemplate.Clone()).Parse(
    strings.Join([]string{
        indexHTML,
        componentReleaseListItemHTML,
    }, "\n"),
))

var LoginTemplate = template.Must(template.Must(BaseTemplate.Clone()).Parse(loginHTML))
var LoginTOTPTemplate = template.Must(template.Must(BaseTemplate.Clone()).Parse(loginTotpHTML))
var RegisterTemplate = template.Must(template.Must(BaseTemplate.Clone()).Parse(registerHTML))
var LogoutTemplate = template.Must(template.Must(BaseTemplate.Clone()).Parse(logoutHTML))
var AccountTemplate = template.Must(template.Must(BaseTemplate.Clone()).Parse(editAccountHTML))
var TOTPSetupTemplate = template.Must(template.Must(BaseTemplate.Clone()).Parse(totpSetupHTML))
var TOTPConfirmTemplate = template.Must(template.Must(BaseTemplate.Clone()).Parse(totpConfirmHTML))

var LogsTemplate = template.Must(template.Must(BaseTemplate.Clone()).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")
    },
}).Parse(logsHTML))

var EditReleaseTemplate = template.Must(template.Must(BaseTemplate.Clone()).Parse(editReleaseHTML))
var EditArtistTemplate = template.Must(template.Must(BaseTemplate.Clone()).Parse(editArtistHTML))
var EditTrackTemplate = template.Must(template.Must(BaseTemplate.Clone()).Parse(
    strings.Join([]string{
        editTrackHTML,
        componentReleaseListItemHTML,
    }, "\n"),
))

var EditCreditsTemplate = template.Must(template.Must(BaseTemplate.Clone()).Parse(componentEditCreditsHTML))
var AddCreditTemplate = template.Must(template.Must(BaseTemplate.Clone()).Parse(componentAddCreditHTML))
var NewCreditTemplate = template.Must(template.Must(BaseTemplate.Clone()).Parse(componentNewCreditHTML))

var EditLinksTemplate = template.Must(template.Must(BaseTemplate.Clone()).Parse(componentEditLinksHTML))

var EditTracksTemplate = template.Must(template.Must(BaseTemplate.Clone()).Parse(componentEditTracksHTML))
var AddTrackTemplate = template.Must(template.Must(BaseTemplate.Clone()).Parse(componentAddTrackHTML))
var NewTrackTemplate = template.Must(template.Must(BaseTemplate.Clone()).Parse(componentNewTrackHTML))
