using System;

namespace XGame.Framework
{
    internal static class GameLogicCreator
    {
        public static IGameLogicProxy Create()
        {
            var config = XGameConfig.Load();
            var gameLogicTypeName = config?.gameLogicTypeName ?? string.Empty;
            XGameConfig.Unload(config);
            var gameLogic = CreateGameLogic(gameLogicTypeName);
            var proxy = new CLRGameLogicProxy(gameLogic);
            proxy.Init();
            return proxy;
        }
        private static IGameLogic CreateGameLogic(string gameLogicTypeName)
        {
            if (string.IsNullOrEmpty(gameLogicTypeName))
            {
                Log.Error($"GameLogic 名字为空. Name:{gameLogicTypeName}");
                return null;
            }
            var type = Type.GetType(gameLogicTypeName);
            if (type == null)
            {
                Log.Error($"GameLogic Type为空. Name:{gameLogicTypeName}");
                return null;
            }
            var instance = Activator.CreateInstance(type) as IGameLogic;
            if (instance == null)
            {
                Log.Error($"GameLogic 实例化失败. Name:{gameLogicTypeName}");
            }
            return instance;
        }
    }
}
