using Godot;
using SharpIDE.Application.Features.SolutionDiscovery.VsPersistence;
using SharpIDE.Godot.Features.IdeDiagnostics;
using SharpIDE.Godot.Features.Problems;

namespace SharpIDE.Godot.Features.BottomPanel;

public partial class BottomPanelManager : Panel
{
    public SharpIdeSolutionModel? Solution
    {
        get;
        set
        {
            field = value;
            _problemsPanel.Solution = value;
        }
    }

    private Control _runPanel = null!;
    private Control _debugPanel = null!;
    private Control _buildPanel = null!;
    private ProblemsPanel _problemsPanel = null!;
    private IdeDiagnosticsPanel _ideDiagnosticsPanel = null!;

    private Dictionary<BottomPanelType, Control> _panelTypeMap = [];
    
    public override void _Ready()
    {
        _runPanel = GetNode<Control>("%RunPanel");
        _debugPanel = GetNode<Control>("%DebugPanel");
        _buildPanel = GetNode<Control>("%BuildPanel");
        _problemsPanel = GetNode<ProblemsPanel>("%ProblemsPanel");
        _ideDiagnosticsPanel = GetNode<IdeDiagnosticsPanel>("%IdeDiagnosticsPanel");
        
        _panelTypeMap = new Dictionary<BottomPanelType, Control>
        {
            { BottomPanelType.Run, _runPanel },
            { BottomPanelType.Debug, _debugPanel },
            { BottomPanelType.Build, _buildPanel },
            { BottomPanelType.Problems, _problemsPanel },
            { BottomPanelType.IdeDiagnostics, _ideDiagnosticsPanel }
        };

        GodotGlobalEvents.Instance.BottomPanelTabSelected.Subscribe(OnBottomPanelTabSelected);
    }

    public override void _ExitTree()
    {
        GodotGlobalEvents.Instance.BottomPanelTabSelected.Subscribe(OnBottomPanelTabSelected);
    }

    private async Task OnBottomPanelTabSelected(BottomPanelType? type)
    {
        await this.InvokeAsync(() =>
        {
            if (type == null)
            {
                GodotGlobalEvents.Instance.BottomPanelVisibilityChangeRequested.InvokeParallelFireAndForget(false);
            }
            else
            {
                GodotGlobalEvents.Instance.BottomPanelVisibilityChangeRequested.InvokeParallelFireAndForget(true);
            }
            foreach (var kvp in _panelTypeMap)
            {
                kvp.Value.Visible = kvp.Key == type;
            }
        });
    }
}