using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using Godot;
using Godot.Bridge;
using Godot.NativeInterop;

[ScriptPath("res://Scripts/Events/Fountain.cs")]
public partial class Fountain : Sprite2D
{
	[Export(PropertyHint.None, "")]
	private Sprite2D soul;

	[Export(PropertyHint.None, "")]
	private AnimationPlayer mainAnim;

	[Export(PropertyHint.None, "")]
	private AnimationPlayer colors;

	[Export(PropertyHint.None, "")]
	private AnimationPlayer soulAnim;

	[Export(PropertyHint.None, "")]
	private AnimationPlayer scrolling;

	[Export(PropertyHint.None, "")]
	private Node2D[] partyPos;

	private static Fountain instance;

	private readonly StringName matProperty = "shader_parameter/motion";

	public override void _Ready()
	{
		instance = this;
	}

	private static Vector2[] SetPartyPos()
	{
		List<Vector2> list = new List<Vector2>();
		Party.UpdateFollowerEntities();
		for (int i = 0; i < SaveFile.current.activeParty.Count; i++)
		{
			Party.party[SaveFile.current.activeParty[i]].oEntity.GlobalPosition = instance.partyPos[i].GlobalPosition + Vector2.Down * 100f;
			Party.party[SaveFile.current.activeParty[i]].oEntity.anim.Play("FountainMove");
			list.Add(Party.party[SaveFile.current.activeParty[i]].oEntity.GlobalPosition);
		}
		return list.ToArray();
	}

	private static IEnumerator MoveParty(Vector2[] pos)
	{
		float a = 0f;
		for (float b = 250f; a <= b + 1f; a += Main.deltaTime)
		{
			for (int i = 0; i < SaveFile.current.activeParty.Count; i++)
			{
				Party.party[SaveFile.current.activeParty[i]].oEntity.GlobalPosition = pos[i].Lerp(instance.partyPos[i].GlobalPosition, a / b);
			}
			yield return null;
		}
		for (int j = 0; j < SaveFile.current.activeParty.Count; j++)
		{
			Party.party[SaveFile.current.activeParty[j]].oEntity.anim.Stop();
		}
	}

	public static IEnumerator SchoolDW()
	{
		Audio.ChangeMusic(null);
		CameraController.Transition(Main.colorClearB);
		yield return null;
		CameraController.Transition(Main.colorBlack, 30f);
		while (!CameraController.transitionRoutine.done)
		{
			yield return null;
		}
		yield return null;
		Room.LoadRoom(Room.IDs.FountainRoom, reparent: true);
		yield return null;
		CameraController.Transition(Main.colorClearB, 120f);
		Room.current.DoMusic();
		Coroutine c = Coroutine.Start(MoveParty(SetPartyPos()));
		while (!c.done)
		{
			yield return null;
		}
		float a;
		for (a = 0f; a < 60f; a += Main.deltaTime)
		{
			yield return null;
		}
		TextSystem.GetText("SchoolFountainDiag");
		while (TextSystem.instance.Visible)
		{
			yield return null;
		}
		Player.instance.anim.Play();
		a = 0f;
		for (float b = 180f; a < b; a += Main.deltaTime)
		{
			Player.instance.GlobalPosition = instance.partyPos[0].GlobalPosition.Lerp(instance.partyPos[3].GlobalPosition, a / b);
			yield return null;
		}
		Player.instance.anim.Stop();
		for (float b = 0f; b < 60f; b += Main.deltaTime)
		{
			yield return null;
		}
		TextSystem.GetText("FountainSoul");
		while (TextSystem.instance.Visible)
		{
			yield return null;
		}
		c = Coroutine.Start(instance.FountainClose());
		while (!c.done)
		{
			yield return null;
		}
		Room.LoadRoom(Room.IDs.SchoolCloset, reparent: true);
		yield return null;
		Main.inEvent = Coroutine.Start(LWDay1Events.ClosetPostDWEvent());
	}

	private IEnumerator FountainClose()
	{
		Audio.ChangeMusic(Room.current.music[1]);
		soul.GlobalPosition = Player.instance.GlobalPosition + Vector2.Up * 16f;
		soul.Visible = true;
		((Node2D)Main.Particle("SoulSplit", Vector2.Zero, null, soul)).Scale *= 1.5f;
		Audio.PlaySound("snd_great_shine.ogg");
		float a;
		for (a = 0f; a < 60f; a += Main.deltaTime)
		{
			yield return null;
		}
		Vector2 p = soul.GlobalPosition;
		Vector2 t = new Vector2(0f, -120f);
		a = 0f;
		float b;
		for (b = 400f; a < b; a += Main.deltaTime)
		{
			soul.GlobalPosition = p.Lerp(t, a / b);
			yield return null;
		}
		colors.Pause();
		b = 0f;
		for (a = 150f; b < a; b += Main.deltaTime)
		{
			mainAnim.SpeedScale = Mathf.Lerp(mainAnim.SpeedScale, 0f, Main.deltaTime * 0.025f);
			scrolling.SpeedScale = Mathf.Lerp(scrolling.SpeedScale, 0f, Main.deltaTime * 0.025f);
			Room.current.Modulate = Room.current.Modulate.Lerp(Main.colorWhite, Main.deltaTime * 0.025f);
			yield return null;
		}
		for (a = 0f; a < 50f; a += Main.deltaTime)
		{
			yield return null;
		}
		Audio.PlaySound("snd_revival.ogg");
		soulAnim.Play("Soul");
		a = 0f;
		for (b = 100f; a < b; a += Main.deltaTime)
		{
			yield return null;
		}
		Color c = soul.SelfModulate;
		b = 0f;
		for (a = 160f; b < a; b += Main.deltaTime)
		{
			soul.SelfModulate = c.Lerp(Main.colorClear, b / a);
			yield return null;
		}
		SaveFile.current.flags.Remove(SaveFile.Flags.IsInDW);
		Party.UpdateFollowerEntities();
		for (int i = 0; i < SaveFile.current.activeParty.Count; i++)
		{
			Party.party[i].AddBonus();
			Party.party[i].LV++;
			Party.party[i].oEntity.animMod = Entity.AnimMods.None;
		}
		Party.LookAt(Vector2.Down);
		CameraController.Transition(Main.colorBlack, 300f);
		while (!CameraController.transitionRoutine.done)
		{
			yield return null;
		}
		a = 0f;
		for (b = 60f; a < b; a += Main.deltaTime)
		{
			yield return null;
		}
	}

	}