using System;
using System.Linq;
using System.Runtime.CompilerServices;

namespace LeafWeb.Core.Utility
{
	[AttributeUsage(AttributeTargets.Property)]
	public class ParseInfoAttribute : Attribute
	{
		public int Position { get; private set; }
		public int? BoolEncodedPosition { get; private set; }
		public string Units { get; private set; }
		public string Title { get; private set; }
		public string AlterateTitle { get; private set; }
		public string ExampleValue { get; private set; }
		public string[] FieldNames
		{
			get
			{
				var fieldNames = new[] {Title, Title.SplitCamelCase()};
				if (!string.IsNullOrEmpty(AlterateTitle))
					fieldNames = fieldNames.Concat(new[] { AlterateTitle }).ToArray();
				return fieldNames;
			}
		}

		public ParseInfoAttribute(int position, [CallerMemberName]string title = null, 
			string units = null, string alternateTitle = null, string exampleValue = null,
			int boolEncodedPosition = -1)
		{
			AlterateTitle = alternateTitle;
			ExampleValue = exampleValue;
			Title = title;
			Position = position;
			Units = units;
			BoolEncodedPosition = boolEncodedPosition == -1 ? (int?)null : boolEncodedPosition;
		}
		
		public bool IsTitleMatch(string title)
		{
			return FieldNames.Any(t => string.Compare(title, t, StringComparison.InvariantCultureIgnoreCase) == 0);
		}

		public bool IsPositionMatch(int index)
		{
			return Position == index;
		}
	}
}