#if !COMBAT_SERVER

using System;
using System.Collections.Generic;
using System.IO;
#if UNITY_EDITOR
using UnityEditor;
#endif
using UnityEngine;
using UnityEngine.Playables;
using UnityEngine.Timeline;
using Object = UnityEngine.Object;

public class SerializationTimeLineTool
{
#if UNITY_EDITOR

       [MenuItem("CombatData/导出英雄timeLine数据")]
        private static void SeveHeroTimeLine()
        {
            string _path = "Assets/Res/TimeLineAssets/";
            string path = Application.dataPath + "/Art/ArtHero";

            string[] filePaths = System.IO.Directory.GetFiles(path, "*", SearchOption.AllDirectories);
            for (int i = 0;
                 i < filePaths.Length;
                 i++)
            {
                string p = filePaths[i];
                if (p.Contains(".meta"))
                {
                    continue;
                }

                GameObject g2 = null;
                try
                {
                    p = p.Replace(Application.dataPath, "Assets");
                    GameObject g = AssetDatabase.LoadAssetAtPath<GameObject>(p);
                    g2 = GameObject.Instantiate(g);
                    PlayableDirector playableDirector = g2.GetComponentInChildren<PlayableDirector>(true);
                    if (playableDirector == null || playableDirector.playableAsset == null)
                    {
                        GameObject.DestroyImmediate(g2);
                        continue;
                    }

                    // g.SetActive(true);
                    playableDirector.RebuildGraph();
                    playableDirector.Play();
                    if (!playableDirector.playableGraph.IsValid())
                    {
                        playableDirector.RebuildGraph();
                    }

                    if (!playableDirector.playableGraph.IsValid())
                    {
                        Debug.LogError("没有playableGraph" + p);
                    }

                    // Debug.Log(playableDirector.playableGraph);
                    TimeLienData timeLienData = SerializationTimeLineTool.SerializationTimeLine(playableDirector);
                    timeLienData.heroName = g.name;
                    string ctx = JsonManager.ToJson(timeLienData);

                    File.WriteAllText(_path + g.name + "_TD.txt", ctx);

                    playableDirector.playableGraph.Destroy();
                    GameObject.DestroyImmediate(g2);
                }
                catch (Exception e)
                {
                    if (g2 != null)
                    {
                        GameObject.DestroyImmediate(g2);
                    }

                    Debug.LogError(p);
                    Debug.LogException(e);
                }
            }

            Debug.Log("生成资源");
            AssetDatabase.Refresh();
            AssetDatabase.SaveAssets();
        }

