using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace wijmoSample1.Models.FileExplorer
{
	public static class Utils
	{
		public static TEnum SafeParseEnum<TEnum>(this string strValue) where TEnum : struct, System.IConvertible
		{
			if (!typeof(TEnum).IsEnum)
			{
				throw new System.ArgumentException("T must be an enumerated type");
			}

			try
			{
				return (TEnum)System.Enum.Parse(typeof(TEnum), strValue, true);
			}
			catch (System.Exception)
			{
				return default(TEnum);
			}
		}

		public static string[] SafeParseStrings(this string strValue)
		{
			return string.IsNullOrEmpty(strValue)
				? new string[] { }
				: strValue.Split(',').Where(s => !string.IsNullOrEmpty(s)).ToArray();
		}

		public static bool SafeParseBool(this string strValue, bool defaultValue = false)
		{
			bool value;
			return bool.TryParse(strValue, out value) ? value : defaultValue;
		}

		public static int SafeParseInt(this string strValue, int defaultValue = 0)
		{
			int value;
			return int.TryParse(strValue, out value) ? value : defaultValue;
		}
	}
}