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


#include "HYPlayerCharacController.h"
#include "HYGameStateBase.h"
#include "Engine/DataTable.h"
#include "hwanyoung2Character.h"

AHYPlayerCharacController::AHYPlayerCharacController()
{
	//this is hardcoding the limits:
	InventorySlotLimit = 50; 
	InventoryWeightLimit = 500;
}

int32 AHYPlayerCharacController::GetInventoryWeight()
{
	int32 TotalWeight = 0;

	for (auto& Item : Inventory) {
		TotalWeight += 1; //hard coding for now
	}
	return TotalWeight;
}

bool AHYPlayerCharacController::AddItemToInventoryByID(FName ID)
{
	AHYGameStateBase* gameState = Cast<AHYGameStateBase>(GetWorld()->GetGameState());
	UDataTable* itemDB = gameState->GetItemDatabase();
	FInventoryItem* itemToAdd = itemDB->FindRow<FInventoryItem>(ID, "");

	if (itemToAdd) {
		if (Inventory.Num() < InventorySlotLimit &&
			GetInventoryWeight() + itemToAdd->ItemWeight <= InventoryWeightLimit) {
			Inventory.Add(*itemToAdd);
			ReloadInventory();
			return true;
		}
	}
	return false;
}

FInventoryItem AHYPlayerCharacController::CraftItem(FInventoryItem ItemA)
{
	/*for (auto Combination : ItemB.CraftCombinations) {
		if (Combination.ItemAID == ItemA.ItemID 
			&& Combination.ItemBID == ItemB.ItemID
			&& Combination.ItemCID == ItemC.ItemID 
			&& Combination.ItemDID == ItemD.ItemID) {
			if (Combination.bDestroyItemA) Inventory.RemoveSingle(ItemA);
			if (Combination.bDestroyItemB) Inventory.RemoveSingle(ItemB);
			if (Combination.bDestroyItemC) Inventory.RemoveSingle(ItemC);
			if (Combination.bDestroyItemD) Inventory.RemoveSingle(ItemD);
			AddItemToInventoryByID(Combination.ProductID);
			ReloadCraftUI();
		}
	}*/

	int32 NumOfCraftingItems = CraftingItems.Num();
	bool bIsValidComb;
	bIsValidComb = false;
	if (NumOfCraftingItems < 2 || NumOfCraftingItems > 4) { return FInventoryItem(); }

	AHYGameStateBase* gameState = Cast<AHYGameStateBase>(GetWorld()->GetGameState());
	UDataTable* itemDB = gameState->GetItemDatabase();
	
	for (auto Combination : ItemA.CraftCombinations) {
		if (CraftingItems.Contains(Combination.ItemBID) && NumOfCraftingItems >= 2) {
			if (Combination.bDestroyItemB) Inventory.RemoveSingle(*itemDB->FindRow<FInventoryItem>(Combination.ItemBID, ""));
			bIsValidComb = bIsValidComb || true;
		}
		if (CraftingItems.Contains(Combination.ItemCID) && NumOfCraftingItems >= 3) {
			if (Combination.bDestroyItemC) Inventory.RemoveSingle(*itemDB->FindRow<FInventoryItem>(Combination.ItemCID, ""));
			bIsValidComb = bIsValidComb && true;
		}
		if (CraftingItems.Contains(Combination.ItemDID) && NumOfCraftingItems == 4) {
			if (Combination.bDestroyItemD) Inventory.RemoveSingle(*itemDB->FindRow<FInventoryItem>(Combination.ItemDID, ""));
			bIsValidComb = bIsValidComb && true;
		}
		if (bIsValidComb) {
			if (Combination.bDestroyItemA) Inventory.RemoveSingle(ItemA);
			AddItemToInventoryByID(Combination.ProductID);
			ReloadCraftUI();
			return *itemDB->FindRow<FInventoryItem>(Combination.ProductID, "");
		}
		else continue;
	}
	return FInventoryItem();
}

void AHYPlayerCharacController::Interact()
{
	if (CurrentInteractable) CurrentInteractable->Interact(this);
}

void AHYPlayerCharacController::SetupInputComponent()
{
	Super::SetupInputComponent();
}

void AHYPlayerCharacController::OnPossess(APawn* InPawn)
{
	Super::OnPossess(InPawn);
}