        [MenuItem("CombatData/ 导美术英雄到战斗英雄里面")]
        private static void ArtHeroToCombatHero()
        {
       
            string path = Application.dataPath + "/Art/ArtHero";

            string[] filePaths = System.IO.Directory.GetFiles(path, "*", SearchOption.AllDirectories);
            for (int i = 0;
                 i < filePaths.Length;
                 i++)
            {
                string p = filePaths[i];
                if (p.Contains(".meta"))
                {
                    continue;
                }

                try
                {
                    p = p.Replace(Application.dataPath, "Assets");
                    GameObject g = AssetDatabase.LoadAssetAtPath<GameObject>(p);


                    // GameObject newObje= ()PrefabUtility.InstantiatePrefab(g);
                    GameObject newObje = GameObject.Instantiate(g);
                    newObje.name = g.name;
                    BoxCollider2D[] collider = newObje.GetComponentsInChildren<BoxCollider2D>(true);
                    for (int j = 0; j < collider.Length; j++)
                    {
                        GameObject.DestroyImmediate(collider[j]);
                    }

                    // SpecialDot[] SpecialDot = newObje.GetComponentsInChildren<SpecialDot>(true);
                    // for (int j = 0; j < SpecialDot.Length; j++)
                    // {
                    //     GameObject.DestroyImmediate(SpecialDot[j]);
                    // }

                    // CombatHeroEntityHitPointMono[] CombatHeroEntityHitPointMono =
                    //     newObje.GetComponentsInChildren<CombatHeroEntityHitPointMono>(true);
                    // for (int j = 0; j < CombatHeroEntityHitPointMono.Length; j++)
                    // {
                    //     GameObject.DestroyImmediate(CombatHeroEntityHitPointMono[j]);
                    // }
                    PlayableDirector playableDirector=    newObje.GetComponent<PlayableDirector>();
                    if (playableDirector != null)
                    {
                        GameObject.DestroyImmediate(playableDirector);
                    }
                    Transform t = newObje.transform.Find("timeline");
                    if (t != null)
                    {
                        GameObject.DestroyImmediate(t.gameObject);
                    }

                    t = newObje.transform.Find("effect");
                    if (t != null)
                    {
                        GameObject.DestroyImmediate(t.gameObject);
                    }


                    PrefabUtility.SaveAsPrefabAssetAndConnect(newObje, "Assets/Res/CombatHero/" + g.name + ".prefab",
                        InteractionMode.UserAction);

                    GameObject.DestroyImmediate(newObje);
                    AssetDatabase.ImportAsset("Assets/Res/CombatHero/" + g.name + ".prefab");
                }
                catch (Exception e)
                {
                    Debug.LogError(p);
                    Debug.LogException(e);
                }
            }

            Debug.Log("生成资源");
            AssetDatabase.Refresh();
            AssetDatabase.SaveAssets();
        }
    
#endif
    public static TimeLienData SerializationTimeLine(PlayableDirector playableDirector)
    {
        TimeLienData timeLienData = new TimeLienData();

        IEnumerable<PlayableBinding> allPlay = playableDirector.playableAsset.outputs;
        foreach (PlayableBinding pb in allPlay)
        {
            TrackAsset playableAsset = pb.sourceObject as TrackAsset;
            if (playableAsset != null)
            {
                IEnumerable<TimelineClip> clips = playableAsset.GetClips();
                GroupTrack groupTrack = playableAsset.GetGroup();
         
                foreach (var valueClip in clips)
                {
                    Object asset = valueClip.asset;
                    Debug.Log(valueClip.asset);
                    TimeLineAssetSerialization serialization = null;
                    if (asset is AudioPlayableAsset)
                    {
                        serialization =
                            new TimeLineAudioSeralization(playableDirector, groupTrack, valueClip);
                        timeLienData.key.Add("TimeLineAudioSeralization");
                    }
                    else if (asset is ActiveGameObjectPlayableAsset)
                    {
                        serialization =
                            new TimeLineActiveGameObjectSerializtion(playableDirector, groupTrack, valueClip);
                        timeLienData.key.Add("TimeLineActiveGameObjectSerializtion");
                    }
                    else if (asset is ControlPlayableAsset)
                    {
                        serialization =
                            new TimeLinePlayFxSerializtion(playableDirector, groupTrack, valueClip);
                        timeLienData.key.Add("TimeLinePlayFxSerializtion");
                    }
                    // else if (asset is SpineAnimationStateClip)
                    // {
                    //     serialization =
                    //         new TimeLineSpineSeralization(playableDirector, groupTrack, valueClip);
                    //     timeLienData.key.Add("TimeLineSpineSeralization");
                    // }
                    else if (asset is TimeLineSkillStartAsset)
                    {
                        serialization =
                            new TimeLineSkillStartSerializtion(playableDirector, groupTrack, valueClip);
                        timeLienData.key.Add("TimeLineSkillStartSerializtion");
                    }
                    else if (asset is TimeLineTriggerAsset)
                    {
                        serialization =
                            new TimeLineTriggerSerialization(playableDirector, groupTrack, valueClip);
                        timeLienData.key.Add("TimeLineTriggerSerialization");
                    }
                    else if (asset is TimeLineEndSingAsset)
                    {
                        serialization =
                            new TimeLineEndSingSerializtion(playableDirector, groupTrack, valueClip);
                        timeLienData.key.Add("TimeLineEndSingSerializtion");
                    }
                    else if (asset is TimeLineCollisionTriggerAsset)
                    {
                        serialization =
                            new TimeLineCollisionTriggerSerializtion(playableDirector, groupTrack, valueClip);
                        timeLienData.key.Add("TimeLineCollisionTriggerSerializtion");
                    }
                    else if (asset is TimeLineCurveAsset)
                    {
                        serialization =
                            new TimeLineCurveSerializtion(playableDirector, groupTrack, valueClip);
                        timeLienData.key.Add("TimeLineCurveSerializtion");
                    }
                    else if (asset is CameraShakingAsset)
                    {
                        serialization =
                            new CameraShakingSerializtion(playableDirector, groupTrack, valueClip);
                        timeLienData.key.Add("CameraShakingSerializtion");
                    }
                    else if (asset is TimeLineSkillPreparationAssets)
                    {
                        serialization =
                            new TimeLineSkillPreparationSerializtion(playableDirector, groupTrack, valueClip);
                        timeLienData.key.Add("TimeLineSkillPreparationSerializtion");
                    }
                    // else if (asset is FMODEventPlayable)
                    // {
                    //     serialization =
                    //         new FMODEventPlayableSerializtion(playableDirector, groupTrack, valueClip);
                    //     timeLienData.key.Add("FMODEventPlayableSerializtion");
                    // }
                    else if (asset is TimeLineFallAsset)
                    {
                        serialization =
                            new TimeLineFallSeraliztion(playableDirector, groupTrack, valueClip);
                        timeLienData.key.Add("TimeLineFallSeraliztion");
                    }
                    else
                    {
                        if (!asset.GetType().ToString().Equals("UnityEngine.Timeline.ActivationPlayableAsset"))
                        {
                            Debug.LogWarning("这是一个目前不受支持的类型+" + asset.GetType().ToString() + "__" + groupTrack.name + "___" + asset.name + "___" +
                                           valueClip.start);
                        }
                        else
                        {
                            serialization =
                                new TimeLineMaxTimeSerializtion(playableDirector, groupTrack, valueClip);
                            timeLienData.key.Add("TimeLineMaxTimeSerializtion");
                        }
                    }

                    if (serialization == null) continue;
                    timeLienData.value.Add(serialization.ToJson());
                }
            }
        }

        return timeLienData;
    }
}
#endif