using System.Collections.Generic;
using UnityEngine;
using UnityEngine.InputSystem;
using UnityEngine.SceneManagement;

public class GameManager : MonoBehaviour
{
    public static GameManager instance;
    public Dictionary<string, PlayerController> playerControllers = new Dictionary<string, PlayerController>();
    public List<BoxController> boxControllers = new List<BoxController>();

    private string ActivePlayerId = "Snail";

    private bool ActivePlayerSwitching = false;

    private void Awake()
    {
        GameManager.instance = this;
    }

    private void Update()
    {
        // Press R to reset the game
        if (Keyboard.current.rKey.wasPressedThisFrame)
        {
            ResetGame();
        }
    }

    public void ResetGame()
    {
        Debug.Log("Resetting game...");
        Scene currentScene = SceneManager.GetActiveScene();
        SceneManager.LoadScene(currentScene.name);
    }

    public void AttachPlayerController(string playerId, PlayerController controller)
    {
        this.playerControllers[playerId] = controller;
    }

    public void AttachBoxController(BoxController controller)
    {
        this.boxControllers.Add(controller);
    }

    public BoxController[] GetBotControllers()
    {
        return this.boxControllers.ToArray();
    }

    public PlayerController GetActivePlayer()
    {
        return this.playerControllers[ActivePlayerId];
    }

    public string GetActivePlayerId()
    {
        return this.ActivePlayerId;
    }

    public void SetActivePlayerId(string activePlayerId)
    {
        this.ActivePlayerId = activePlayerId;
    }

    public bool GetActivePlayerSwitching()
    {
        return this.ActivePlayerSwitching;
    }

    public void SetActivePlayerSwitching(bool activePlayerSwitching)
    {
        this.ActivePlayerSwitching = activePlayerSwitching;
    }

    public Vector3 GetPlayerPosition()
    {
        return this.playerControllers[this.ActivePlayerId]!.GetPosition();
    }
}

