using System.ComponentModel;
using System.Reflection;

namespace AyCode.Core.Serializers.Toons;

public static partial class AcToonSerializer
{
    /// <summary>
    /// Resolve [#...] placeholders in type description string.
    /// </summary>
    private static string ResolveTypeDescriptionPlaceholders(string template, Type type)
    {
        var result = template;

        // [#Description] → Microsoft [Description]
        if (result.Contains("[#Description]"))
        {
            var msDesc = type.GetCustomAttribute<System.ComponentModel.DescriptionAttribute>();
            var value = msDesc?.Description ?? "";
            result = result.Replace("[#Description]", value);
        }

        // [#DisplayName] → Microsoft [DisplayName]
        if (result.Contains("[#DisplayName]"))
        {
            var displayName = type.GetCustomAttribute<System.ComponentModel.DisplayNameAttribute>();
            var value = displayName?.DisplayName ?? type.Name;
            result = result.Replace("[#DisplayName]", value);
        }

        // [#SmartDescription] → Smart inference
        if (result.Contains("[#SmartDescription]"))
        {
            var value = GetTypeDescription(type);
            result = result.Replace("[#SmartDescription]", value);
        }

        return CleanupPlaceholders(result);
    }

    /// <summary>
    /// Resolve [#...] placeholders in type purpose string.
    /// </summary>
    private static string ResolveTypePurposePlaceholders(string template, Type type)
    {
        var result = template;

        // [#SmartPurpose] → Would be empty for classes, so just remove it
        if (result.Contains("[#SmartPurpose]"))
        {
            result = result.Replace("[#SmartPurpose]", "");
        }

        return CleanupPlaceholders(result);
    }
}
