using Microsoft.AspNetCore.Components;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Connected.Utilities;
public static class Helper
{
	public static string GetEnumDescription<T>(T EnumVariable)
	{
		string result = string.Empty;

		if (!typeof(T).IsEnum)
			return string.Empty;

		result = EnumVariable.ToString();

		var fieldInfo = EnumVariable.GetType().GetField(EnumVariable.ToString());

		if (fieldInfo != null)
		{
			var attrs = fieldInfo.GetCustomAttributes(typeof(DescriptionAttribute), true);
			if (attrs != null && attrs.Length > 0)
			{
				result = ((DescriptionAttribute)attrs[0]).Description;
			}
		}

		return result;
	}

	public static bool IsTypeNumeric(string type)
	{
		switch (type.ToLower())
		{
			case "long":
			case "ulong":
			case "short":
			case "ushort":
			case "byte":
			case "sbyte":
			case "uint16":
			case "uint32":
			case "uint64":
			case "int16":
			case "int32":
			case "int64":
			case "int":
			case "double":
			case "decimal":
			case "float":
				return true;
			default:
				return false;
		}
	}

	public static int GetDecimalPlaces(object number)
	{
		try
		{
			decimal n = (decimal)Convert.ChangeType(number, TypeCode.Decimal);
			n = Math.Abs(n); //make sure it is positive.
			n -= (int)n;     //remove the integer part of the number.
			var decimalPlaces = 0;
			while (n > 0)
			{
				decimalPlaces++;
				n *= 10;
				n -= (int)n;
			}
			return decimalPlaces;
		}
		catch
		{
			return 0;
		}
	}

	public static bool IsTypeDate(string type)
	{
		switch (type.ToLower())
		{
			case "datetime":
			case "dateonly":
			case "timeonly":
				return true;
			default:
				return false;
		}
	}

	public static bool NumberHasDecimalPlaces(object number)
	{
		if (number == null) return false;
		string type = number.GetType().Name.ToLower();
		if (IsTypeNumeric(type))
		{
			if (!type.Contains("double") && !type.Contains("decimal") && !type.Contains("float")) return false;
			else
			{
				if (type.Equals("double"))
				{
					var doubleVal = (double)Convert.ChangeType(number, TypeCode.Double);
					if (doubleVal % 1 == 0) return false;
					return true;
				}
				if (type.Equals("decimal"))
				{
					var doubleVal = (decimal)Convert.ChangeType(number, TypeCode.Decimal);
					if (doubleVal % 1 == 0) return false;
					return true;
				}
				if (type.Equals("float"))
				{
					var doubleVal = (float)number;
					if (doubleVal % 1 == 0) return false;
					return true;
				}
			}
		}
		return false;
	}

	public static bool IsTypeString(string type)
	{
		switch (type.ToLower())
		{
			case "string":
				return true;
			default:
				return false;
		}
	}

	public static T? ConvertToType<T>(object variable)
	{
		try
		{
			if (variable is not null)
				return (T)Convert.ChangeType(variable, typeof(T));
			return default;
		} catch
		{
			return default;
		}
	}

	public static bool IsNumeric(string input)
	{
		try
		{
			return Double.TryParse(input, out var number);
		}
		catch
		{
			return false;
		}
	}

	public static RenderFragment HtmlString(string raw)
	{
		return builder => {
			builder.AddContent(0, raw);
		};
	}

	public static MarkupString HtmlMarkup(string raw)
	{
		return (MarkupString)raw;
	}

}
