using System;
using System.Collections.Generic;
using UnityEngine;
using Wizard.Battle.View.Vfx;

namespace Wizard.Battle.Player.ClassCharacter;

public class SkinEffectVfx : VfxBase, IEffectVfx
{
	private Func<EffectBattle> _getLoadedEffectObject;

	protected EffectBattle _effectObject;

	private Transform _targetTransform;

	private int _order = -1;

	private bool _isEvolve = true;

	public bool IsEffectEnd { get; private set; }

	public SkinEffectVfx(Func<EffectBattle> getLoadedEffectObject, Transform baseTransform, int order = -1, bool isEvolve = true)
	{
		if (BattleManagerBase.GetIns() == null || !BattleManagerBase.GetIns().IsRecovery)
		{
			_targetTransform = baseTransform;
			_getLoadedEffectObject = getLoadedEffectObject;
			_order = order;
			_isEvolve = isEvolve;
		}
	}

	public static void SetLayerAndOrder(GameObject gameObject, int layerNo, int order, bool needSetChildren = true)
	{
		if (gameObject == null)
		{
			return;
		}
		gameObject.layer = layerNo;
		ParticleSystemRenderer component = gameObject.GetComponent<ParticleSystemRenderer>();
		if (component != null)
		{
			component.sortingOrder = order;
		}
		if (!needSetChildren)
		{
			return;
		}
		foreach (Transform item in gameObject.transform)
		{
			SetLayerAndOrder(item.gameObject, layerNo, order, needSetChildren);
		}
	}

	public override void Play()
	{
		if (BattleManagerBase.GetIns() != null && BattleManagerBase.GetIns().IsRecovery)
		{
			return;
		}
		base.Play();
		_effectObject = _getLoadedEffectObject();
		if (!(_effectObject == null))
		{
			_effectObject.GetComponent<ParticleSystemRenderer>();
			SetLayerAndOrder(_effectObject.gameObject, _isEvolve ? 13 : 15, _order);
			if (!_isEvolve)
			{
				SetMaskSetting(_effectObject.transform);
			}
			_effectObject.gameObject.SetActive(value: true);
			IsEffectEnd = false;
			IsEnd = true;
		}
	}

	private void SetMaskSetting(Transform effectObject)
	{
		foreach (Transform item in effectObject)
		{
			ParticleSystemRenderer component = item.GetComponent<ParticleSystemRenderer>();
			if (component != null)
			{
				component.maskInteraction = SpriteMaskInteraction.VisibleInsideMask;
			}
			SetMaskSetting(item);
		}
	}

	public void UpdateEffect()
	{
		if (_effectObject == null)
		{
			IsEffectEnd = true;
		}
		else
		{
			RemoveCheck();
		}
	}

	public void RemoveCheck()
	{
		if (CheckEffectEnd(_effectObject))
		{
			DestroyEffect();
		}
	}

	public void DestroyEffect()
	{
		UnityEngine.Object.Destroy(_effectObject.gameObject);
		IsEffectEnd = true;
	}

	public override void Update(float dt, List<IEffectVfx> effectVfxList)
	{
		base.Update(dt, effectVfxList);
		if (IsEnd)
		{
			VfxMgr.CheckAndAddEffectVfxList(this, effectVfxList);
		}
	}

	private bool CheckEffectEnd(EffectBattle effect)
	{
		if (effect.AnimationObj != null)
		{
			if (effect.AnimationObj.isPlaying)
			{
				return false;
			}
			effect.AnimationObj.gameObject.SetActive(value: false);
		}
		foreach (Transform item in effect.transform)
		{
			ParticleSystem component = item.GetComponent<ParticleSystem>();
			if (component != null && component.IsAlive())
			{
				return false;
			}
		}
		return true;
	}
}
