using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace WebAPIBase.NetCore.BusinessCore
{
    public sealed class EntityDataManager
    {
        private static Hashtable m_entitys = new Hashtable();

        private EntityDataManager()
        {
        }

        public static EntityData GetEmptyEntity(string className)
        {
            if (m_entitys.Contains(className))
            {
                return ((EntityData)m_entitys[className]).CloneData();
            }
            try
            {
                string classBuilderName = ClassBuilderManager.GetClassBuilderName(className);
                IClassBuilder classBuilder = ClassBuilderFactory.GetClassBuilder(classBuilderName);
                EntityData entityData = classBuilder.BuildClass(className);
                lock (m_entitys)
                {
                    if (!m_entitys.Contains(className))
                    {
                        m_entitys.Add(className, entityData);
                    }
                }
                return entityData.CloneData();
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public static void CloneEntityStruct(EntityData entity, string className)
        {
            try
            {
                EntityData entityData = null;
                if (m_entitys.Contains(className))
                {
                    entityData = (EntityData)m_entitys[className];
                }
                else
                {
                    string classBuilderName = ClassBuilderManager.GetClassBuilderName(className);
                    IClassBuilder classBuilder = ClassBuilderFactory.GetClassBuilder(classBuilderName);
                    entityData = classBuilder.BuildClass(className);
                    lock (m_entitys)
                    {
                        if (!m_entitys.Contains(className))
                        {
                            m_entitys.Add(className, entityData);
                        }
                    }
                }
                entityData.CloneDataStucture(entity);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
    }
}
