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

public partial class BuddyListItem : PanelContainer
{
	[Export]
	public TextureRect buddyUnblockedIconRect;
	[Export]
	public TextureRect buddyBlockedIconRect;
	[Export]
	public Label nameLabel;
	[Export]
	public Label ageLabel;
	[Export]
	public Label moodLabel;
	[Export]
	public TextureRect buddyTemporaryIconRect;
	[Export]
	public Button chatButton;
	[Export]
	public Button inviteButton;
	[Export]
	public Button blockButton;
	[Export]
	public Button unblockButton;
	[Export]
	public Button removeButton;
	[Export]
	public Button addButton;
	[Export]
	public Panel unreadMsgContainer;
	[Export]
	public Label unreadMsgLabel;

	private bool buddyBlocked = false;

	public bool BuddyBlocked => buddyBlocked;
	
	public void Update(Buddy buddy)
	{
		// Nickname
		nameLabel.Text = buddy.DisplayName;

        // Age
        DateTime now = DateTime.Now;
		var yearVar = buddy.GetVariable(LobbyController.BUDDYVAR_YEAR);
		ageLabel.Text = (yearVar != null && !yearVar.IsNull) ? String.Format("({0} yo)", now.Year - yearVar.IntValue) : "";
		ageLabel.Visible = ageLabel.Text != "";

		// Mood
		var moodVar = buddy.GetVariable(LobbyController.BUDDYVAR_MOOD);
		bool isValidMood = moodVar != null && !moodVar.IsNull && moodVar.StringValue != "";
		moodLabel.Text = isValidMood ? moodVar.StringValue : "";
		moodLabel.Visible = isValidMood;

        // Temporary
		buddyTemporaryIconRect.Visible = buddy.IsTemp;

        // Save blocked state
        // (see BuddyList.UpdateListItem method)
        buddyBlocked = buddy.IsBlocked;

		// If buddy is not blocked and is temporary, show add button and hide remove button
		bool showAddButton = !buddy.IsBlocked && buddy.IsTemp;
		addButton.Visible = showAddButton;
		removeButton.Visible = !showAddButton;

        // Status icon and buttons
        if (buddy.IsBlocked)
		{
			// State
			buddyBlockedIconRect.Visible = true;
			buddyUnblockedIconRect.Visible = false;
			buddyBlockedIconRect.Modulate = new Color(0f, 0f, 0f, 0.25f); // Black, sempi-transparent

			// Block/unblock buttons
			blockButton.Visible = false;
			unblockButton.Visible = true;

			// Chat button & invite button
			chatButton.Disabled = true;
			inviteButton.Disabled = true;
			UpdateUnreadMsgCounter(0);
        }
        else
		{
            // State
			buddyBlockedIconRect.Visible = false;
			buddyUnblockedIconRect.Visible = true;

            // Block/unblock buttons
            blockButton.Visible = true;
			unblockButton.Visible = false;

            if (!buddy.IsOnline)
			{
                // State
				buddyUnblockedIconRect.Modulate = new Color(0f, 0f, 0f, 0.25f); // Black, sempi-transparent

				// Chat button & invite button
				chatButton.Disabled = true;
				inviteButton.Disabled = true;
				UpdateUnreadMsgCounter(0);
            }
            else
            {
                // State
                var state = buddy.State;

                if (state == "Available")
                    buddyUnblockedIconRect.Modulate = new Color(0f, 0.8f, 0f, 1f); // Green
                else if (state == "Away")
                    buddyUnblockedIconRect.Modulate = new Color(1f, 0.8f, 0f, 1f); // Yellow
                else if (state == "Occupied")
					buddyUnblockedIconRect.Modulate = new Color(0.8f, 0f, 0f, 1f); // Red

                // Chat button & invite button
                chatButton.Disabled = false;
				inviteButton.Disabled = false;
            }
        }
    }

	public void UpdateUnreadMsgCounter(int cnt)
	{
		unreadMsgLabel.Text = cnt.ToString();
		unreadMsgContainer.Visible = cnt > 0;
	}
}
