// Fill out your copyright notice in the Description page of Project Settings.


#include "Slate/CommonBtn/SCsdkCommonBtns.h"
#include "SlateOptMacros.h"
#include "Blueprint/UserWidget.h"
#include "Slate/CommonBtn/SCsdkBaseBtn.h"
#include "Widget/CommonBtn/CsdkCommonBtns.h"
#include "Widgets/Images/SImage.h"

BEGIN_SLATE_FUNCTION_BUILD_OPTIMIZATION

void SCsdkCommonBtns::Construct(const FArguments& InArgs)
{
	Orientation = InArgs._Orientation;
	BtnNum = InArgs._BtnNum;
	SlotWidgets = InArgs._SlotWidgets;
	OnClicked = InArgs._OnClicked;
	Padding =InArgs._Padding;
	ChildSlot
	 [
		 CreateWidgets().ToSharedRef()
	 ];
}


TSharedPtr<SWidget> SCsdkCommonBtns::CreateWidgets()
{
	TSharedPtr<SWidget> Widget;
	switch (Orientation)
	{
	case EOrientation::Orient_Horizontal:
		{
		
			Widget =CreateHorizontalBox();
			break;	
		}
	case EOrientation::Orient_Vertical:
		{
			Widget = CreateVerticalBox();
			break;
		}
	}

	return Widget;
}

TSharedPtr<SWidget> SCsdkCommonBtns::CreateHorizontalBox()
{
	const TSharedPtr<SHorizontalBox> HorizontalBox = SNew(SHorizontalBox);
	BoxPanel = HorizontalBox;
	for( int32 i = 0; i < BtnNum; ++i )
	{
		HorizontalBox->AddSlot().Padding(Padding)
		[
			CreateBtn(i)
		];
	}
	return HorizontalBox.ToSharedRef();
}

TSharedPtr<SWidget> SCsdkCommonBtns::CreateVerticalBox()
{
	const TSharedPtr<SVerticalBox> VerticalBox = SNew(SVerticalBox);
	BoxPanel = VerticalBox;
	for( int32 i = 0; i < BtnNum; ++i )
	{
		VerticalBox->AddSlot().Padding(Padding)
		[
			CreateBtn(i)
		];
	}
	
	return VerticalBox.ToSharedRef();
}

TSharedRef<SCsdkBaseBtn> SCsdkCommonBtns::CreateBtn(int32 Index)
{


	auto Btn = SNew(SCsdkBaseBtn)
	.SlotWidget(SlotWidgets[Index])
	.OnClicked(this,&SCsdkCommonBtns::OnButtonClicked, Index);
	
	return Btn;
}


void SCsdkCommonBtns::SetPadding(const TAttribute<FMargin>& InPadding)
{
	TPanelChildren<SBoxPanel::FSlot>* Children = static_cast<TPanelChildren<SBoxPanel::FSlot>*>(BoxPanel->GetChildren());
	
	for(int32 i =0; i<Children->Num(); ++i)
	{
		(*Children)[i].SetPadding(InPadding);
	}
}

FReply SCsdkCommonBtns::OnButtonClicked(int32 Index)
{
	if(OnClicked.IsBound())
	{
		return  OnClicked.Execute(Index);
	}
	return FReply::Unhandled();
}

END_SLATE_FUNCTION_BUILD_OPTIMIZATION
