// Package i18n is the language catalog for the web interface.
// English is the source text. Nederlands, Deutsch and Français are
// translations. Cuneiform is the English text written phonetically
// with Sumero-Akkadian signs.
package i18n

import (
	"errors"
	"fmt"
	"strings"
	"time"
)

// Lang is a choice stored on the user.
type Lang string

const (
	EN        Lang = "en"
	NL        Lang = "nl"
	DE        Lang = "de"
	FR        Lang = "fr"
	Cuneiform Lang = "cuneiform"
)

// All is the order shown in the account settings.
func All() []Lang { return []Lang{EN, NL, DE, FR, Cuneiform} }

// Name is the label of the language in the picker.
// These stay in their own script so a cuneiform page can still be left.
func (l Lang) Name() string {
	switch l {
	case NL:
		return "Nederlands"
	case DE:
		return "Deutsch"
	case FR:
		return "Français"
	case Cuneiform:
		return "Cuneiform"
	default:
		return "English"
	}
}

// Parse accepts the five languages. Anything else is English.
func Parse(s string) Lang {
	switch Lang(strings.ToLower(strings.TrimSpace(s))) {
	case NL, DE, FR, Cuneiform:
		return Lang(strings.ToLower(strings.TrimSpace(s)))
	default:
		return EN
	}
}

// Error is a user-visible failure identified by a catalog key.
type Error struct {
	Key  string
	Args []any
}

func (e Error) Error() string { return T(EN, e.Key, e.Args...) }

// E builds a translatable error.
func E(key string, args ...any) error { return Error{Key: key, Args: args} }

// Text renders err in lang. Unknown errors stay as they are.
func Text(lang Lang, err error) string {
	if err == nil {
		return ""
	}
	var e Error
	if errors.As(err, &e) {
		return T(lang, e.Key, e.Args...)
	}
	return err.Error()
}

// T looks up key and formats it. A missing translation falls back to English,
// then to the key itself. Cuneiform is the formatted English, written in signs.
func T(lang Lang, key string, args ...any) string {
	msg := lookup(lang, key)
	if lang == Cuneiform {
		msg = lookup(EN, key)
	}
	out := msg
	if len(args) > 0 {
		out = fmt.Sprintf(msg, args...)
	}
	if lang == Cuneiform {
		return Transliterate(out)
	}
	return out
}

func lookup(lang Lang, key string) string {
	if lang != EN && lang != Cuneiform {
		if m, ok := catalogs[lang][key]; ok && m != "" {
			return m
		}
	}
	if m, ok := catalogs[EN][key]; ok {
		return m
	}
	return key
}

// FormatDate is "2 January 2006" in the active language.
func FormatDate(lang Lang, t time.Time) string {
	if t.IsZero() {
		return ""
	}
	month := T(lang, fmt.Sprintf("month.%d", int(t.Month())))
	return fmt.Sprintf("%d %s %d", t.Day(), month, t.Year())
}

// MonthShort is the heatmap label, such as "Oct".
func MonthShort(lang Lang, m time.Month) string {
	return T(lang, fmt.Sprintf("month.s.%d", int(m)))
}

// Ago is a short relative time.
func Ago(lang Lang, unix int64) string {
	if unix <= 0 {
		return ""
	}
	d := time.Since(time.Unix(unix, 0))
	if d < time.Minute {
		return T(lang, "ago.just")
	}
	if d < time.Hour {
		n := int(d.Minutes())
		if n < 1 {
			n = 1
		}
		if n == 1 {
			return T(lang, "ago.minute")
		}
		return T(lang, "ago.minutes", n)
	}
	if d < 24*time.Hour {
		n := int(d.Hours())
		if n < 1 {
			n = 1
		}
		if n == 1 {
			return T(lang, "ago.hour")
		}
		return T(lang, "ago.hours", n)
	}
	days := int(d.Hours() / 24)
	if days < 1 {
		days = 1
	}
	if days == 1 {
		return T(lang, "ago.day")
	}
	if days < 14 {
		return T(lang, "ago.days", days)
	}
	if days < 60 {
		n := days / 7
		if n < 1 {
			n = 1
		}
		if n == 1 {
			return T(lang, "ago.week")
		}
		return T(lang, "ago.weeks", n)
	}
	if days < 365 {
		n := days / 30
		if n < 1 {
			n = 1
		}
		if n == 1 {
			return T(lang, "ago.month")
		}
		return T(lang, "ago.months", n)
	}
	n := days / 365
	if n < 1 {
		n = 1
	}
	if n == 1 {
		return T(lang, "ago.year")
	}
	return T(lang, "ago.years", n)
}

// Contrib is the heatmap sentence. year is the twelve-month total;
// otherwise date is the long date of one cell.
func Contrib(lang Lang, n int, date string, year bool) string {
	switch {
	case year && n <= 0:
		return T(lang, "heat.none.year")
	case year && n == 1:
		return T(lang, "heat.one.year")
	case year:
		return T(lang, "heat.many.year", n)
	case n <= 0:
		return T(lang, "heat.none.day", date)
	case n == 1:
		return T(lang, "heat.one.day", date)
	default:
		return T(lang, "heat.many.day", n, date)
	}
}
