#include "TabbedPanelWidget.h"
#include "StackWidget.h"
#include "HorizontalSpacer.h"
#include "VerticalSpacer.h"
#include "Button.h"

TabbedPanelWidget::TabbedPanelWidget()
    : mNavPanel(),
      mStack()
{
    auto stack = StackWidget::Create();
    mStack = stack.get();

    auto nav = HorizontalSpacer::Create();
    mNavPanel = nav.get();
    nav->SetSize({0, 0, 0, 0, 200, 0});

    auto vertSpacer = VerticalSpacer::Create();
    vertSpacer->AddWidgetWithScale(std::move(nav), 1);
    vertSpacer->AddWidgetWithScale(std::move(stack), 4);
    AddWidget(std::move(vertSpacer));
}

std::unique_ptr<TabbedPanelWidget> TabbedPanelWidget::Create()
{
    return std::make_unique<TabbedPanelWidget>();
}

StackWidget* TabbedPanelWidget::GetStack() const
{
    return mStack;
}

void TabbedPanelWidget::AddPanel(WidgetUPtr panel, const std::string& label)
{
    auto button = Button::Create();
    button->SetLabel(label);
    button->SetBackgroundColor(Color::Create(156, 156, 156));
    button->SetMargin({1, 0, 0, 1});
    auto rawPanel = panel.get();
    auto onClick = [this, rawPanel](Widget*){
        if(this && rawPanel)
        {
            this->GetStack()->ShowChild(rawPanel);
        }
    };
    button->SetOnClickFunction(onClick);
    mStack->AddWidget(std::move(panel));
    mNavPanel->AddWidget(std::move(button));
}


