using Robust.Client.AutoGenerated;
using Robust.Client.Profiling;
using Robust.Client.UserInterface.Controls;
using Robust.Client.UserInterface.XAML;
using Robust.Shared.IoC;

namespace Robust.Client.UserInterface;

[GenerateTypedNameReferences]
public sealed partial class DevWindowTabPerf : Control
{
    [Dependency] private readonly ProfViewManager _profViewMgr = default!;

    private ProfViewManager.Snapshot? _currentSnapshot;

    public DevWindowTabPerf()
    {
        RobustXamlLoader.Load(this);
        IoCManager.InjectDependencies(this);

        SnapButton.OnPressed += SnapButtonOnOnPressed;
        ButtonDeleteSnap.OnPressed += ButtonDeleteSnapOnOnPressed;
        GraphView.FrameSelected += SelectFrame;
    }

    private void SelectFrame(long obj)
    {
        if (_currentSnapshot == null)
            return;

        GraphView.HighlightFrame = obj;
        Tree.Frame = obj;
        Tree.RebuildTree();
    }

    protected override void EnteredTree()
    {
        _profViewMgr.SnapshotsUpdated += ProfViewMgrOnSnapshotsUpdated;

        UpdateSnapshotList();
    }

    protected override void ExitedTree()
    {
        _profViewMgr.SnapshotsUpdated -= ProfViewMgrOnSnapshotsUpdated;
    }

    private void ProfViewMgrOnSnapshotsUpdated()
    {
        UpdateSnapshotList();
    }

    private void SnapButtonOnOnPressed(BaseButton.ButtonEventArgs obj)
    {
        _profViewMgr.Snap();
        SnapshotSelected(_profViewMgr.Snapshots[^1]);
    }

    private void ButtonDeleteSnapOnOnPressed(BaseButton.ButtonEventArgs obj)
    {
        if (_currentSnapshot == null)
            return;

        _profViewMgr.DeleteSnapshot(_currentSnapshot);
        _currentSnapshot = null;
        UpdateRightPanel();
    }

    private void UpdateSnapshotList()
    {
        SnapList.RemoveAllChildren();

        for (var i = _profViewMgr.Snapshots.Count - 1; i >= 0; i--)
        {
            var snap = _profViewMgr.Snapshots[i];

            var button = new Button
            {
                Text = $"{snap.Identifier}: {snap.StartFrame} - {snap.EndFrame}",
                ToggleMode = true
            };

            if (snap == _currentSnapshot)
                button.Pressed = true;

            button.OnPressed += _ => SnapshotSelected(snap);

            SnapList.AddChild(button);
        }
    }

    private void SnapshotSelected(ProfViewManager.Snapshot snapshot)
    {
        _currentSnapshot = snapshot;
        UpdateSnapshotList();
        UpdateRightPanel();
        SelectFrame(_currentSnapshot.EndFrame);
        Tree.RebuildTree();
    }

    private void UpdateRightPanel()
    {
        RightPanel.Visible = _currentSnapshot != null;
        RightPanelPlaceholder.Visible = _currentSnapshot == null;

        if (_currentSnapshot == null)
            return;

        LabelTopText.Text =
            $"Snapshot: {_currentSnapshot.StartFrame} - {_currentSnapshot.EndFrame}  Frame count: {_currentSnapshot.FrameCount}";

        GraphView.LoadSnapshot(_currentSnapshot);
        Tree.LoadSnapshot(_currentSnapshot);
    }
}
