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

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

	[Export(PropertyHint.None, "")]
	private SaveFile.Flags neededFlag;

	private string[] names;

	private List<Room.IDs> targets = new List<Room.IDs> { Room.IDs.DarkPrisonRight };

	private List<Vector2> pos = new List<Vector2>
	{
		new Vector2(280f, -220f)
	};

	public override void _EnterTree()
	{
		if (!SaveFile.HasFlag(neededFlag))
		{
			smoke.Visible = false;
			base.Frame = 2;
		}
		else
		{
			base.Frame = 0;
			smoke.Visible = true;
		}
	}

	public void Interact()
	{
		if (!SaveFile.HasFlag(neededFlag))
		{
			TextSystem.GetText("MagicDoorInactive");
			return;
		}
		names = FileAccess.Open(Texts.textFolder + "/WarpDoor.txt", FileAccess.ModeFlags.Read).GetAsText(skipCr: true).Split('\n');
		for (int i = 0; i < names.Length; i++)
		{
			names[i] = names[i].Replace("@", "\n");
		}
		List<string> list = new List<string> { names[0] };
		if (!SaveFile.HasFlag(SaveFile.Flags.Day1End))
		{
			list.Add(names[1]);
			targets.Add(Room.IDs.SchoolDWLake);
			pos.Add(new Vector2(449f, 261f));
		}
		else if (!SaveFile.HasFlag(SaveFile.Flags.Day2End))
		{
			list.Add(names[2]);
			targets.Add(Room.IDs.ChujinDWOverview);
			pos.Add(new Vector2(544f, 202f));
			if (SaveFile.current.values[5] >= 23)
			{
				list.Add(names[3]);
				targets.Add(Room.IDs.ChujinDWUpstairsBalloons);
				pos.Add(new Vector2(206f, -120f));
			}
		}
		Main.inEvent = Coroutine.Start(Event(list.ToArray()));
	}

	private IEnumerator Event(string[] p)
	{
		TextSystem.SingleText(Texts.mainDiag["MagicDoorPrompt"][0].text, null, null, p);
		while (TextSystem.instance.Visible)
		{
			yield return null;
		}
		if (Room.current.id == targets[TextSystem.lastPrompt])
		{
			TextSystem.GetText("MagicDoorAlready");
			while (TextSystem.instance.Visible)
			{
				yield return null;
			}
			yield break;
		}
		SaveFile.AddFlag(SaveFile.Flags.KanakoTalkAfterTutorial);
		SaveFile.AddFlag(SaveFile.Flags.WarpDoorFirst);
		base.Frame = 1;
		Audio.PlaySound("snd_dooropen_ch1.wav");
		TextSystem.GetText("MagicDoorWarp");
		while (TextSystem.instance.Visible)
		{
			yield return null;
		}
		Audio.PlaySound("snd_doorclose_ch1.wav");
		CameraController.Transition(Main.colorBlack);
		Audio.music.Stop();
		for (float a = 0f; a < 30f; a += Main.deltaTime)
		{
			yield return null;
		}
		Party.Reparent(Main.instance);
		Room.LoadRoom(targets[TextSystem.lastPrompt]);
		yield return null;
		Party.Reparent(Room.current);
		Player.instance.GlobalPosition = pos[TextSystem.lastPrompt];
		Player.instance.direction = Entity.Direction.South;
		Player.instance.UpdateAnim(force: true);
		Party.TeleportFollowers();
		for (float a = 0f; a < 30f; a += Main.deltaTime)
		{
			yield return null;
		}
		Audio.PlaySound("snd_dooropen_ch1.wav");
		CameraController.Transition(Main.colorClearB, 30f);
		Room.current.DoMusic();
		while (!CameraController.transitionRoutine.done)
		{
			yield return null;
		}
	}

	}