using Microsoft.Maui.ApplicationModel;
using System;
using System.Reflection;

namespace TodoList.Maui.Services;

public static class AppMetadata
{
	private const string AppNameText = "\u5F85\u529E\u4E8B\u9879";

	public static string AppName => AppNameText;

	public static string? GetDisplayVersion()
	{
		// 优先使用Assembly版本
		var asmVersion = Assembly.GetExecutingAssembly().GetName().Version;
		if (asmVersion != null)
		{
			// 只返回主版本.次版本.修订版本 (如: 1.0.4)
			return $"{asmVersion.Major}.{asmVersion.Minor}.{asmVersion.Build}";
		}

		// 回退到AppInfo
		var versionString = AppInfo.Current.VersionString?.Trim();
		if (string.IsNullOrWhiteSpace(versionString))
		{
			return null;
		}

		if (!Version.TryParse(versionString, out var parsed))
		{
			return null;
		}

		return $"{parsed.Major}.{parsed.Minor}.{parsed.Build}";
	}

	public static string GetDisplayTitle()
	{
		var version = GetDisplayVersion();
		return string.IsNullOrWhiteSpace(version) ? AppName : $"{AppName} v{version}";
	}

	public static string GetTitleBarVersionText()
	{
		var version = GetDisplayVersion();
		return string.IsNullOrWhiteSpace(version) ? AppNameText : $"{AppNameText} v{version}";
	}

	public static string GetWindowTitle()
	{
		return GetTitleBarVersionText();
	}

	public static string GetTrayTooltipText()
	{
		var text = GetDisplayTitle();
		return text.Length > 63 ? text[..63] : text;
	}
}
