package api

import "testing"

func TestNormalizeSubtitleLanguage(t *testing.T) {
	cases := map[string]string{
		"ita":   "it",
		"ITA":   "it",
		" it ":  "it",
		"fre":   "fr",
		"fra":   "fr",
		"pt-BR": "pt",
		"zho":   "zh",
		"":      "",
		// Unknown codes survive as themselves so an exact match still works for a
		// language the catalogue has no entry for.
		"klingon": "klingon",
	}
	for input, want := range cases {
		if got := normalizeSubtitleLanguage(input); got != want {
			t.Fatalf("normalizeSubtitleLanguage(%q) = %q, want %q", input, got, want)
		}
	}
}

func TestSubtitleLanguageOptionsAreLegalPreferenceValues(t *testing.T) {
	definition, ok := preferenceDefinitionFor("subtitleLanguage")
	if !ok {
		t.Fatal("subtitleLanguage is missing from the catalogue")
	}
	if got := normalizePreference(definition, "it"); got != "it" {
		t.Fatalf("Italian was rejected by the catalogue: %v", got)
	}
	if got := normalizePreference(definition, "nonsense"); got != subtitleLanguageAuto {
		t.Fatalf("an illegal language became %v, want the default", got)
	}
	if got := normalizePreferences(map[string]any{})["subtitlesEnabled"]; got != true {
		t.Fatalf("subtitlesEnabled defaulted to %v, want true", got)
	}
}

func TestSelectSubtitleOffMeansOff(t *testing.T) {
	subtitles := []playableSubtitle{{ID: "1", Language: "eng", IsDefault: true}}
	if got := selectSubtitle(subtitles, false, "en"); got != "" {
		t.Fatalf("selected %q with subtitles turned off", got)
	}
}

func TestSelectSubtitlePrefersChosenLanguage(t *testing.T) {
	subtitles := []playableSubtitle{
		{ID: "1", Language: "eng", IsDefault: true},
		{ID: "2", Language: "ita", IsForced: true},
		{ID: "3", Language: "ita", IsHearingImpaired: true},
		{ID: "4", Language: "ita"},
	}
	// The full Italian track, not the forced one that comes first and not the SDH one.
	if got := selectSubtitle(subtitles, true, "it"); got != "4" {
		t.Fatalf("selectSubtitle picked %q, want the full Italian track", got)
	}
}

func TestSelectSubtitleMissingLanguageFallsBackToForcedOnly(t *testing.T) {
	withForced := []playableSubtitle{
		{ID: "1", Language: "eng", IsDefault: true},
		{ID: "2", Language: "eng", IsForced: true},
	}
	if got := selectSubtitle(withForced, true, "it"); got != "2" {
		t.Fatalf("selectSubtitle picked %q, want the forced track", got)
	}
	// No forced track and no Italian: nothing, rather than English the viewer never asked
	// for.
	withoutForced := []playableSubtitle{{ID: "1", Language: "eng", IsDefault: true}}
	if got := selectSubtitle(withoutForced, true, "it"); got != "" {
		t.Fatalf("selectSubtitle picked %q, want nothing", got)
	}
}

func TestSelectSubtitleAutomaticKeepsFlagOrder(t *testing.T) {
	subtitles := []playableSubtitle{
		{ID: "1", Language: "eng"},
		{ID: "2", Language: "eng", IsDefault: true},
		{ID: "3", Language: "eng", IsForced: true},
	}
	if got := selectSubtitle(subtitles, true, subtitleLanguageAuto); got != "3" {
		t.Fatalf("automatic picked %q, want the forced track", got)
	}
	if got := selectSubtitle(subtitles[:2], true, ""); got != "2" {
		t.Fatalf("automatic picked %q, want the default track", got)
	}
	if got := selectSubtitle(subtitles[:1], true, subtitleLanguageAuto); got != "1" {
		t.Fatalf("automatic picked %q, want the only track", got)
	}
	if got := selectSubtitle(nil, true, subtitleLanguageAuto); got != "" {
		t.Fatalf("automatic picked %q with no tracks", got)
	}
}
