using System;

namespace KitsuneCafe.Extension
{
    public static class CollectionExtension
    {
        public static bool IsTypeOf<T, U>()
        {
            return IsTypeOf(typeof(T), typeof(U));
        }

        public static bool IsTypeOf<T>(this object value)
        {
            return IsTypeOf(value.GetType(), typeof(T));
        }

        public static bool IsTypeOf(this object value, Type type)
        {
            return IsTypeOf(value.GetType(), type);
        }

        public static bool IsTypeOf<T>(this Type type)
        {
            if (type == null) { return false; }

            return IsTypeOf(type, typeof(T));
        }

        public static bool IsTypeOf(this Type type, Type other)
        {
            if (type == null || other == null || !other.IsGenericTypeDefinition)
            {
                return false;
            }

            return other.IsGenericType && other.GetGenericTypeDefinition() == type;
        }

        public static Type GetGenericArgument(this object value)
        {
            if (value == null) { return null; }
            var type = value.GetType();
            var args = type.GetGenericArguments();
            return args.Length > 0 ? args[0] : null;
        }
    }
}
