using System;
using System.Collections.Generic;
using System.Linq;
using Swan.Collections;
namespace Swan {
///
/// Provide Enumerations helpers with internal cache.
///
public class EnumHelper
: SingletonBase>> {
///
/// Gets all the names and enumerators from a specific Enum type.
///
/// The type of the attribute to be retrieved.
/// A tuple of enumerator names and their value stored for the specified type.
public static IEnumerable> Retrieve() where T : struct, IConvertible => Instance.Retrieve(typeof(T), t => Enum.GetValues(t).Cast().Select(item => Tuple.Create(Enum.GetName(t, item), item)));
///
/// Gets the cached items with the enum item value.
///
/// The type of enumeration.
/// if set to true [humanize].
///
/// A collection of Type/Tuple pairs
/// that represents items with the enum item value.
///
public static IEnumerable> GetItemsWithValue(Boolean humanize = true) where T : struct, IConvertible => Retrieve().Select(x => Tuple.Create((Int32)x.Item2, humanize ? x.Item1.Humanize() : x.Item1));
///
/// Gets the flag values.
///
/// The type of the enum.
/// The value.
/// if set to true [ignore zero].
///
/// A list of values in the flag.
///
public static IEnumerable GetFlagValues(Int32 value, Boolean ignoreZero = false) where TEnum : struct, IConvertible => Retrieve().Select(x => (Int32)x.Item2).When(() => ignoreZero, q => q.Where(f => f != 0)).Where(x => (x & value) == x);
///
/// Gets the flag values.
///
/// The type of the enum.
/// The value.
/// if set to true [ignore zero].
///
/// A list of values in the flag.
///
public static IEnumerable GetFlagValues(Int64 value, Boolean ignoreZero = false) where TEnum : struct, IConvertible => Retrieve().Select(x => (Int64)x.Item2).When(() => ignoreZero, q => q.Where(f => f != 0)).Where(x => (x & value) == x);
///
/// Gets the flag values.
///
/// The type of the enum.
/// The value.
/// if set to true [ignore zero].
///
/// A list of values in the flag.
///
public static IEnumerable GetFlagValues(Byte value, Boolean ignoreZero = false) where TEnum : struct, IConvertible => Retrieve().Select(x => (Byte)x.Item2).When(() => ignoreZero, q => q.Where(f => f != 0)).Where(x => (x & value) == x);
///
/// Gets the flag names.
///
/// The type of the enum.
/// the value.
/// if set to true [ignore zero].
/// if set to true [humanize].
///
/// A list of flag names.
///
public static IEnumerable GetFlagNames(Int32 value, Boolean ignoreZero = false, Boolean humanize = true) where TEnum : struct, IConvertible => Retrieve().When(() => ignoreZero, q => q.Where(f => (Int32)f.Item2 != 0)).Where(x => ((Int32)x.Item2 & value) == (Int32)x.Item2).Select(x => humanize ? x.Item1.Humanize() : x.Item1);
///
/// Gets the flag names.
///
/// The type of the enum.
/// The value.
/// if set to true [ignore zero].
/// if set to true [humanize].
///
/// A list of flag names.
///
public static IEnumerable GetFlagNames(Int64 value, Boolean ignoreZero = false, Boolean humanize = true) where TEnum : struct, IConvertible => Retrieve().When(() => ignoreZero, q => q.Where(f => (Int64)f.Item2 != 0)).Where(x => ((Int64)x.Item2 & value) == (Int64)x.Item2).Select(x => humanize ? x.Item1.Humanize() : x.Item1);
///
/// Gets the flag names.
///
/// The type of the enum.
/// The value.
/// if set to true [ignore zero].
/// if set to true [humanize].
///
/// A list of flag names.
///
public static IEnumerable GetFlagNames(Byte value, Boolean ignoreZero = false, Boolean humanize = true) where TEnum : struct, IConvertible => Retrieve().When(() => ignoreZero, q => q.Where(f => (Byte)f.Item2 != 0)).Where(x => ((Byte)x.Item2 & value) == (Byte)x.Item2).Select(x => humanize ? x.Item1.Humanize() : x.Item1);
///
/// Gets the cached items with the enum item index.
///
/// The type of enumeration.
/// if set to true [humanize].
///
/// A collection of Type/Tuple pairs that represents items with the enum item value.
///
public static IEnumerable> GetItemsWithIndex(Boolean humanize = true) where T : struct, IConvertible {
Int32 i = 0;
return Retrieve().Select(x => Tuple.Create(i++, humanize ? x.Item1.Humanize() : x.Item1));
}
}
}