using UnityEngine;
using UnityEngine.UI;

using Sfs2X;
using Sfs2X.Util;

/**
 * Script attached to the Controller object in the Login scene.
 */
public class LoginSceneController : BaseSceneController
{
	//----------------------------------------------------------
	// Editor public properties
	//----------------------------------------------------------

	[Tooltip("IP address or domain name of the SmartFoxCluster Lobby instance")]
	public string host = "127.0.0.1";

	[Tooltip("Display SmartFoxServer client debug messages")]
	public bool debug = false;

	//----------------------------------------------------------
	// UI elements
	//----------------------------------------------------------

	public InputField nameInput;
	public Button loginButton;
	public WarningPanel warningPanel;

    //----------------------------------------------------------
    // Private properties
    //----------------------------------------------------------

    public const int TCP_PORT = 9933;		// Predefined TCP port in Overcast
    public const int HTTP_PORT = 8080;      // Predefined HTTP port in Overcast
    public const string ZONE = "Cluster";   // Predefined Zone name for SmartFoxCluster's Lobby

    private SmartFox sfsLobby;

	//----------------------------------------------------------
	// Unity callback methods
	//----------------------------------------------------------

	private void Start()
	{
		Debug.Log("LOGIN SCENE - Scene loaded");

		// Focus on username input
		nameInput.Select();
		nameInput.ActivateInputField();

		// Reset buddy chat history
		BuddyChatHistoryManager.Init();

		// Check if a connection lost message should be displayed
		CheckConnectionLost();
	}

	//----------------------------------------------------------
	// Abstract methods from BaseSceneController implementation
	//----------------------------------------------------------
	#region
	override protected void RemoveSmartFoxListeners()
	{
		// Nothing to do, this scene didn't add dedicated listeners
	}

	override public void ShowError(string errorMsg)
	{
		// Display connection or login error
		warningPanel.Show(errorMsg);

		// Enable user interface
		EnableUI(true);
	}

	override protected void HideModals()
	{
		warningPanel.Hide();
	}
	#endregion

	//----------------------------------------------------------
	// UI event listeners
	//----------------------------------------------------------
	#region
	/**
	 * On username input edit end, if the Enter key was pressed, connect to SmartFoxServer.
	 */
	public void OnNameInputEndEdit()
	{
		if (Input.GetKeyDown(KeyCode.Return) || Input.GetKeyDown(KeyCode.KeypadEnter))
			Connect();
	}

	/**
	 * On Login button click, connect to SmartFoxServer.
	 */
	public void OnLoginButtonClick()
	{
		Connect();
	}
	#endregion

	//----------------------------------------------------------
	// Helper methods
	//----------------------------------------------------------
	#region
	/**
	 * Enable/disable username input interaction.
	 */
	private void EnableUI(bool enable)
	{
		nameInput.interactable = enable;
		loginButton.interactable = enable;
	}

	/**
	 * Connect to SmartFoxServer.
	 */
	private void Connect()
	{
		// Disable user interface
		EnableUI(false);

		// Set connection parameters
		ConfigData cfg = new ConfigData();
		cfg.Host = host;
		cfg.Port = TCP_PORT;
		cfg.Zone = ZONE;
		cfg.Debug = debug;

#if UNITY_WEBGL
		cfg.Port = HTTP_PORT;
#endif

		// Retrieve SmartFox instance from Global Manager
		// The username is passed because the Global manager will take care of the login step automatically
		sfsLobby = gm.CreateLobbyClient(nameInput.text);

		// NOTE
		// If this scene needed its own SmartFox event listeners, they should be added now
		// In such case, listeners must be removed in the RemoveSmartFoxListeners method above

		// Configure SmartFox internal logger
		sfsLobby.Logger.EnableConsoleTrace = debug;

		// Connect to SmartFoxServer
		sfsLobby.Connect(cfg);
	}
	#endregion
}
