﻿using System;
using TSGenHD.TypeScript;

namespace TSGenHD.Factories
{
    public class EnumFactory
    {
        public static TsType CreateEnum(TsType parent, Type type)
        {
            var typeName = type.Name;
            var newEnum = new TypeScript.Enum(parent, typeName);
            var names = System.Enum.GetNames(type);
            var typeCode = Type.GetTypeCode(System.Enum.GetUnderlyingType(type));

            foreach (var enumName in names)
            {
                var val = System.Enum.Parse(type, enumName);
                var i = Convert.ChangeType(val, typeCode);
                newEnum.Values.Add(new EnumValue(enumName, Convert.ToInt64(i)));
            }

            return newEnum;
        } 
    }
}