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

#include "AC_PlayerInventory.h"
#include "Engine/DataTable.h"
#include "AC_PlayerStats.h"


// Sets default values for this component's properties
UAC_PlayerInventory::UAC_PlayerInventory()
{
	// Set this component to be initialized when the game starts, and to be ticked every frame.  You can turn these features
	// off to improve performance if you don't need them.
	PrimaryComponentTick.bCanEverTick = true;

	InventoryCapacityPerCategory.Add(EItemType::Weapon, 0);
	InventoryCapacityPerCategory.Add(EItemType::Armor, 0);
	InventoryCapacityPerCategory.Add(EItemType::Goods, 0);
	InventoryCapacityPerCategory.Add(EItemType::None, 0);


	// ...
}


// Called when the game starts
void UAC_PlayerInventory::BeginPlay()
{
	Super::BeginPlay();

	InitializePlayerInventory();

	

	// ...
	
}


// Called every frame
void UAC_PlayerInventory::TickComponent(float DeltaTime, ELevelTick TickType, FActorComponentTickFunction* ThisTickFunction)
{
	Super::TickComponent(DeltaTime, TickType, ThisTickFunction);

	// ...
}

void UAC_PlayerInventory::InitializePlayerInventory()
{

	for (const FStartingItems& Item : StartingItems)
	{
		FString ContextString = TEXT("Initialize Player Inventory");
		InventoryCurrentCapacity = PlayerInventory.Num();

		// For each of the quantities defined
		for (int32 i = 0; i < Item.QuantityOfItem; i++)
		{
			// Is the data table valid?
			if (Item.ItemDataTableAndRow.DataTable)
			{
				FBaseItemInfo* LocItemInfo = Item.ItemDataTableAndRow.DataTable->FindRow<FBaseItemInfo>(Item.ItemDataTableAndRow.RowName, ContextString);

				//If the rows struct is valid
				if (LocItemInfo)
				{
					// Add item to player inventory array
					PlayerInventory.Add(*LocItemInfo);
					//Update Carry Capacity
					
				}
			}
		}

	}
	InventoryCurrentCapacity = PlayerInventory.Num();
}

TArray<FBaseItemInfo> UAC_PlayerInventory::GetPlayerInventory()
{
	if (!PlayerInventory.IsEmpty())
	{
		return PlayerInventory;
	}
	else {
		UE_LOG(LogTemp, Warning, TEXT("Player Inventory is empty!"));
		return PlayerInventory;
	}
}

void UAC_PlayerInventory::RequestAddItem(FBaseItemInfo InItemInfo, int32 InItemQuantity, AActor* ActorToDelete)
{
	if (bUseRequiredLevel)
	{
		//Using required level so perform checks
		UAC_PlayerStats* PlayerStatsComponent = GetOwner()->GetWorld()->GetFirstPlayerController()->GetPawn()->FindComponentByClass<UAC_PlayerStats>();
		if (InItemInfo.RequiredLevel <= GetOwner()->GetWorld()->GetFirstPlayerController()->GetPawn()->FindComponentByClass<UAC_PlayerStats>()->PlayerLevel)
		{

			for (int32 i = 0; i < InItemQuantity; i++)
			{
				PlayerInventory.Add(InItemInfo);
				InventoryCurrentCapacity++;
				ActorToDelete->Destroy();
			}
		}
		else {
			UE_LOG(LogTemp, Warning, TEXT("Item Required Level is higher than Players current level"))
		}
	}
	else {
		
		//Not using required level so just add item

		for (int32 i = 0; i < InItemQuantity; i++)
		{
			PlayerInventory.Add(InItemInfo);
			InventoryCurrentCapacity++;
			ActorToDelete->Destroy();
		}
	}
}

TArray<FBaseItemInfo> UAC_PlayerInventory::GetAllInventoryItemsInCategory(EItemType InItemType)
{
	TArray<FBaseItemInfo> LocItemsInCategory;

	for (const FBaseItemInfo& Item : PlayerInventory)
	{
		
		if (Item.ItemType == InItemType)
		{
			LocItemsInCategory.Add(Item);
		}

	}
	return LocItemsInCategory;
}

EInventoryCapType UAC_PlayerInventory::GetInventoryCapType()
{
	return this->InventoryCapType;
}

int32 UAC_PlayerInventory::CountNumberOfSameItemsInInventory(const FBaseItemInfo& ItemToCheck)
{
	int32 ItemCount = 0;

	for (const FBaseItemInfo& Item : PlayerInventory)
	{
		if (Item == ItemToCheck)
		{
			ItemCount++;
		}
	}

	return ItemCount;
}

void UAC_PlayerInventory::RequestDropItem(const FBaseItemInfo& ItemToDrop, const int32& ItemCount)
{

	for (int32 i = 0; i < ItemCount; i++)
	{
		PlayerInventory.RemoveSingle(ItemToDrop);
	}
	
		
}

