using System;
using System.Collections.Generic;
using System.Linq;
using Godot;

namespace Cirno.Scripts;

public static class Tools
{
	public static T CreateChild<T>(this Node2D node, PackedScene prefab) where T : Node2D
	{
		return CreateChild<T>(node, prefab, node.GlobalPosition);
	}

	public static T CreateChild<T>(this Node2D node, PackedScene prefab, Vector2 position) where T : Node2D
	{
		return CreateChildOf<T>(node, node, prefab, position);
		
		// if (prefab == null) return null;
		// var newInstance = prefab.Instantiate<T>();
		// node.GetParent().CallDeferred("add_child", newInstance);
		// // Need to use parent instead of owner because tilemap scenes have no owner
		// //node.Owner.CallDeferred("add_child", newInstance);
		// newInstance.Transform = node.GlobalTransform;
		// newInstance.Position = position;
		//
		// return newInstance;
	}
	
	public static T CreateSibling<T>(this Node2D node, PackedScene prefab) where T : Node2D
	{
		return CreateChildOf<T>(node, node.GetParent<Node2D>(), prefab, node.GlobalPosition);
	} 
	
	public static T CreateSibling<T>(this Node2D node, PackedScene prefab,  Vector2 position) where T : Node2D
	{
		return CreateChildOf<T>(node, node.GetParent<Node2D>(),  prefab, position);
	} 

	public static T CreateChildOf<T>(this Node2D node, Node2D parentNode, PackedScene prefab) where T : Node2D
	{
		return CreateChildOf<T>(node, parentNode, prefab, node.GlobalPosition);
	}
	
	public static T CreateChildOf<T>(this Node2D node, Node2D parentNode, PackedScene prefab, Vector2 position) where T : Node2D
	{
		if (prefab == null)
		{
			GD.PrintErr("Tried to instantiate a null prefab");
			return null;
		}
		
		if (parentNode == null)
		{
			GD.PrintErr("Tried to instantiate child of a null parent");
			return null;
		}
		
		var newInstance = prefab.Instantiate<T>();
		//node.GetParent().CallDeferred("add_child", newInstance);
		parentNode.CallDeferred("add_child", newInstance);
		//newInstance.Transform = node.GlobalTransform;
		newInstance.Position = parentNode.ToLocal(position);

		return newInstance;
	}

	public static GameManager GetGameManager(this Node2D node)
	{
		return node.GetTree().Root.GetNode<GameManager>("GameScene");
		// node.GetNode<GameManager>("/root/GameScene");
	}

	public static AlarmManager GetAlarmManager(this Node2D node)
	{
		return node.GetNodeOrNull<AlarmManager>("/root/GameScene/AlarmManager");
	}

	public static InventoryManager GetInventoryManager(this Node2D node)
	{
		return node.GetNodeOrNull<InventoryManager>("/root/GameScene/InventoryManager");
	}
	
	public static Vector2 GetSnappedDirection(Vector2 direction)
	{
		// Snap to one of 8 directions
		var angles = new[]
		{
			Vector2.Up, Vector2.Up + Vector2.Right, Vector2.Right, Vector2.Right + Vector2.Down,
			Vector2.Down, Vector2.Down + Vector2.Left, Vector2.Left, Vector2.Left + Vector2.Up
		};
        
		return angles[Mathf.PosMod((int)(direction.X + 4), 8)].Normalized();
	}
	
	public static Vector2 SnapToCardinal(this Vector2 input)
	{
		if (Mathf.Abs(input.X) > Mathf.Abs(input.Y))
		{
			return new Vector2(Mathf.Sign(input.X), 0); // Left or Right
		}
		else
		{
			return new Vector2(0, Mathf.Sign(input.Y)); // Up or Down
		}
	}
	
	public static T PickRandom<T>(this IEnumerable<T> list)
	{
		var arr = list.ToArray();
		return arr[GD.RandRange(0, arr.Length -1)];
	}
}
