// Copyright Pacificao. All Rights Reserved.

#pragma once

#include "CoreMinimal.h"
#include "Components/ActorComponent.h"
#include "AgrarianTypes.h"
#include "AgrarianCraftingComponent.generated.h"

class UAgrarianInventoryComponent;
class UAgrarianRecipeDataAsset;

DECLARE_DYNAMIC_MULTICAST_DELEGATE_TwoParams(FAgrarianCraftCompletedSignature, FName, RecipeId, const FAgrarianItemStack&, Result);
DECLARE_DYNAMIC_MULTICAST_DELEGATE_TwoParams(FAgrarianCraftFailedSignature, FName, RecipeId, FText, Reason);

UCLASS(ClassGroup = (Agrarian), BlueprintType, Blueprintable, meta = (BlueprintSpawnableComponent))
class UAgrarianCraftingComponent : public UActorComponent
{
	GENERATED_BODY()

public:
	UAgrarianCraftingComponent();

	UPROPERTY(BlueprintAssignable, Category = "Agrarian|Crafting")
	FAgrarianCraftCompletedSignature OnCraftCompleted;

	UPROPERTY(BlueprintAssignable, Category = "Agrarian|Crafting")
	FAgrarianCraftFailedSignature OnCraftFailed;

	UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Agrarian|Crafting")
	TArray<FAgrarianRecipe> KnownRecipes;

	UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Agrarian|Crafting")
	TArray<TObjectPtr<UAgrarianRecipeDataAsset>> KnownRecipeAssets;

	UFUNCTION(BlueprintCallable, Category = "Agrarian|Crafting")
	bool CanCraft(FName RecipeId, FText& FailureReason) const;

	UFUNCTION(BlueprintCallable, Category = "Agrarian|Crafting")
	bool Craft(FName RecipeId);

	UFUNCTION(Server, Reliable, BlueprintCallable, Category = "Agrarian|Crafting")
	void ServerCraft(FName RecipeId);

	UFUNCTION(BlueprintCallable, Category = "Agrarian|Crafting")
	bool AddKnownRecipe(const FAgrarianRecipe& Recipe);

	UFUNCTION(BlueprintCallable, Category = "Agrarian|Crafting")
	bool FindRecipe(FName RecipeId, FAgrarianRecipe& OutRecipe) const;

	UFUNCTION(BlueprintCallable, Category = "Agrarian|Crafting")
	void GetKnownRecipes(TArray<FAgrarianRecipe>& OutRecipes) const;

protected:
	UAgrarianInventoryComponent* GetInventory() const;
	void FailCraft(FName RecipeId, const FText& Reason);
};
