using Godot;
using System;
using System.Data;

public partial class Ball : RigidBody2D
{
	public bool _placed, _potted, _available, _hovered, _selected, _aimed, _moving;
	public float _moveThreshold = 5.0f;
	
	public static Ball Create(string SCENENAME, int NUMBER)
	{
		PackedScene scene = ResourceLoader.Load<PackedScene>("res://Gameplay/"+SCENENAME+".tscn");
		Ball newBall = scene.Instantiate<Ball>();
		string fileName;
		if (NUMBER == 0)
		{
			fileName = "res://art/cue_ball.png";
		}
		else
		{
			fileName = "res://art/ball_"+NUMBER+".png";
		}
		Texture2D image = GD.Load<Texture2D>(fileName);
		newBall.GetNode<Sprite2D>("Image").Texture = image;
		return newBall;
	}
	
	public override void _Ready()
	{
		_available = true;
		_hovered = false;
		_selected = false;
		_aimed = false;
		_moving = false;
	}
	
	public override void _Process(double DELTA_)
	{
		if (LinearVelocity.Length() > 0 && LinearVelocity.Length() < _moveThreshold)
		{
			Sleeping = true;
			if (_moving)
			{
				_moving = false;
			}
		}
		else if (LinearVelocity.Length() >= _moveThreshold)
		{
			GD.Print(Position);
			if (!_moving)
			{
				_moving = true;
			}
		}
		
		if (Globals.Instance._anyMovement)
		{
			if (_available)
			{
				_available = false;
			}
		}
		else
		{
			if (!_available)
			{
				_available = true;
			}
		}	
	}
	
	public void Place(Vector2 POSITION)
	{
		_placed = true;
		Position = POSITION;
	}
	
	private void OnMouseEntered()
	{
		_hovered = true;
	}
	
	private void OnMouseExited()
	{
		_hovered = false;
	}
	
}
