// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

namespace System.ComponentModel.Design.Serialization
{
    /// <summary>
    /// This attribute can be placed on a class to indicate what serialization
    /// object should be used to serialize the class at design time if it is
    /// being used as a root object.
    /// </summary>
    [AttributeUsage(AttributeTargets.Class | AttributeTargets.Interface, AllowMultiple = true, Inherited = true)]
    [Obsolete("RootDesignerSerializerAttribute has been deprecated. Use DesignerSerializerAttribute instead. For example, to specify a root designer for CodeDom, use DesignerSerializerAttribute(...,typeof(TypeCodeDomSerializer)) instead.")]
    public sealed class RootDesignerSerializerAttribute : Attribute
    {
        private string? _typeId;

        /// <summary>
        /// Creates a new designer serialization attribute.
        /// </summary>
        public RootDesignerSerializerAttribute(Type serializerType, Type baseSerializerType, bool reloadable)
        {
            ArgumentNullException.ThrowIfNull(serializerType);
            ArgumentNullException.ThrowIfNull(baseSerializerType);

            SerializerTypeName = serializerType.AssemblyQualifiedName;
            SerializerBaseTypeName = baseSerializerType.AssemblyQualifiedName;
            Reloadable = reloadable;
        }

        /// <summary>
        /// Creates a new designer serialization attribute.
        /// </summary>
        public RootDesignerSerializerAttribute(string serializerTypeName, Type baseSerializerType, bool reloadable)
        {
            ArgumentNullException.ThrowIfNull(baseSerializerType);

            SerializerTypeName = serializerTypeName;
            SerializerBaseTypeName = baseSerializerType.AssemblyQualifiedName;
            Reloadable = reloadable;
        }

        /// <summary>
        /// Creates a new designer serialization attribute.
        /// </summary>
        public RootDesignerSerializerAttribute(string? serializerTypeName, string? baseSerializerTypeName, bool reloadable)
        {
            SerializerTypeName = serializerTypeName;
            SerializerBaseTypeName = baseSerializerTypeName;
            Reloadable = reloadable;
        }

        /// <summary>
        /// Indicates that this root serializer supports reloading. If false, the design document
        /// will not automatically perform a reload on behalf of the user. It will be the user's
        /// responsibility to reload the document themselves.
        /// </summary>
        public bool Reloadable { get; }

        /// <summary>
        /// Retrieves the fully qualified type name of the serializer.
        /// </summary>
        public string? SerializerTypeName { get; }

        /// <summary>
        /// Retrieves the fully qualified type name of the serializer base type.
        /// </summary>
        public string? SerializerBaseTypeName { get; }

        /// <summary>
        /// This defines a unique ID for this attribute type. It is used
        /// by filtering algorithms to identify two attributes that are
        /// the same type. For most attributes, this just returns the
        /// Type instance for the attribute. EditorAttribute overrides
        /// this to include the type of the editor base type.
        /// </summary>
        public override object TypeId
        {
            get
            {
                if (_typeId == null)
                {
                    ReadOnlySpan<char> baseType = SerializerBaseTypeName;
                    int comma = baseType.IndexOf(',');
                    if (comma >= 0)
                    {
                        baseType = baseType.Slice(0, comma);
                    }
                    _typeId = string.Concat(GetType().FullName, baseType);
                }
                return _typeId;
            }
        }
    }
}
