using Godot;
using Sfs3.Client.Entities;
using Sfs3.Client.Entities.Managers;
using Sfs3.Client.Entities.Variables;
using System;

/**
 * Script attached to the main node in the ProfileConfiguratorPanel scene.
 */
public partial class ProfileConfiguratorPanel : Control
{
	[Export]
	public Label usernameLabel;
	[Export]
	public CheckButton onlineCheck;
	[Export]
	public LineEdit nicknameInput;
	[Export]
	public OptionButton stateSelect;
	[Export]
	public ExperienceInput playerExpInput;

	private Action<bool> onlineChangeAction;
	private Action<string, object> buddySettingChangeAction;
	private Action<string, object> playerSettingChangeAction;

	public Action<bool> OnlineChangeAction
	{
		get => onlineChangeAction;
		set => onlineChangeAction = value;
	}

	public Action<string, object> BuddySettingChangeAction
	{
		get => buddySettingChangeAction;
		set => buddySettingChangeAction = value;
	}

	public Action<string, object> PlayerSettingChangeAction
	{
		get => playerSettingChangeAction;
		set => playerSettingChangeAction = value;
	}

	public int PlayerExperience => playerExpInput.Experience;

	public void DisplayUserName(string name)
	{
		usernameLabel.Text = name;
	}

	/**
	 * Show the profile settings related to the Buddy List system.
	 */
	public void InitBuddyProfile(IBuddyManager sfsBuddyManager)
	{
		// User online/offline state
		onlineCheck.Disabled = false;
		onlineCheck.SetPressedNoSignal(sfsBuddyManager.MyOnlineState);
		EnableBuddyControls(sfsBuddyManager.MyOnlineState);

		// User nickname
		nicknameInput.Text = sfsBuddyManager.MyNickname ?? "";

		// Available states and current user state
		stateSelect.Clear();

		int selectedIndex = -1;
		for (int i = 0; i < sfsBuddyManager.BuddyStates.Count; i++)
		{
			string state = sfsBuddyManager.BuddyStates[i];
			stateSelect.AddItem(state);

			if (state == sfsBuddyManager.MyState)
				selectedIndex = i;
		}
		stateSelect.Select(selectedIndex);
	}

	/**
	 * Show the player details (used in invitations, matchmaking, etc).
	 */
	public void InitPlayerProfile(User user)
	{
		// User variable: experience
		playerExpInput.Experience = (int)user.GetVariable(LobbyController.USERVAR_EXPERIENCE).IntValue;
	}

	//----------------------------------------------------------
	// UI event listeners
	//----------------------------------------------------------
	#region
	public void OnCloseButtonClick()
	{
		Visible = false;
	}

	public void OnOnlineToggle(bool isOn)
	{
		onlineChangeAction?.Invoke(isOn);

		// Enable/disable controls
		EnableBuddyControls(isOn);
	}

	public void OnNicknameSubmit(string nickname)
	{
		buddySettingChangeAction?.Invoke(ReservedBuddyVariables.BV_NICKNAME, nickname);
	}

	public void OnNicknameFocusExit()
	{
		OnNicknameSubmit(nicknameInput.Text);
	}

	public void OnStateSelected(int index)
	{
		buddySettingChangeAction?.Invoke(ReservedBuddyVariables.BV_STATE, stateSelect.GetItemText(index));
	}

	public void OnPlayerExperienceChanged(int value)
	{
		playerSettingChangeAction?.Invoke(LobbyController.USERVAR_EXPERIENCE, value);
	}
	#endregion

	//----------------------------------------------------------
	// Helper methods
	//----------------------------------------------------------
	#region
	private void EnableBuddyControls(bool enable)
	{
		nicknameInput.Editable = enable;
		stateSelect.Disabled = !enable;
	}
	#endregion
}
