using System.Collections.Generic;
using InABox.Core;
using InABox.DynamicGrid;

namespace InABox.Wpf;

public interface IMasterDetailPanelPage
{
    IBasePanel? Panel { get; }
}

public abstract class MasterDetailPanelPage<TMaster,TPanel> : MasterDetailPage<TMaster>, IMasterDetailPanelPage
    where TPanel : class, IBasePanel, IMasterDetailControl<TMaster>, new()
{
    protected MasterDetailPanelPage(DynamicTabItem tab) : base(tab) { }
    
    public TPanel? Panel { get; set; }

    IBasePanel? IMasterDetailPanelPage.Panel => Panel;
    
    public override Dictionary<string, object[]>? Selected() => Panel?.Selected();

    protected abstract void DoRefresh(TPanel? panel);

    public override IDataModelSource DataModelSource() => CheckPanel();
    
    protected override IDataModelSource? Refresh()
    {
        
        CheckPanel();
        DoRefresh(Panel);

        return Panel;
    }

    private IDataModelSource CheckPanel()
    {
        if (Panel == null)
        {
            Panel = new TPanel
            {
                IsReady = false
            };
            Panel.Setup();
            Panel.IsReady = true;
            Tab.Content = Panel;
        }

        Panel.Master = Master;
        return Panel;
    }
}