package tui

import (
	"strings"

	"gitgud.io/mike/mpv-manager/pkg/locale"
)

// LanguageOptionToMajorLangItem converts a locale.LanguageOption to TUI majorLangItem
// This requires original locales array to look up full LocaleEntry
func LanguageOptionToMajorLangItem(opt locale.LanguageOption, locales []locale.LocaleEntry) majorLangItem {
	// Extract language code from Locale (e.g., "ja" from "ja-JP")
	// LanguageCode field should be set in LanguageOption, but fallback to Locale parsing
	langCode := extractLanguageCode(opt.Locale)

	// Look up full LocaleEntry
	entry := locale.FindByLanguageCode(langCode, locales)

	// Create a synthetic entry if not found in locales array
	if entry == nil {
		entry = &locale.LocaleEntry{
			LanguageName:  opt.LanguageName,
			LanguageLocal: opt.LanguageLocal,
			LanguageCode:  langCode,
			Regions:       []locale.RegionEntry{},
		}
	}

	return majorLangItem{entry: entry}
}

// LanguageOptionToRegionLangItem converts a locale.LanguageOption to TUI regionLangItem
// This requires original locales array to look up full LocaleEntry
func LanguageOptionToRegionLangItem(opt locale.LanguageOption, locales []locale.LocaleEntry) regionLangItem {
	// Extract language code from Locale
	langCode := extractLanguageCode(opt.Locale)

	// Look up full LocaleEntry
	entry := locale.FindByLanguageCode(langCode, locales)

	// Create a synthetic entry if not found
	if entry == nil {
		entry = &locale.LocaleEntry{
			LanguageName:  opt.LanguageName,
			LanguageLocal: opt.LanguageLocal,
			LanguageCode:  langCode,
			Regions:       []locale.RegionEntry{},
		}
	}

	// Handle major language without regions (use LanguageCode as Locale)
	// If CountryCode is empty, this is a major language without regional variants
	if opt.CountryCode == "" {
		return regionLangItem{
			entry:   entry,
			region:  nil,
			isMajor: true,
		}
	}

	// Create RegionEntry directly from LanguageOption data (no need to look up again)
	region := &locale.RegionEntry{
		Locale:               opt.Locale,
		CountryName:          opt.CountryName,
		CountryLocal:         opt.CountryLocal, // Use correct CountryLocal field from LanguageOption!
		CountryCode:          opt.CountryCode,
		Flag:                 opt.Flag, // Use correct Flag field from LanguageOption!
		MajorRegionalVariant: opt.MajorRegionalVariant,
		Population:           opt.Population,
	}

	// Determine flag from locale string if not provided in LanguageOption
	if region.Flag == "" {
		flag := locale.GetFlagForLanguageCode(langCode)
		region.Flag = flag
	}

	return regionLangItem{
		entry:   entry,
		region:  region,
		isMajor: false,
	}
}

// ConvertLanguageOptionsToMajorItems converts multiple LanguageOptions to majorLangItems
func ConvertLanguageOptionsToMajorItems(opts []locale.LanguageOption, locales []locale.LocaleEntry) []majorLangItem {
	items := make([]majorLangItem, len(opts))
	for i, opt := range opts {
		items[i] = LanguageOptionToMajorLangItem(opt, locales)
	}
	return items
}

// ConvertLanguageOptionsToRegionItems converts multiple LanguageOptions to regionLangItems
func ConvertLanguageOptionsToRegionItems(opts []locale.LanguageOption, locales []locale.LocaleEntry) []regionLangItem {
	items := make([]regionLangItem, len(opts))
	for i, opt := range opts {
		items[i] = LanguageOptionToRegionLangItem(opt, locales)
	}
	return items
}

// extractLanguageCode extracts the base language code from a locale string
// e.g., "en-US" -> "en", "ja-JP" -> "ja", "zh-Hans-CN" -> "zh"
func extractLanguageCode(localeStr string) string {
	if localeStr == "" {
		return ""
	}

	parts := strings.Split(localeStr, "-")
	if len(parts) > 0 {
		return parts[0]
	}

	return localeStr
}
