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

[ScriptPath("res://Scripts/UI/PartyHUD.cs")]
public partial class PartyHUD : NinePatchRect
{
	[Export(PropertyHint.None, "")]
	private int id;

	[Export(PropertyHint.None, "")]
	public RichTextLabel name;

	[Export(PropertyHint.None, "")]
	public RichTextLabel hp;

	[Export(PropertyHint.None, "")]
	public RichTextLabel diag;

	[Export(PropertyHint.None, "")]
	public RichTextLabel smallHP;

	[Export(PropertyHint.None, "")]
	public Sprite2D hpBar;

	[Export(PropertyHint.None, "")]
	public Sprite2D icon;

	[Export(PropertyHint.None, "")]
	public Sprite2D smallHpBar;

	[Export(PropertyHint.None, "")]
	public Node2D battleStuff;

	[Export(PropertyHint.None, "")]
	public Node2D big;

	[Export(PropertyHint.None, "")]
	public Node2D small;

	[Export(PropertyHint.None, "")]
	private Sprite2D[] battleButtons;

	public static readonly int[][] pPos = new int[3][]
	{
		new int[5] { 110, 0, 0, 0, 0 },
		new int[5] { 35, 179, 0, 0, 0 },
		new int[5] { 1, 110, 219, 0, 0 }
	};

	private const int sizeBig = 103;

	private const int sizeSmall = 54;

	private static readonly Texture2D[] actButtons = new Texture2D[2]
	{
		GD.Load<Texture2D>("res://Sprites/Menus/Menu Sprites/DWAct.tres"),
		GD.Load<Texture2D>("res://Sprites/Menus/Menu Sprites/DWMagic.tres")
	};

	public float mesTime;

	public override void _Ready()
	{
		big.Visible = true;
		small.Visible = false;
	}

	public void Update()
	{
		if (id < SaveFile.current.activeParty.Count)
		{
			battleButtons[1].Texture = actButtons[Party.party[SaveFile.current.activeParty[id]].doMagicMenu ? 1u : 0u];
			icon.Texture = GD.Load<Texture2D>("res://Sprites/Menus/Menu Sprites/" + Party.party[SaveFile.current.activeParty[id]].id.ToString() + "Icon.tres");
			name.Text = Party.party[SaveFile.current.activeParty[id]].name;
			base.SelfModulate = Main.instance.partyColors[SaveFile.current.activeParty[id]];
			hpBar.SelfModulate = base.SelfModulate;
			battleStuff.GetChild<Control>(0).Modulate = base.SelfModulate;
			hpBar.Scale = new Vector2(Mathf.Clamp((float)Party.party[SaveFile.current.activeParty[id]].HP / (float)Party.party[SaveFile.current.activeParty[id]].MAXHP, 0f, 1f), 1f);
			hp.Text = "[center]" + ((Party.party[SaveFile.current.activeParty[id]].HP <= 0) ? "[color=red]" : "") + Party.party[SaveFile.current.activeParty[id]].HP + "/" + Party.party[SaveFile.current.activeParty[id]].MAXHP;
			smallHP.Text = hp.Text;
			smallHpBar.Scale = hpBar.Scale;
			smallHpBar.SelfModulate = hpBar.SelfModulate;
			bool flag = SaveFile.current.activeParty.Count > 3;
			diag.Size = new Vector2(flag ? 60 : 110, flag ? 16 : 8);
			diag.AutowrapMode = (TextServer.AutowrapMode)(flag ? 3 : 0);
		}
	}

	public override void _Process(double delta)
	{
		if (mesTime > 0f)
		{
			diag.Visible = true;
			mesTime -= Main.deltaTime;
			if (mesTime < 100f)
			{
				diag.SelfModulate = Main.colorClear.Lerp(Main.colorWhite, mesTime / 100f);
			}
			else
			{
				diag.SelfModulate = Main.colorWhite;
			}
		}
		else
		{
			diag.Visible = false;
		}
		if (SaveFile.current.activeParty.Count > 3)
		{
			UpdatePos();
		}
		if (BattleDR.current != null && BattleDR.current.spareAble && (BattleDR.current.blockedCommands == null || !BattleDR.current.blockedCommands.Contains(3)))
		{
			battleButtons[3].SelfModulate = Main.colorWhite.Lerp(DWMenu.instance.buttonColors[2], Mathf.Abs(Mathf.Sin(Mathf.DegToRad((float)Time.GetTicksMsec() * 0.2f))));
		}
		icon.Visible = DWMenu.instance.selectedParty != id;
	}

	private void UpdatePos()
	{
		if (BattleDR.current?.currentActor == id)
		{
			if (!big.Visible)
			{
				big.Visible = true;
			}
			if (small.Visible)
			{
				small.Visible = false;
			}
			base.Size = new Vector2(103f, base.Size.Y);
		}
		else
		{
			if (big.Visible)
			{
				big.Visible = false;
			}
			if (!small.Visible)
			{
				small.Visible = true;
			}
			base.Size = new Vector2(54f, base.Size.Y);
		}
		if (id > 0)
		{
			base.Position = new Vector2(GetPreviousSize(), base.Position.Y);
		}
		else if (SaveFile.current.activeParty.Count >= 4 && (BattleDR.current == null || BattleDR.DoingAction()))
		{
			base.Position = new Vector2((SaveFile.current.activeParty.Count == 5) ? 24 : 18, base.Position.Y);
		}
		else
		{
			base.Position = new Vector2(1f, base.Position.Y);
		}
	}

	private float GetPreviousSize()
	{
		float num = (DWMenu.instance.partyHUDs[id - 1].big.Visible ? 103 : 54);
		if (SaveFile.current.activeParty.Count == 4)
		{
			num += (float)((BattleDR.current == null || BattleDR.DoingAction()) ? 18 : 16);
			if (id >= 2)
			{
				num += 1f;
			}
		}
		return DWMenu.instance.partyHUDs[id - 1].Position.X + num;
	}

	public void UpdateOpt(int id)
	{
		for (int i = 0; i < battleButtons.Length; i++)
		{
			BattleDR current = BattleDR.current;
			if (current != null && current.blockedCommands?.Contains(i) == true)
			{
				battleButtons[i].SelfModulate = ((id == i) ? Main.colorGray : Main.colorDark);
			}
			else
			{
				battleButtons[i].SelfModulate = DWMenu.instance.buttonColors[(id == i) ? 1 : 2];
			}
		}
	}

	}