using UnityEngine;

/**
 * Parent class of the script attached to the Controller object in all scenes.
 */
public abstract class BaseSceneController : MonoBehaviour
{
	protected GlobalManager gm;

	/**
	 * On awake, set a reference to Global Manager.
	 */
	protected virtual void Awake()
	{
		// Get Global Manager instance
		gm = GlobalManager.Instance;

        // Set reference to scene controller in Global Manager
        gm.SetCurrentSceneController(this);
	}

	/**
	 * On update, hide modals if ESC button is pressed.
	 */
	protected virtual void Update()
	{
		// On ESC button press, hide modals
		if (Input.GetKeyDown("escape"))
			HideModals();
	}

	/**
	 * When a scene is destroyed, make sure all event listeners it added to the SmartFox class instance are removed.
	 */
	protected virtual void OnDestroy()
	{
		RemoveSmartFoxListeners();
	}

	/**
	 * Called by scene controllers to check if a connection error message should be displayed on load.
	 */
	protected void CheckConnectionLost()
	{
		// Retrieve the last connection lost message from Global Manager
		string connLostMessage = gm.ConnectionLostMessage;

		if (connLostMessage != null)
			ShowError(connLostMessage);
	}

    /**
	 * Abstract method to be implemented by controller classes, to remove all SmartFox-related event listeners they added.
	 */
    protected abstract void RemoveSmartFoxListeners();

    /**
	 * Abstract method to be implemented by controller classes, to display connection-related errors.
	 */
    public abstract void ShowError(string errorMsg);

    /**
	 * Abstract method to be implemented by controller classes, to hide popup modals.
	 */
    protected abstract void HideModals();

    /**
	 * Virtual method to be overridden by the controller classes which need to react to a failed attempt to join a game (typically the lobby scene controller).
	 * Reason of the failed attempt could be: load balancer error (i.e. game node not found), game node connection or login failed, game room creation or join failed.
	 * Controller should show an error or retry the game join attempt (send the ClusterJoinOrCreateRequest again).
	 */
    public virtual void OnGameJoinFailed()
	{
		// Nothing to do, must be overridden where needed
	}
}
