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

#pragma once

#include "CoreMinimal.h"
#include "Subsystems/GameInstanceSubsystem.h"
#include "Faye/Inventory/BookItem.h"
#include "ItemDatabase.generated.h"

class UItemFilter;

USTRUCT()
struct FBookCategoryMap
{
	GENERATED_BODY()
	
	UPROPERTY()
	TArray<UBookItem*> Books;
};

/**
 * 
 */
UCLASS()
class FAYE_API UItemDatabase : public UGameInstanceSubsystem
{
	GENERATED_BODY()
public:
	virtual void Initialize(FSubsystemCollectionBase& Collection) override;
	
	static UItemDatabase* Get(const UObject* WorldContext);
	
public:
	
	UFUNCTION(BlueprintPure, meta=(WorldContext="WorldContext"))
	static UBookItem* GetBook(const UObject* WorldContext, int32 Id);
	
	UFUNCTION(BlueprintPure, meta=(WorldContext="WorldContext"))
	static TArray<UBookItem*> GetBooksFromCategory(const UObject* WorldContext, EBookCategory Category);
	
	static TArray<FText> GetAuthorList(const UObject* WorldContext);
	
	static TArray<UItem*> FilterItems(const UObject* WorldContext, const UItemFilter* Filter);
	static void FilterItemArray(const UItemFilter* Filter, TArray<UItem*>& ItemList);
	
	static const TArray<UItem*>& GetAllBooks(const UObject* WorldContext);
	
	int32 GetEarliestPublicationDate() const { return EarliestPublicationDate; }
	int32 GetLatestPublicationDate() const { return LatestPublicationDate; }
protected:
	
	UPROPERTY()
	TMap<int32, UBookItem*> LoadedBooksMap;
	
	UPROPERTY()
	TArray<UItem*> AllBooks;
	
	UPROPERTY()
	TMap<EBookCategory, FBookCategoryMap> BooksByCategoryMap;
	
	UPROPERTY()
	TArray<FText> Authors;
	
	UPROPERTY()
	int32 EarliestPublicationDate = 0;
	UPROPERTY()
	int32 LatestPublicationDate = 0;
};
