using System.Collections.Generic;
using UnityEngine;
using WS;
[ObjectSystem]
public class SceneDataSaveComponentAwakSystem : AwakeSystem<SceneDataSaveComponent>
{
    public override void Awake(SceneDataSaveComponent self)
    {
        self.Awake();
    }
}

public class SceneDataSaveComponent : SingleComponent<SceneDataSaveComponent>
{
    private Dictionary<string, List<SceneData>> data = new Dictionary<string, List<SceneData>>();
    public class SceneData
    {
        public string objName;
        public string animName;
    }
    public void Awake()
    {

    }
    /// <summary>添加</summary>
    public void AddSceneData(string sceneName, string objName, string animName)
    {
        if (data.ContainsKey(sceneName))
        {
            if (data[sceneName].Find(x=>x.objName== objName)==null)
            {
                data[sceneName].Add(new SceneData() { objName = objName, animName = animName });
            }
            else
            {
                int index = data[sceneName].FindIndex(x => x.objName == objName);
                data[sceneName][index].animName = animName;
            }
        }
        else
        {
            data.Add(sceneName, new List<SceneData>());
            data[sceneName].Add(new SceneData() { objName = objName, animName = animName });
        }
    }
    /// <summary>删除流程</summary>
    public void RemoveSceneData(string sceneName, string objName, string animName)
    {
        if (data.ContainsKey(sceneName))
        {
            var d = data[sceneName].FindAll(x => x.objName == objName);
            if (d != null && d.Count != 0 && d.Find(x => x.animName == animName) != null)
            {
                data[sceneName].Remove(d.Find(x => x.animName == animName));
            }
        }
    }
    ///<summary>加载场景数据</summary>
    public void LoadScene(string sceneName)
    {
        if (data.ContainsKey(sceneName))
        {
            var d = data[sceneName];
            for (int i = 0; i < d.Count; i++)
            {
                var InteractObj = GameObject.Find(d[i].objName);
                if (InteractObj != null)
                {
                    var anim = InteractObj.transform.GetComponent<Animator>();
                    var infos = anim.runtimeAnimatorController.animationClips;
                    for (int j = 0;j<infos.Length;j++)
                    {
                        var info = infos[j];
                        if(info.name== d[i].animName)
                        {
                            anim.Play(d[i].animName, -1, info.length);
                        }
                    }
                  
                }
            }
        }
    }
    ///<summary>清除单个场景数据</summary>
    public void RestScene(string sceneName)
    {
        if (data.ContainsKey(sceneName))
        {
            data[sceneName].Clear();
        }
    }
    ///<summary>清除所有数据</summary>
    public void RestAllScene()
    {
        data.Clear();
    }
}


