using System.Collections.Generic;
using System.Numerics;

namespace Set.Data
{
    public class TraitUtils
    {
        public static HashSet<TraitsEnum> FromInt(int traits)
        {
            HashSet<TraitsEnum> result = new HashSet<TraitsEnum>();
            foreach (TraitsEnum trait in System.Enum.GetValues(typeof(TraitsEnum)))
            {
                if ((traits & (int)trait) != 0)
                {
                    result.Add(trait);
                }
            }
            return result;
        }

        public static int ToInt(HashSet<TraitsEnum> traits)
        {
            int result = 0;
            foreach (TraitsEnum trait in traits)
            {
                result |= (int)trait;
            }
            return result;
        }

        public static int TraitCountFromInt(int traits)
        {
            int count = 0;
            while (traits != 0)
            {
                count += traits & 1;
                traits >>= 1;
            }
            return count;
        }

        public static bool ContainsTrait(int traits, TraitsEnum trait)
        {
            return (traits & (int)trait) != 0;
        }
        public static bool ContainsTrait(int traits, int trait)
        {
            return (traits & trait) != 0;
        }
        
    }
}
