using Godot;
using System;

namespace SessionZeroApp;

public partial class MainUILogic : Control
{
    [ExportCategory("Node Exports")]
	[Export] public TextureButton HamburgerButton;
	[Export] public Panel SideNavPanel;
	[Export] public MarginContainer MainContentContainer;
    [ExportGroup("NavPanelButtons")]
    [Export] private Button _homeNavButton;
	[Export] private Button _libraryNavButton;
	[Export] private Button _charactersNavButton;
	[Export] private Button _advancedNavButton;
	[Export] private Button _profileNavButton;
	[Export] private Button _settingsNavButton;

    [ExportCategory("Behaviour")]
	[Export] private int _navPanelExpandedSize = 60;

	private bool _sideNavToggled = true;
	private Tween _sideNavTween;
	private VBoxContainer _sideNavVBox;

	public override void _Ready()
	{
		_sideNavVBox = SideNavPanel.GetNode<VBoxContainer>("VBoxContainer");
		
		SetupButtonConnections();
    }

	
	private void SetupButtonConnections()
	{
		HamburgerButton.Pressed += () => {ToggleSideNav();};

        // Nav panel buttons
        _homeNavButton.Pressed += () => { AppManager.Instance.LoadPage("Home"); };
        _libraryNavButton.Pressed += () => { AppManager.Instance.LoadPage("Library"); };
        _charactersNavButton.Pressed += () => { AppManager.Instance.LoadPage("Characters"); };
        _advancedNavButton.Pressed += () => { AppManager.Instance.LoadPage("Advanced"); };
        _profileNavButton.Pressed += () => { AppManager.Instance.LoadPage("Profile"); };
        _settingsNavButton.Pressed += () => { AppManager.Instance.LoadPage("Settings"); };

    }


	private void ToggleSideNav()
	{
		_sideNavTween?.Kill();
		_sideNavTween = CreateTween();

		float targetSize = _sideNavToggled ? 0 : _navPanelExpandedSize;
		float targetAlpha = _sideNavToggled ? 0.0f : 1.0f;
		
		_sideNavTween.SetParallel(true);

		_sideNavTween.TweenProperty(SideNavPanel, "custom_minimum_size:x", targetSize, 0.3f)
			.SetEase(Tween.EaseType.InOut)
			.SetTrans(Tween.TransitionType.Cubic);

		_sideNavTween.TweenProperty(_sideNavVBox, "modulate:a", targetAlpha, 0.2f);	

		_sideNavToggled = !_sideNavToggled;
	}

	public void LoadPageContent(Control pageContent)
	{
		if (MainContentContainer.GetChildCount() > 0)
		{
			MainContentContainer.RemoveChild(MainContentContainer.GetChild(0));
		}

        MainContentContainer.AddChild(pageContent);
    }
}
