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

#pragma once

#include "CoreMinimal.h"
#include "Components/ActorComponent.h"
#include "InventoryStructs.h"
#include "InventoryComponent.generated.h"


UCLASS(ClassGroup=(Custom), meta=(BlueprintSpawnableComponent))
class ENDLESSVENDETTA_API UInventoryComponent : public UActorComponent
{
	GENERATED_BODY()

public:
	// Sets default values for this component's properties
	UInventoryComponent();
	virtual void InitializeComponent() override;

	UPROPERTY(EditDefaultsOnly, BlueprintReadOnly, Category="Inventory")
	int Columns = 10;
	UPROPERTY(EditDefaultsOnly, BlueprintReadOnly, Category="Inventory")
	int Rows = 10;
	UPROPERTY(EditDefaultsOnly, BlueprintReadOnly, Category="Inventory")
	float MaxPickupDistance = 200.0f;

	
protected:
	// Called when the game starts
	virtual void BeginPlay() override;

public:
	// Called every frame
	virtual void TickComponent(float DeltaTime, ELevelTick TickType, FActorComponentTickFunction* ThisTickFunction) override;

	DECLARE_DYNAMIC_MULTICAST_DELEGATE(FOnInventoryUpdated);
	UPROPERTY(BlueprintAssignable, Category="Inventory")
	FOnInventoryUpdated OnInventoryUpdated;
	DECLARE_DYNAMIC_MULTICAST_DELEGATE(FOnInventorySizeUpdated);
	UPROPERTY(BlueprintAssignable, Category = "Inventory")
	FOnInventorySizeUpdated OnInventorySizeUpdated;
	
	UFUNCTION(BlueprintCallable, Category="Inventory")
	bool AddItem(class UBaseItem* Item);
	UFUNCTION(BlueprintCallable, Category="Inventory")
	void ProjectTraceForItem();
	UFUNCTION(BlueprintCallable, Category="Inventory")
	void Pickup();
	UFUNCTION(BlueprintCallable, Category="Inventory")
	bool IsRoomAvailable(UBaseItem* Item, const int TopLeftIndex);
	UFUNCTION(BlueprintCallable, Category="Inventory")
	FInventoryTile IndexToTile(const int Index) const;
	TTuple<UBaseItem*, bool> GetItemAtIndex(const int Index);
	UFUNCTION(BlueprintCallable, Category="Inventory")
	int TileToIndex(const FInventoryTile InventoryTile) const;
	UFUNCTION(BlueprintCallable, Category="Inventory")
	void AddItemAt(UBaseItem* Item, const int TopLeftIndex);
	UFUNCTION(BlueprintCallable, Category="Inventory")
	TMap<UBaseItem*, FInventoryTile> GetAllItems();
	UFUNCTION(BlueprintCallable, Category="Inventory")
	void RemoveItem(UBaseItem* Item);
	UFUNCTION(BlueprintCallable, Category="Inventory")
	void SpawnItem(UBaseItem* Item, FVector Location);
	UFUNCTION(BlueprintCallable, Category = "Inventory")
	void UpdateInventorySize(const int _Columns, const int _Rows);

	void SetPrimaryWeapon(AActor* const _PrimaryWeapon);
	AActor* GetPrimaryWeapon() const;
	void SetSecondaryWeapon(AActor* const _SecondaryWeapon);
	AActor* GetSecondaryWeapon() const;

private:
	bool IsTileValid(const FInventoryTile InventoryTile) const;
	
	UPROPERTY()
	AActor* PlayerCharacter;
	UPROPERTY()
	AActor* ItemToPickup;
	bool IsDirty = false;
	UPROPERTY()
	TArray<UBaseItem*> InventoryItems;
	AActor* PrimaryWeapon;
	AActor* SecondaryWeapon;
};
