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

/// TODO alter code to player vs computer to account for differing logic
public partial class Manager : Node
{
	public bool _dead, _ready;
	public int _ballsMoving = 0, _health = 10, _healthMax;
	public string _imagePath;
	public CollisionShape2D _startArea;
	public ManagerPanel _managerPanel = null;
	public Manager _opponent;
	public List<Worker> _workers = new();
	public List<Tchotchke> _tchotckes = new();

	public override void _Ready()
	{
		_healthMax = _health;

		SetSprite("res://art/ness.png");

		_managerPanel = GetNode<ManagerPanel>("Panel");

		_managerPanel.SetManager(this);

		Worker newWorker = Globals.Instance._workerScene.Instantiate<Worker>();
		newWorker.Position = Globals.Instance._screenCenter + new Vector2(0, 100);
		newWorker._id = 1;
		newWorker._manager = this;
		AddChild(newWorker);
		_workers.Add(newWorker);

		newWorker = Globals.Instance._workerScene.Instantiate<Worker>();
		newWorker.Position = Globals.Instance._screenCenter - new Vector2(0, 100);
		newWorker._id = 2;
		newWorker._manager = this;
		AddChild(newWorker);
		_workers.Add(newWorker);

		for (int i = 0; i < _workers.Count; i++)
		{
			_workers[i]._healthBar.Position = _managerPanel.GetNode<Panel>("Team").GetNode<Panel>("T"+(i+1)).GlobalPosition;
		}

		Tchotchke newTchotchke = ResourceLoader.Load<PackedScene>("res://Gameplay/Tchotchkes/awfully_hot_coffee_pot.tscn").Instantiate<Tchotchke>();
		newTchotchke.Position =  new Vector2(Globals.Instance._screenSize.X - 100, Globals.Instance._screenCenter.Y);
		AddChild(newTchotchke);
		_tchotckes.Add(newTchotchke);
	}

	public override void _Process(double DELTA_)
	{

		if (_workers.All(w => w._placed && w._primed))
		{
			for (int i = 0; i < _workers.Count; i++)
			{
				_workers[i].Launch();
			}
		}
	}

	public void ChangeHealth(int CHANGE)
	{
		_health += CHANGE;
		_health = Math.Min(_health, _healthMax);

		if (_health < 0)
		{
			_dead = true;
			_health = 0;
		}

		GetNode<ManagerPanel>("Panel").SetValue(_health);
	}

	public void SetSprite(string PATH)
	{
		_imagePath = PATH;
	}

	
}
