namespace Billing.Languages
{
    public partial class PlatformCulture
    {
        public string PlatformString { get; set; }
        public string LanguageCode { get; set; }
        public string LocaleCode { get; set; }

        public string Language => string.IsNullOrEmpty(LocaleCode) ? LanguageCode : LanguageCode + "-" + LocaleCode;

        public partial string GetNamespace();
        public partial void Init();

        public PlatformCulture()
        {
            Init();
        }

        private void Init(string cultureString) 
        {
            if (string.IsNullOrEmpty(cultureString))
            {
                cultureString = "zh-CN";
            }

            PlatformString = cultureString.Replace('_', '-');
            if (PlatformString.Contains('-'))
            {
                var parts = PlatformString.Split('-');
                LanguageCode = parts[0];
                LocaleCode = parts[^1];
            }
            else
            {
                LanguageCode = PlatformString;
                LocaleCode = string.Empty;
            }
        }

        private string ToDotnetFallbackLanguage()
        {
            string netLanguage = LanguageCode switch
            {
                // fallback to Portuguese (Portugal)
                "pt" => "pt-PT",
                // equivalent to German (Switzerland) for this app
                "gsw" => "de-CH",

                // add more application-specific cases here (if required)
                // ONLY use cultures that have been tested and known to work

                // use the first part of the identifier (two chars, usually);
                _ => LanguageCode,
            };
            Helper.Debug($".NET Fallback Language/Locale: {LanguageCode} to {netLanguage} (application-specific)");
            return netLanguage;
        }

        public override string ToString() => PlatformString;
    }
}
