using System.Diagnostics;
using Hotline.FlowEngine.WorkflowModules;
using Hotline.Share.Dtos;

namespace Hotline.SeedData.Codes
{
    /// <summary>
    /// 编码
    /// </summary>
    public interface ICode
    {
        IReadOnlyList<Kv> GetAll<TThis>();
    }

    public interface ICode<TTarget> : ICode where TTarget : class
    {
        IReadOnlyList<TTarget> GetAll(Func<Kv, TTarget> construct);
    }

    public class ConstsHelper
    {
        public static IEnumerable<Kv> GetAll<TSource>(TSource source)
        {
            var fields = typeof(TSource).GetFields();
            foreach (var fieldInfo in fields)
            {
                var value = fieldInfo.GetValue(source);
                if (value is Kv idname)
                {
                    yield return idname;
                }
            }
        }

        public static IEnumerable<TDest> GetAll<TSource, TDest>(TSource source, Func<Kv, TDest> construct)
        {
            var idnames = GetAll(source);
            foreach (var idname in idnames)
            {
                yield return construct(idname);
            }
        }
    }

    public class TestDemo : IConstTable<WorkflowModule>
    {
        public static readonly KeyValuePair<string, string> OrderManage = new("OrderManage", "工单办理");
        public static readonly Kv OrderManage1 = new Kv("OrderManage1", "工单办理1");
        public IEnumerable<WorkflowModule> GetData<TSource>(TSource source) =>
            ConstsHelper.GetAll(new TestDemo(), d => new WorkflowModule(d.Key, d.Value));
    }

    public interface IConstTable<TEntity>
    {
        IEnumerable<TEntity> GetData<TSource>(TSource source);
    }

    public interface IConstManager
    {
        //todo 1. list to db 2. single to db

    }
}
