using System;
using UnityEditor;
using UnityEngine;

namespace XGame.Editor.Nodes
{
    public class NodeCodeGen : CodeGenerator
    {
        public bool Gen(string nodeName, string componentName, string nodeGroupName)
        {
            if (string.IsNullOrEmpty(nodeName))
            {
                EditorUtility.DisplayDialog("代码生成错误", "Node名字不能为空!", "确认");
                return false;
            }
            if (string.IsNullOrEmpty(componentName))
            {
                EditorUtility.DisplayDialog("代码生成错误", "Component名字不能为空!", "确认");
                return false;
            }
            if (string.IsNullOrEmpty(nodeGroupName))
            {
                EditorUtility.DisplayDialog("代码生成错误", "NodeGroup不能为空!", "确认");
                return false;
            }
            try
            {
                InitPath("Nodes");
                if (!GenNode(nodeName, componentName, nodeGroupName))
                {
                    return false;
                }
                if (!GenCommonCode(nodeName, componentName, componentName + "Component", "NodeComponentTemplate"))
                {
                    return false;
                }
                AssetDatabase.SaveAssets();
                AssetDatabase.Refresh();
            }
            catch (Exception ex)
            {
                Debug.LogException(ex);
                return false;
            }
            return true;
        }
        private bool GenNode(string nodeName, string componentName, string nodeGroupName)
        {
            return GenCommonCode(nodeName, componentName, nodeName + "Node", "NodeTemplate", codeText =>
            {
                return codeText.Replace("<#NodeGroupName>", nodeGroupName);
            });
        }
    }
}
