using System;
using System.Collections.Generic;
using CombatCore.SerializationTimeLine;
using CombatLibrary.CombatLibrary.CombatCore.CustomizeTimeLogic.FxLogic;
using CombatLibrary.CombatLibrary.CombatCore.Utility;
using Fort23.UTool;
using Utility;
#if !COMBAT_SERVER
using UnityEngine;
#endif

[System.Serializable]
public class TimeLienData
{
    public List<string> key = new List<string>();

    public List<string> value = new List<string>();
    public string heroName;

    public Map<string, List<TimeLineAssetSerialization>> allAssetSerializations =
        new Map<string, List<TimeLineAssetSerialization>>();

    public BetterList<string> allAssetSerializationsKeys = new BetterList<string>();
    // public TimeLineAssetSerialization[] GetAllTimeLineAssetSerialization
    // {
    //     get { return allAssetSerializations.ToArray(); }
    // }

    public TimeLienData Clone()
    {
        TimeLienData timeLienData = (TimeLienData) this.MemberwiseClone();
        timeLienData.DeserializeData();
        return timeLienData;
    }

    /// <summary>
    /// 反序列化
    /// </summary>
    public void DeserializeData()
    {
        allAssetSerializations.Clear();
        allAssetSerializationsKeys.Clear();
        for (int i = 0; i < key.Count; i++)
        {
            Type type = System.Type.GetType(key[i]);
            if (type != null)
            {
                TimeLineAssetSerialization timeLineAssetSerialization =
                    (TimeLineAssetSerialization) JsonManager.FromJson(value[i], type);
                if (allAssetSerializations.TryGetValue(timeLineAssetSerialization.groupName,
                        out List<TimeLineAssetSerialization> info))
                {
                    info.Add(timeLineAssetSerialization);
                }
                else
                {
                    info = new List<TimeLineAssetSerialization>();
                    info.Add(timeLineAssetSerialization);
                    string keys = timeLineAssetSerialization.groupName;
                    allAssetSerializations.Add(keys, info);
                    allAssetSerializationsKeys.Add(keys);
                }
            }
            else
            {
                LogTool.Error(key[i] + "失败");
            }
        }
    }

    public T GetTimeLineEventLogicGroup<T>(string groupName, IFilterTimeLine filterTimeLine)
        where T : TimeLineEventLogicGroupBasic, new()
    {
        List<TimeLineAssetSerialization> timeLineAssetSerializations =
            GetTimeLineAssetSerialization(groupName, filterTimeLine);
        if (timeLineAssetSerializations.Count <= 0)
        {
            // LogTool.Log("没有时间线" + groupName);
            return null;
        }

        T timeLineEventLogicGroup = new T();
        timeLineEventLogicGroup.groupName = groupName;

        timeLineEventLogicGroup.Init(timeLineAssetSerializations);
        return timeLineEventLogicGroup;
    }

    public float GetTimeLineEventLogicGroupForTime(string groupName, IFilterTimeLine filterTimeLine)
    {
        float maxTime = 1;
        List<TimeLineAssetSerialization> timeLineAssetSerializations =
            GetTimeLineAssetSerialization(groupName, filterTimeLine);
        for (int i = 0; i < timeLineAssetSerializations.Count; i++)
        {
            TimeLineAssetSerialization timeLineAssetSerialization = timeLineAssetSerializations[i];
            if (timeLineAssetSerialization is TimeLineMaxTimeSerializtion)
            {
                if (maxTime < timeLineAssetSerialization.endTime)
                {
                    maxTime = timeLineAssetSerialization.endTime;
                }
            }
        }

        return maxTime;
    }

    public List<CurveInfo> GetTimeLineEventCurveInfos(string groupName, string guid, IFilterTimeLine filterTimeLine)
    {
        List<TimeLineAssetSerialization> timeLineAssetSerializations =
            GetTimeLineAssetSerialization(groupName, filterTimeLine);
        for (int i = 0; i < timeLineAssetSerializations.Count; i++)
        {
            TimeLineCurveSerializtion timeLineAssetSerialization =
                timeLineAssetSerializations[i] as TimeLineCurveSerializtion;
            if (timeLineAssetSerialization != null && timeLineAssetSerialization.guid.Equals(guid))
            {
                return timeLineAssetSerialization.CurveInfos;
            }
        }

        return null;
    }
    public ACurve GetTimeLineEventACurve(string groupName, string guid, IFilterTimeLine filterTimeLine)
    {
       
        List<TimeLineAssetSerialization> timeLineAssetSerializations =
            GetTimeLineAssetSerialization(groupName, filterTimeLine);
        for (int i = 0; i < timeLineAssetSerializations.Count; i++)
        {
            TimeLineCurveSerializtion timeLineAssetSerialization =
                timeLineAssetSerializations[i] as TimeLineCurveSerializtion;
            if (timeLineAssetSerialization != null && timeLineAssetSerialization.guid.Equals(guid))
            {
                ACurve aCurve = new ACurve();
                aCurve.CurveInfos= timeLineAssetSerialization.CurveInfos;
                return aCurve;
            }
        }

        return default;
    }
    public float GetTimeLineEventSkillForTime(string groupName, IFilterTimeLine filterTimeLine)
    {
        float maxTime = 0;
        List<TimeLineAssetSerialization> timeLineAssetSerializations =
            GetTimeLineAssetSerialization(groupName, filterTimeLine);
        for (int i = 0; i < timeLineAssetSerializations.Count; i++)
        {
            TimeLineAssetSerialization timeLineAssetSerialization = timeLineAssetSerializations[i];
            // if (timeLineAssetSerialization is TimeLineSpineSeralization)
            // {
            //     return timeLineAssetSerialization.continuedTime;
            // }
        }

        return 0;
    }
    public float GetTimeLineEventSkillForPuaseTime(string groupName, IFilterTimeLine filterTimeLine)
    {
        float maxTime = 0;
        List<TimeLineAssetSerialization> timeLineAssetSerializations =
            GetTimeLineAssetSerialization(groupName, filterTimeLine);
        for (int i = 0; i < timeLineAssetSerializations.Count; i++)
        {
            TimeLineAssetSerialization timeLineAssetSerialization = timeLineAssetSerializations[i];
            if (timeLineAssetSerialization is TimeLineSkillStartSerializtion)
            {
                return timeLineAssetSerialization.continuedTime;
            }
        }

        return 0;
    }

    public List<TimeLineAssetSerialization> GetTimeLineAssetSerialization(string groupName,
        IFilterTimeLine filterTimeLine)
    {
        List<TimeLineAssetSerialization> currTimeLineAssetSerializations = new List<TimeLineAssetSerialization>();
        if (allAssetSerializations.TryGetValue(groupName,
                out List<TimeLineAssetSerialization> allInfo))
        {
            for (int i = 0; i < allInfo.Count; i++)
            {
                TimeLineAssetSerialization fxSerializtion = allInfo[i];
                if (fxSerializtion.groupName.Equals(groupName) && (filterTimeLine==null||filterTimeLine.Filter(fxSerializtion)))
                {
                    currTimeLineAssetSerializations.Add(fxSerializtion);
                }
            }

            return allInfo;
        }

        return currTimeLineAssetSerializations;
    }
}