using System;
using System.Text;
using Agebull.EntityModel.Config;

namespace Agebull.EntityModel.RobotCoder
{
    /// <summary>
    /// ´úÂëÆ¬¶Ï»ùÀà
    /// </summary>
    public class MomentCoderBase : CoderBase
    {
        #region ´úÂëÉú³É´úÀí½Ó¿Ú
        
        /// <summary>
        /// Ö´ÐÐÄ¿±êµÄ¶¯×÷
        /// </summary>
        /// <param name="config"></param>
        /// <param name="coder"></param>
        /// <returns></returns>
        public static string CreateCode<TConfig>(ConfigBase config, Func<TConfig, string> coder)
            where TConfig : ConfigBase
        {
            StringBuilder code = new StringBuilder();

            using (CodeGeneratorScope.CreateScope(GlobalConfig.CurrentSolution))
            {
                config.Foreach<TConfig>(arg =>
                {
                    code.AppendLine(coder(arg));
                });
            }

            return code.ToString();
        }

        /// <summary>
        /// Ö´ÐÐÄ¿±êµÄ¶¯×÷
        /// </summary>
        /// <param name="config"></param>
        /// <param name="condition"></param>
        /// <param name="coder"></param>
        /// <returns></returns>
        public static string CreateCode<TConfig>(ConfigBase config, Func<TConfig, bool> condition, Func<TConfig, string> coder)
            where TConfig : ConfigBase
        {
            StringBuilder code = new StringBuilder();

            using (CodeGeneratorScope.CreateScope(GlobalConfig.CurrentSolution))
            {
                config.Foreach(condition, arg =>
                {
                    code.AppendLine(coder(arg));
                });
            }

            return code.ToString();
        }
        #endregion

    }
}