using System;
using UnityEditor;
using UnityEngine;
using XGame.Framework.Map;
using XGame.Framework.ObjectCollection;

namespace XGame.Editor.Map
{
    internal class MapViewCodeGen : CodeGenerator
    {
        public bool Gen(MapBehaviour behaviour, string componentName)
        {
            if (string.IsNullOrEmpty(componentName))
            {
                EditorUtility.DisplayDialog("代码生成错误", "Component名字不能为空!", "确认");
                return false;
            }
            if (!VerifyPrefab(behaviour.gameObject))
            {
                return false;
            }
            try
            {
                InitPath("Map");
                var mapName = ToTitleCase(behaviour.gameObject.name);
                if (!GenCommonCode(mapName, componentName, mapName + "View", "MapViewTemplate"))
                {
                    return false;
                }
                if (!GenCommonCode(mapName, componentName, componentName + "Component", "MapComponentTemplate"))
                {
                    return false;
                }
                if (!GenViewModelCode(behaviour.Collector as ObjectCollector, mapName, componentName, mapName + "VM", "MapVMTemplate"))
                {
                    return false;
                }
                AssetDatabase.SaveAssets();
                AssetDatabase.Refresh();
            }
            catch (Exception ex)
            {
                Debug.LogException(ex);
                return false;
            }
            return true;
        }
    }
}
