#include "TabbedPanelWidget.h"
#include "StackWidget.h"
#include "HorizontalSpacer.h"
#include "VerticalSpacer.h"
#include "TextNode.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(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));
    mStack->showChild(rawPanel);

    mNavPanel->addWidget(std::move(button));
}


