using Godot;
using System;

public partial class Cue : Sprite2D
{
	[Signal]
	public delegate void ShootEventHandler(Vector2 IMPULSE);
	
	public bool _shown;
	public int _powerDirection = 1;
	public float _power = 0.0f, _maxPower = 8.0f;
	public ProgressBar _progressBar;
	
	public static Cue Create(string SCENENAME)
	{
		PackedScene scene = ResourceLoader.Load<PackedScene>("res://Gameplay/"+SCENENAME+".tscn");
		Cue newCue = scene.Instantiate<Cue>();
		Texture2D image = GD.Load<Texture2D>("res://art/cue.png");
		newCue.Texture = image;
		return newCue;
	}
	
	public override void _Ready()
	{
		//_progressBar = GetParent().GetNode<ProgressBar>("PowerBar");
	}
	
	public override void _Process(double DELTA_)
	{
		Vector2 mousePosition = GetViewport().GetMousePosition();
		LookAt(mousePosition);
		if (Input.IsActionPressed("left_click"))
		{
			_power += 0.1f * _powerDirection;
			if (_power >= _maxPower)
			{
				_powerDirection = -1;
			}
			else if (_power <= 0)
			{
				_powerDirection = 1;
			}
		}
		else
		{
			if (_power > 0f)
			{
				_powerDirection = 1;
				Vector2 direction = mousePosition - Position;
				EmitSignal(SignalName.Shoot, _power * direction);
				_power = 0;
			}
		}
		
	}
	
	public void HideCue()
	{
		_shown = false;
		SetProcess(false);
		Hide();
		//_progressBar.Hide();
	}
	
	public void ShowCue(Ball CUEBALL)
	{
		_shown = true;
		SetProcess(true);
		Position = CUEBALL.Position;
		//_progressBar.Position = new Vector2(CUEBALL.Position.X - _progressBar.Size.X / 2, CUEBALL.Position.Y + _progressBar.Size.Y / 2);
		Show();
		//_progressBar.Show();
	}
	
}
