namespace XGame.Editor.Build
{
    internal static class BuildCommandExt
    {
        public static void Start(this IBuildCommand cmd)
        {
            if (cmd is ICommandExecuter executer)
            {
                var code = executer.Execute();
                var context = cmd as IBuildContextAdapter;
                BuildLog.Log($"ICommandExecuter Completed. Type:{executer.GetType()} Code:{code}");
                BuildUtils.CmdCompleted(code, context?.Context);
            }
            else if (cmd is ICommandExecuterAsync executerAsync)
            {
                executerAsync.Execute();
            }
            else
            {
                BuildLog.Error($"IBuildCommand Execute Error: No Implemented ICommandExecuter or ICommandExecuterAsync. Type:{cmd?.GetType()}");
                var context = cmd as IBuildContextAdapter;
                BuildUtils.CmdCompleted(BuildErrorCode.CommandExecuteFailed, context?.Context);
            }
        }
        /// <summary>
        /// 异步Command的结束方法
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="cmdAsync"></param>
        /// <param name="code"></param>
        public static void Completed<T>(this T cmdAsync, BuildErrorCode code = BuildErrorCode.CmdCompleted) where T : class, ICommandExecuterAsync
        {
            //if (cmdAsync == null)
            //{
            //    BuildLog.Error($"ICommandExecuterAsync is null. code:{code}");
            //    return;
            //}
            var context = cmdAsync as IBuildContextAdapter;
            //if (context == null)
            //{
            //    BuildLog.Error($"ICommandExecuterAsync Context is null. code:{code}");
            //    return;
            //}
            BuildLog.Log($"ICommandExecuterAsync Completed. Type:{cmdAsync?.GetType()} Code:{code}");
            BuildUtils.CmdCompleted(code, context?.Context);
        }
    }
}
