using System.Globalization;
using Microsoft.Extensions.DependencyInjection;
using ZonyLrcTools.Common.Infrastructure.DependencyInject;
using ZonyLrcTools.Common.Infrastructure.Logging;
using ZonyLrcTools.Common.Infrastructure.Network;
using ZonyLrcTools.Desktop.Infrastructure.Configuration;
using ZonyLrcTools.Desktop.Infrastructure.Localization;
using ZonyLrcTools.Desktop.ViewModels;
namespace ZonyLrcTools.Desktop.Services;
public static class ServiceCollectionExtensions
{
///
/// Register all services for the desktop application.
///
public static IServiceCollection AddDesktopServices(this IServiceCollection services)
{
// Register Common project's auto dependency injection
services.BeginAutoDependencyInject();
services.BeginAutoDependencyInject();
// Configure Common services
// 桌面端使用可写的配置目录,避免只读安装位置导致保存失败。
services.ConfigureDesktopConfiguration();
services.ConfigureLocalization();
services.ConfigureToolService();
// Configure Desktop localization
// Note: Don't set ResourcesPath here because the embedded resource name is
// ZonyLrcTools.Desktop.UIStrings (not ZonyLrcTools.Desktop.Resources.UIStrings)
services.AddLocalization();
services.AddSingleton();
// Register GUI-specific IWarpLogger implementation
services.AddSingleton();
services.AddSingleton(sp => sp.GetRequiredService());
// Register navigation and dialog services
services.AddSingleton();
services.AddSingleton();
services.AddSingleton();
// 保存网易云登录状态。
services.AddSingleton();
// 保存本机酷狗数据库路径。
services.AddSingleton();
// Register ViewModels (Singleton to preserve state across navigation)
services.AddSingleton();
services.AddSingleton();
services.AddSingleton();
services.AddSingleton();
services.AddSingleton();
services.AddSingleton();
return services;
}
///
/// Initialize language settings based on system culture or user preference.
///
public static void InitializeLanguage(string? preferredCulture = null)
{
CultureInfo culture;
if (!string.IsNullOrEmpty(preferredCulture))
{
// Use user's preferred culture
culture = new CultureInfo(preferredCulture);
}
else
{
// Use system culture, fallback to zh-CN if not supported
var systemCulture = CultureInfo.CurrentUICulture.Name;
culture = systemCulture.StartsWith("zh") ? new CultureInfo("zh-CN") :
systemCulture.StartsWith("en") ? new CultureInfo("en-US") :
new CultureInfo("zh-CN");
}
CultureInfo.CurrentCulture = culture;
CultureInfo.CurrentUICulture = culture;
}
}