using System;
using System.Collections.Generic;

using UnityEngine;
using UnityEngine.Events;
using UnityEngine.UI;

using Sfs2X.Entities;
using Sfs2X.Entities.Managers;
using Sfs2X.Entities.Variables;

/**
 * Script attached to User Profile Panel prefab.
 */
public class UserProfilePanel : BasePanel
{
	public Text usernameLabel;

	public Toggle onlineToggle;
	public InputField nickInput;
	public Dropdown stateDropdown;

	public ExperienceInput expInput;

	public UnityEvent<bool> onOnlineToggleChange;
	public UnityEvent<string, object> onBuddyDetailChange;
	public UnityEvent<string, object> onPlayerDetailChange;

	/**
	 * Show the generic user profile details.
	 */
	public void InitUserProfile(string username)
	{
		// Username
		usernameLabel.text = "Username: <b>" + username + "</b>";
	}

	/**
	 * Show the profile details related to the buddy list system.
	 */
	public void InitBuddyProfile(IBuddyManager buddyManager)
    {
		// User online/offline state
		onlineToggle.isOn = buddyManager.MyOnlineState;

		// User nickname
		nickInput.text = (buddyManager.MyNickName != null ? buddyManager.MyNickName : "");

		// Available states and current user state
		stateDropdown.AddOptions(buddyManager.BuddyStates);
		stateDropdown.SetValueWithoutNotify(buddyManager.BuddyStates.IndexOf(buddyManager.MyState));
	}

	/**
	 * Show the player details (used invitations, quick-join, etc).
	 */
	public void InitPlayerProfile(User user)
	{
		// User variable: experience
		expInput.Experience = user.GetVariable(LobbySceneController.USERVAR_EXPERIENCE).GetIntValue();
	}

	/**
	 * Dispatch a custom event to set the user's online/offline state in the buddy list system.
	 */
	public void OnOnlineToggleChange(bool isChecked)
	{
		// Dispatch event
		onOnlineToggleChange.Invoke(isChecked);

		nickInput.interactable = isChecked;
		stateDropdown.interactable = isChecked;
	}

	/**
	 * Dispatch a custom event to set the user's nickname in the buddy list system.
	 */
	public void OnNickInputEnd()
	{
		// Dispatch event
		onBuddyDetailChange.Invoke(ReservedBuddyVariables.BV_NICKNAME, nickInput.text);
	}

	/**
	 * Dispatch a custom event to set the user's state in the buddy list system.
	 */
	public void OnStateDropdownChange()
	{
		// Dispatch event
		onBuddyDetailChange.Invoke(ReservedBuddyVariables.BV_STATE, stateDropdown.options[stateDropdown.value].text);
	}

	/**
	 * Dispatch a custom event to set the user's experience in User Variables.
	 */
	public void OnExperienceChange()
	{
		// Dispatch event
		onPlayerDetailChange.Invoke(LobbySceneController.USERVAR_EXPERIENCE, expInput.Experience);
	}
}
