//
//  AttributeInfoExtensions.cs
//
//  Copyright (c) František Boháček. All rights reserved.
//  Licensed under the MIT license. See LICENSE file in the project root for full license information.
using System.Diagnostics;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using NosSmooth.PacketSerializersGenerator.Data;
namespace NosSmooth.PacketSerializersGenerator.Extensions;
/// 
/// Extension methods for .
/// 
public static class AttributeInfoExtensions
{
    /// 
    /// Get value of a named parameter.
    /// 
    /// The attribute information.
    /// The name of the parameter.
    /// The default value to return if not found.
    /// The value type.
    /// The value of the attribute.
    public static TValue? GetNamedValue(this AttributeInfo attributeInfo, string name, TValue? @default)
    {
        if (attributeInfo.NamedAttributeArguments.TryGetValue(name, out var value))
        {
            if (typeof(TValue) == typeof(string))
            {
                return (TValue?)(object?)value.VisualValue;
            }
            return (TValue?)value.RealValue;
        }
        return @default;
    }
    /// 
    /// Get value of a named parameter.
    /// 
    /// The attribute information.
    /// The index of the parameter.
    /// The value type.
    /// The value of the attribute.
    public static TValue? GetIndexedValue(this AttributeInfo attributeInfo, int index)
    {
        var value = attributeInfo.IndexedAttributeArguments[index];
        if (typeof(TValue) == typeof(string))
        {
            return (TValue?)(object?)value?.VisualValue;
        }
        return (TValue?)value.RealValue;
    }
    /// 
    /// Gets visual values of params parameters in the constructor.
    /// 
    /// The attribute info.
    /// The index the values start at.
    /// A list containing all the values.
    public static IReadOnlyList GetParamsVisualValues(this AttributeInfo attributeInfo, int startingIndex)
    {
        if (attributeInfo.IndexedAttributeArguments.Count - 1 < startingIndex)
        {
            return Array.Empty();
        }
        if (attributeInfo.IndexedAttributeArguments[startingIndex].IsArray)
        {
            return attributeInfo.IndexedAttributeArguments[startingIndex].GetArrayVisualValues();
        }
        return attributeInfo.IndexedAttributeArguments
            .Skip(startingIndex)
            .Select(x => x.VisualValue)
            .ToArray();
    }
    /// 
    /// Gets the visual values of the array.
    /// 
    /// The attribute argument.
    /// The list of the elements.
    public static IReadOnlyList GetArrayVisualValues(this AttributeArgumentInfo attributeArgumentInfo)
    {
        var arrayCreation = attributeArgumentInfo.Argument.Expression as ArrayCreationExpressionSyntax;
        if (arrayCreation is null)
        {
            throw new ArgumentException($"The given attribute argument is not an array creation.", nameof(attributeArgumentInfo));
        }
        if (arrayCreation.Initializer is null)
        {
            return Array.Empty();
        }
        return arrayCreation.Initializer.Expressions
            .Select(x => x.ToString())
            .ToArray();
    }
}