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

public partial class Actor : Node
{
	public bool _available, _hovered, _selected;
	public List<Cue> _cues = new();
	public Cue _selectedCue = null;
	public List<Ball> _balls = new();
	public List<Ball> _ballBag = new();
	public Ball _hoveredBall = null;
	public Ball _selectedBall = null;
	public Ball _heldBall = null;

	public Sprite2D _tempBallSprite = new();
	
	public static Actor Create(string SCENENAME)
	{
		PackedScene scene = ResourceLoader.Load<PackedScene>("res://Gameplay/" + SCENENAME + ".tscn");
		Actor newActor = scene.Instantiate<Actor>();
		return newActor;
	}

	public override void _Ready()
	{
		Ball newBall = Ball.Create("ball", 0);
		_balls.Add(newBall);
		newBall = Ball.Create("ball", 1);
		_balls.Add(newBall);

		Cue newCue = Cue.Create("cue");
		AddChild(newCue);
		_cues.Add(newCue);
		for (int i = 0; i < _cues.Count; i++)
		{
			_cues[i].Shoot += OnCueShoot;
		}

		_ballBag = new(_balls);
	}

	public override void _Process(double DELTA_)
	{
		if (_ballBag.Count > 0)
		{
<<<<<<< HEAD
			if (_heldBall == null)
			{
				_heldBall = _ballBag[0];
			}
			else if (Input.IsActionJustPressed("scroll_up"))
			{
				_heldBall = _ballBag[(_ballBag.IndexOf(_heldBall) + 1) % _ballBag.Count];
			}
			else if (Input.IsActionJustPressed("scroll_down"))
			{
				_heldBall = _ballBag[(_ballBag.IndexOf(_heldBall) - 1) < 0 ? ^1 : (_ballBag.IndexOf(_heldBall) - 1)];
			}
			Vector2 mousePosition = GetViewport().GetMousePosition();
			if (_tempBallSprite.Texture == null)
			{
				if (GetChildren().All(n => n != _tempBallSprite))
				{
					AddChild(_tempBallSprite);
				}
				_tempBallSprite.Texture = _heldBall.GetNode<Sprite2D>("Image").Texture;
			}
			_tempBallSprite.Position = mousePosition;
			if (Input.IsActionJustReleased("left_click"))
			{
				_heldBall.Place(mousePosition);
				AddChild(_heldBall);
				int ballIndex = _ballBag.IndexOf(_heldBall);
				_ballBag.Remove(_heldBall);
				_tempBallSprite.Texture = null;
				if (_ballBag.Count > 0)
				{
					_heldBall = _ballBag[ballIndex - (ballIndex > _ballBag.Count ? 1 : 0)];
				}
=======
			_activeCue.HideCue();
			Vector2 mousePosition = GetViewport().GetMousePosition();
			GetNode<Marker2D>("StartPosition").Position = mousePosition;
			if (Input.IsActionJustReleased("left_click"))
			{
				_activeBall.Place(GetNode<Marker2D>("StartPosition").Position);
				GD.Print(_activeBall.Position);
>>>>>>> 920c5c27fb732c902987b01dcc093e55b1552a9f
			}
		}
		else
		{
			if (_balls.Any(b => b._hovered))
			{
				_hoveredBall = _balls.Single(b => b._hovered);
			}
			else
			{
				_hoveredBall = null;
			}

			if (_selectedCue == null && _cues.Count > 0)
			{
				_selectedCue = _cues[0];
			}

			if ((_selectedBall == null || _selectedBall != _hoveredBall) && (_hoveredBall?._available ?? false))
			{
				if (Input.IsActionJustReleased("left_click"))
				{
					GD.Print(_selectedBall);
					_selectedBall = _hoveredBall;
					_selectedBall._selected = true;
					_selectedCue.Don(_selectedBall);
					GD.Print(_selectedBall);
				}
			}
			else if (Input.IsActionJustReleased("right_click"))
			{
				GD.Print(2);
				_selectedBall._selected = false;
				_selectedBall = null;
				_selectedCue.Doff();
			}
			else if (_hoveredBall == null)
			{
				if (Input.IsActionJustReleased("left_click") && _selectedCue._power == 0)
				{
					GD.Print(3);
					_selectedBall._selected = false;
					_selectedBall = null;
					_selectedCue.Doff();
				}
			}
		}


	}

	//public void ResetCueBall()
	//{
	//_cueBall = Ball.Create("ball", 0, _startPosition);
	//_cueBall.SetName("CueBall");
	//AddChild(_cueBall);
	//Texture2D image = GD.Load<Texture2D>("res://art/cue_ball.png");
	//_cueBall.GetNode<Sprite2D>("Image").Texture = image;
	//_cueBall._placed = true;
	//_balls = GetTree().GetNodesInGroup("balls").Select(b => (Ball)b).ToList<Ball>();
	//}



	private void OnCueShoot(Vector2 IMPULSE)
	{
		if (_selectedBall != null && _selectedBall._placed)
		{
			_selectedBall.ApplyCentralImpulse(IMPULSE);
			_selectedBall._selected = false;
			_selectedBall = null;
			_selectedCue.Doff();
		}
	}
}
