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

#pragma once

#include "CoreMinimal.h"
#include "StructUtils/InstancedStruct.h"
#include "UObject/Object.h"
#include "Inventory.generated.h"

class UItemFilter;
struct FItemFilter;
class UItem;

USTRUCT(BlueprintType)
struct FItemStack
{
	GENERATED_BODY()
	
	UPROPERTY(EditAnywhere, BlueprintReadWrite)
	int32 Stack = 1;
	
	UPROPERTY(EditAnywhere, BlueprintReadWrite)
	UItem* ItemPtr = nullptr;
};

DECLARE_DYNAMIC_MULTICAST_DELEGATE_TwoParams(FOnGoldChanged, int32, ChangeAmount, int32, CurrentGold);

/**
 * 
 */
UCLASS(BlueprintType)
class FAYE_API UInventory : public UObject
{
	GENERATED_BODY()
	
public:
	
	UFUNCTION(BlueprintCallable, Category = "Inventory")
	void AddItem(UItem* InItem, int32 Stack = 1);
	
	// -1 to remove all
	UFUNCTION(BlueprintCallable, Category = "Inventory")
	bool RemoveItem(const UItem* InItem, int32 Stacks = -1);
	
	UFUNCTION(BlueprintCallable, Category = "Inventory")
	bool GetItemStack(const UItem* Item, FItemStack& OutItemStack) const;
	
	UFUNCTION(BlueprintCallable, Category = "Inventory")
	TArray<UItem*> FilterItems(UItemFilter* Filter) const;
	
	UFUNCTION(BlueprintCallable, Category = "Inventory")
	int32 CountStacksForItems(const TArray<UItem*>& Items) const;
	
	
	UFUNCTION(BlueprintCallable, Category = "Inventory|Gold")
	bool ModifyGold(int32 Amount);
	UFUNCTION(BlueprintPure, Category = "Inventory|Gold")
	int32 GetGold() const { return Gold; }
	
	
	UPROPERTY(BlueprintAssignable)
	FOnGoldChanged OnGoldChanged;
	
protected:
	
	FItemStack* GetItemStack_Internal(const UItem* Item);
	
	UPROPERTY()
	TArray<FItemStack> ItemStacks;
	
	UPROPERTY()
	int32 Gold = 0;
	
};
