using System.Linq;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor.Experimental.GraphView;
using MeetAndTalk.Nodes;
using UnityEngine.UIElements;
#if UNITY_EDITOR
using UnityEditor;
#endif

namespace MeetAndTalk.Editor
{
    public class DialogueSaveAndLoad
    {
        private DialogueGraphView graphView;

        public DialogueSaveAndLoad(DialogueGraphView graphView)
        {
            this.graphView = graphView;
        }

#if UNITY_EDITOR
        public void Save(DialogueContainerSO dialogueContainerSO)
        {
            // Очищаем данные в контейнере
            dialogueContainerSO.NodeLinkDatas.Clear();
            dialogueContainerSO.DialogueNodeDatas.Clear();
            dialogueContainerSO.EndNodeDatas.Clear();
            dialogueContainerSO.StartNodeDatas.Clear();

            // Собираем ноды и связи
            var nodes = graphView.nodes.ToList();
            var edges = graphView.edges.ToList();

            // Сохраняем связи между нодами
            foreach (var edge in edges)
            {
                if (edge.input.node is BaseNode inputNode && edge.output.node is BaseNode outputNode)
                {
                    dialogueContainerSO.NodeLinkDatas.Add(new NodeLinkData
                    {
                        BaseNodeGuid = outputNode.nodeGuid,
                        TargetNodeGuid = inputNode.nodeGuid
                    });
                }
            }

            // Сохраняем данные нод
            foreach (var node in nodes)
            {
                if (node is DialogueNode dialogueNode)
                {
                    dialogueContainerSO.DialogueNodeDatas.Add(CreateDialogueNodeData(dialogueNode));
                }
                else if (node is StartNode startNode)
                {
                    dialogueContainerSO.StartNodeDatas.Add(CreateStartNodeData(startNode));
                }
                else if (node is EndNode endNode)
                {
                    dialogueContainerSO.EndNodeDatas.Add(CreateEndNodeData(endNode));
                }
            }

            EditorUtility.SetDirty(dialogueContainerSO);
            AssetDatabase.SaveAssets();
        }

        public void Load(DialogueContainerSO dialogueContainerSO)
        {
            ClearGraph();

            // Создаем ноды
            foreach (var nodeData in dialogueContainerSO.DialogueNodeDatas)
            {
                DialogueNode node = graphView.CreateDialogueNode(nodeData.Position);
                node.nodeGuid = nodeData.NodeGuid;
                LoadNodeData(node, nodeData);
                graphView.AddElement(node);
            }

            foreach (var nodeData in dialogueContainerSO.StartNodeDatas)
            {
                StartNode node = graphView.CreateStartNode(nodeData.Position);
                node.nodeGuid = nodeData.NodeGuid;
                graphView.AddElement(node);
            }

            foreach (var nodeData in dialogueContainerSO.EndNodeDatas)
            {
                EndNode node = graphView.CreateEndNode(nodeData.Position);
                node.nodeGuid = nodeData.NodeGuid;
                node.EndNodeType = nodeData.EndNodeType;
                node.LoadValueInToField();
                graphView.AddElement(node);
            }

            // Соединяем ноды
            var allNodes = graphView.nodes.ToList().OfType<BaseNode>().ToList();
            foreach (var linkData in dialogueContainerSO.NodeLinkDatas)
            {
                BaseNode outputNode = allNodes.FirstOrDefault(n => n.nodeGuid == linkData.BaseNodeGuid);
                BaseNode inputNode = allNodes.FirstOrDefault(n => n.nodeGuid == linkData.TargetNodeGuid);

                if (outputNode != null && inputNode != null)
                {
                    Port outputPort = outputNode.outputContainer.Query<Port>().First();
                    Port inputPort = inputNode.inputContainer.Query<Port>().First();

                    Edge newEdge = new Edge
                    {
                        output = outputPort,
                        input = inputPort
                    };
                    inputPort.Connect(newEdge);
                    outputPort.Connect(newEdge);
                    graphView.Add(newEdge);
                }
            }
        }

        private void ClearGraph()
        {
            graphView.nodes.ForEach(node => graphView.RemoveElement(node));
            graphView.edges.ForEach(edge => graphView.RemoveElement(edge));
        }

        // Методы создания и загрузки данных для каждого типа ноды
        private DialogueNodeData CreateDialogueNodeData(DialogueNode node)
        {
            return new DialogueNodeData
            {
                NodeGuid = node.nodeGuid,
                Position = node.GetPosition().position,
                TextType = node.Texts,
                AudioClips = node.AudioClips,
                Duration = node.DurationShow,
                TextEffects = node.TextEffects
            };
        }

        private void LoadNodeData(DialogueNode node, DialogueNodeData nodeData)
        {
            node.Texts.Clear();
            node.Texts.AddRange(nodeData.TextType);
            node.AudioClips.Clear();
            node.AudioClips.AddRange(nodeData.AudioClips);
            node.DurationShow = nodeData.Duration;
            node.TextEffects = nodeData.TextEffects;
            node.LoadValueInToField();
        }

        private StartNodeData CreateStartNodeData(StartNode node)
        {
            return new StartNodeData
            {
                NodeGuid = node.nodeGuid,
                Position = node.GetPosition().position
            };
        }

        private EndNodeData CreateEndNodeData(EndNode node)
        {
            return new EndNodeData
            {
                NodeGuid = node.nodeGuid,
                Position = node.GetPosition().position,
                EndNodeType = node.EndNodeType
            };
        }
#endif
    }
}

// DialogueNodeData.cs
using System.Collections.Generic;
using UnityEngine;

namespace MeetAndTalk
{
    [System.Serializable]
    public class DialogueNodeData : BaseNodeData
    {
        public List<LanguageGeneric<AudioClip>> AudioClips;
        public List<LanguageGeneric<string>> TextType;
        public float Duration;
        public List<TextEffectData> TextEffects = new List<TextEffectData>();
    }
}

Assets\Meet and Talk\Editor\DialogueSaveAndLoad.cs(53,63): error CS1503: Argument 1: cannot convert from 'MeetAndTalk.DialogueNodeData [F:\Dowloads\TestNomer1\Assets\Meet and Talk\Editor\Node\DialogueNodeData.cs(7)]' to 'MeetAndTalk.DialogueNodeData [F:\Dowloads\TestNomer1\Library\Bee\artifacts\1900b0aEDbg.dag\Assembly-CSharp.ref.dll]'


Assets\Meet and Talk\Editor\DialogueSaveAndLoad.cs(78,36): error CS1503: Argument 2: cannot convert from 'MeetAndTalk.DialogueNodeData [F:\Dowloads\TestNomer1\Library\Bee\artifacts\1900b0aEDbg.dag\Assembly-CSharp.ref.dll]' to 'MeetAndTalk.DialogueNodeData [F:\Dowloads\TestNomer1\Assets\Meet and Talk\Editor\Node\DialogueNodeData.cs(7)]'

Исрпавь данные ошибки, напиши полный код