using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Threading.Tasks;
using XYY.Core.Standard.Mvc;

namespace XYY.Api.Finance.Charges.Controllers
{
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class EnumsController : ApiControllerBase
    {
        public async Task<IActionResult> Get(string enumName)
        {
            var enumList = GetEnumItems(enumName);
            return Ok(enumList);
        }


        public static List<EnumItem2> GetEnumItems(string enumName)
        {
            List<EnumItem2> enumItems = new List<EnumItem2>();
            string fileName = System.IO.Path.Combine(AppContext.BaseDirectory, "XYY.Api.Finance.Charges.dll");
            var ass = Assembly.LoadFile(fileName);
            var t = ass.GetType("XYY.DS.Common." + enumName);
            if (t != null)
            {
                if (t.IsEnum)
                {
                    var values = t.GetEnumValues();
                    foreach (var v in values)
                    {
                        enumItems.Add(new EnumItem2
                        {
                            Id = (int)v,
                            Name = t.GetEnumName(v)
                        });
                    }
                }
            }
            return enumItems;
        }
    }
}
