using Thousandto.Code.Logic;
using Thousandto.Core.Asset;
using Thousandto.SkillEditor;
using Thousandto.SkillEditor.DIY;
using Thousandto.SkillEditor.Support;
using System;
using System.Collections.Generic;
using System.IO;
using System.Text.RegularExpressions;
using UnityEditor;
using UnityEngine;

namespace Thousandto.SkillEditor.DIY
{
    //编辑器链接效果
    public class EditorLinkDamageEffect
    {
        public EditorEntity Owner { get; private set; }
        public PlayLinkDamageEventInfo EventInfo { get; private set; }
        public bool IsFinish { get; private set; }
        public int CurFrame { get; private set; }
        private float _frameTimer = 0f;
        private int _curHitCount = 0;
        private const float OneFrameTime = 1f / 30f;
        private List<EditorVFXSingleLink> _vfxList = new List<EditorVFXSingleLink>();
        private List<EditorEntity> _targetList = null;

        public EditorLinkDamageEffect(EditorEntity owner, PlayLinkDamageEventInfo eventInfo, List<EditorEntity> targetList)
        {
            Owner = owner;
            EventInfo = eventInfo;
            IsFinish = false;
            CurFrame = 0;
            _frameTimer = 0f;
            _curHitCount = 0;
            _vfxList.Clear();
            _targetList = targetList;

            if (targetList.Count <= 0)
            {
                Stop();
                return;
            }

            EditorEntity startEntity = null;
            if (EventInfo.StartPosType != 0)
            {
                //目标之间进行连接,必须要大于一个目标
                if (targetList.Count <= 1)
                {
                    Stop();
                    return;
                }
                startEntity = targetList[0];
            }
            else
            {
                startEntity = Owner;
            }

            if (EventInfo.LinkType != 0)
            {
                //并联
                for (int i = 0; i < targetList.Count; ++i)
                {
                    if (targetList[i] == startEntity)
                    {
                        continue;
                    }
                    LinkEntity(startEntity, targetList[i], startEntity == Owner ? EventInfo.UserSlot : EventInfo.TargetSlot, EventInfo.TargetSlot, EventInfo.VfxID);
                }
            }
            else
            {
                //串联
                var start = startEntity;
                for (int i = 0; i < targetList.Count; ++i)
                {
                    if(start == targetList[i])
                    {
                        continue;
                    }
                    LinkEntity(start, targetList[i], start == Owner ? EventInfo.UserSlot : EventInfo.TargetSlot, EventInfo.TargetSlot, EventInfo.VfxID);
                    start = targetList[i];
                }
            }
        }

        public void Update(float dt)
        {
            if (IsFinish)
            {
                return;
            }

            _frameTimer += dt;
            while (_frameTimer > OneFrameTime)
            {
                ++CurFrame;
                OnFrameUpdate(CurFrame);
                _frameTimer -= OneFrameTime;
            }

            if (CurFrame > EventInfo.LifeFrameCount)
            {
                Stop();
            }
        }

        public void Stop()
        {
            IsFinish = true;
            for(int i=0;i< _vfxList.Count;++i)
            {
                _vfxList[i].Stop();
            }
            _vfxList.Clear();
        }

        private void LinkEntity(EditorEntity start, EditorEntity target, string slotStart, string slotTarget, int vfxID)
        {
            var vfx = new EditorVFXSingleLink(ModelTypeCode.SkillVFX, vfxID);
            vfx.SetParams(start.RootTrans, slotStart, target.RootTrans, slotTarget);
            vfx.Play();
            _vfxList.Add(vfx);
        }

        private void OnFrameUpdate(int frame)
        {
            if(_curHitCount < EventInfo.HitCount && (frame - EventInfo.StartHitFrame) % EventInfo.HitInterval == 0)
            {
                //产生伤害
                ++_curHitCount;
                var mainDir = Owner.RootTrans.forward;
                mainDir.y = 0f;
                for (int i = 0; i < _targetList.Count; ++i)
                {
                    var target = _targetList[i];
                    target.DoHitEffect(Owner, mainDir, EventInfo.HitInfo);
                }
            }
        }
    }
}