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

using System;
using System.Linq;

namespace Microsoft.AspNetCore.Razor.Language.Intermediate
{
	public sealed class HtmlAttributeIntermediateNode : IntermediateNode
	{
		public override IntermediateNodeCollection Children { get; } = new IntermediateNodeCollection();

		public CSharpExpressionIntermediateNode AttributeNameExpression { get; set; }

		public string AttributeName { get; set; }

		public string Prefix { get; set; }

		public string Suffix { get; set; }

		public string EventUpdatesAttributeName { get; set; }

		public override void Accept( IntermediateNodeVisitor visitor )
		{
			if ( visitor == null )
			{
				throw new ArgumentNullException( nameof( visitor ) );
			}

			visitor.VisitHtmlAttribute( this );
		}

		public override void FormatNode( IntermediateNodeFormatter formatter )
		{
			formatter.WriteContent( AttributeName );

			formatter.WriteProperty( nameof( AttributeName ), AttributeName );
			formatter.WriteProperty( nameof( AttributeNameExpression ), string.Join( string.Empty, AttributeNameExpression?.FindDescendantNodes<IntermediateToken>().Select( n => n.Content ) ?? Array.Empty<string>() ) );
			formatter.WriteProperty( nameof( Prefix ), Prefix );
			formatter.WriteProperty( nameof( Suffix ), Suffix );
			formatter.WriteProperty( nameof( EventUpdatesAttributeName ), EventUpdatesAttributeName );
		}


		/// <summary>
		/// Does this attreibute look like it should be treated differently
		/// </summary>
		public bool LooksLikeNonHtml
		{
			get
			{
				var c = AttributeName[0];
				if ( c == '@' ) return true;
				if ( char.IsUpper( c ) ) return true;

				return false;
			}
		}
	}
}
