using UnityEngine;
using UnityEngine.InputSystem;

[RequireComponent(typeof(PlayerInput))]
public class Player : MonoBehaviour {
	public GameObject labelPrefab;
	public GameObject bombPrefab;
	public Arena arena;

	int strength = 1;

	void Start() {
		Transform canvas = GameObject.Find("Canvas").transform;
		PlayerLabel label = Instantiate(labelPrefab, canvas).GetComponent<PlayerLabel>();

		var playerInput = GetComponent<PlayerInput>();

		label.player = transform;
		label.SetName(gameObject.name);
		label.SetController(playerInput.currentControlScheme);

		playerInput.actions["Place"].performed += OnPlace;
	}

	void OnPlace(InputAction.CallbackContext ctx) {
		var bomb = Instantiate(bombPrefab, arena.GetClosestTilePos(transform.position) - Vector3.up * 0.5f, bombPrefab.transform.rotation);
		bomb.GetComponent<Bomb>().Init(strength, arena);
	}
}