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


#include "CraftingRecipeSlotWidget.h"

#include "Components/Image.h"
#include "Components/TextBlock.h"
#include "CraftingRecipeDataAsset.h"
#include "CraftingWidget.h"

void UCraftingRecipeSlotWidget::InitSlot(UCraftingRecipeDataAsset* InRecipe, UCraftingWidget* InOwningGrid)
{
	Recipe = InRecipe;
	OwningGrid = InOwningGrid;

	if (RecipeIcon)
	{
		UTexture2D* Icon = Recipe ? Recipe->GetIcon() : nullptr;
		if (Icon)
		{
			RecipeIcon->SetBrushFromTexture(Icon);
			RecipeIcon->SetVisibility(ESlateVisibility::HitTestInvisible);
		}
		else
		{
			// Pas d'icone : on masque plutot que de laisser le brush par defaut,
			// qui afficherait un carre blanc franchement laid dans la grille.
			RecipeIcon->SetVisibility(ESlateVisibility::Hidden);
		}
	}

	if (RecipeNameText)
	{
		RecipeNameText->SetText(Recipe ? Recipe->GetDisplayName() : FText::GetEmpty());
	}

	SetSelected(false);
	SetCraftable(true);
}

void UCraftingRecipeSlotWidget::SetSelected(bool bSelected)
{
	if (SelectionBorder)
	{
		SelectionBorder->SetVisibility(bSelected ? ESlateVisibility::HitTestInvisible : ESlateVisibility::Hidden);
	}
}

void UCraftingRecipeSlotWidget::SetCraftable(bool bCraftable)
{
	if (RecipeIcon)
	{
		RecipeIcon->SetColorAndOpacity(bCraftable ? CraftableTint : UncraftableTint);
	}
}

FReply UCraftingRecipeSlotWidget::NativeOnMouseButtonDown(const FGeometry& InGeometry, const FPointerEvent& InMouseEvent)
{
	if (InMouseEvent.GetEffectingButton() == EKeys::LeftMouseButton && OwningGrid.IsValid())
	{
		OwningGrid->NotifyRecipeClicked(Recipe);
		return FReply::Handled();
	}

	return Super::NativeOnMouseButtonDown(InGeometry, InMouseEvent);
}
