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


#include "GadgetTutorialStation.h"

#include "Blueprint/UserWidget.h"

// Sets default values
AGadgetTutorialStation::AGadgetTutorialStation()
{
 	// Set this actor to call Tick() every frame.  You can turn this off to improve performance if you don't need it.
	PrimaryActorTick.bCanEverTick = true;

}

// Called when the game starts or when spawned
void AGadgetTutorialStation::BeginPlay()
{
	Super::BeginPlay();

	EndlessVendettaCharacter = Cast<AEndlessVendettaCharacter>(GetWorld()->GetFirstPlayerController()->GetPawn());
}

// Called every frame
void AGadgetTutorialStation::Tick(float DeltaTime)
{
	Super::Tick(DeltaTime);

}

void AGadgetTutorialStation::Interact()
{
	if (GadgetsArray.IsEmpty()) return;

	// Setup Widget so it can be seen and interacted with
	FInputModeUIOnly InputMode;
	APlayerController* PlayerController = GetWorld()->GetFirstPlayerController();
	PickGadgetWidget = CreateWidget<UUserWidget>(GetWorld(), PickGadgetWidgetClass);
	PickGadgetWidget->AddToViewport(3);
	PlayerController->SetInputMode(InputMode);
	PlayerController->bShowMouseCursor = true;
	PlayerController->bEnableClickEvents = true;
	PlayerController->bEnableMouseOverEvents = true;

	// Set up gadget menu's behaviours
	GadgetMenu = Cast<UGadgetMenu>(PickGadgetWidget);
	GadgetMenu->CloseMenuDelegate.AddDynamic(this, &AGadgetTutorialStation::CloseWidget);
	GadgetMenu->NextGadgetDelegate.AddDynamic(this, &AGadgetTutorialStation::NextGadget);
	GadgetMenu->PreviousGadgetDelegate.AddDynamic(this, &AGadgetTutorialStation::PreviousGadget);
	GadgetMenu->SelectGadgetDelegate.AddDynamic(this, &AGadgetTutorialStation::SelectGadget);

	// Runs Update Display Gadget at the end which overrides the default stand in gadget info
	NextGadget();
}

void AGadgetTutorialStation::InteractPrompt()
{
	
}

void AGadgetTutorialStation::NextGadget()
{
	FGadgetInfo GadgetInfo;
	// Either wrap around to the beginning or increment gadget index
	GadgetIndex = GadgetIndex >= GadgetsArray.Num() - 1 ? 0 : GadgetIndex + 1;
	
	GadgetInfo.GadgetIcon = GadgetsArray[GadgetIndex]->GetDefaultObject<AGadgetBase>()->GetGadgetIcon();
	GadgetInfo.GadgetDesc = GadgetsArray[GadgetIndex]->GetDefaultObject<AGadgetBase>()->GetGadgetDesc();
	GadgetInfo.GadgetName = GadgetsArray[GadgetIndex]->GetDefaultObject<AGadgetBase>()->GetGadgetName();

	GadgetMenu->UpdateDisplayedGadget(GadgetInfo);
}

void AGadgetTutorialStation::PreviousGadget()
{
	FGadgetInfo GadgetInfo;
	// Either wrap around to the end or decrement gadget index
	GadgetIndex = GadgetIndex <= 0 ? GadgetsArray.Num() - 1 : GadgetIndex - 1;
	
	GadgetInfo.GadgetIcon = GadgetsArray[GadgetIndex]->GetDefaultObject<AGadgetBase>()->GetGadgetIcon();
	GadgetInfo.GadgetDesc = GadgetsArray[GadgetIndex]->GetDefaultObject<AGadgetBase>()->GetGadgetDesc();
	GadgetInfo.GadgetName = GadgetsArray[GadgetIndex]->GetDefaultObject<AGadgetBase>()->GetGadgetName();

	GadgetMenu->UpdateDisplayedGadget(GadgetInfo);
}

void AGadgetTutorialStation::SelectGadget()
{
	// Eventually expand to give player feedback of being denied if changing gadgets fails
	if (!EndlessVendettaCharacter->UpdateGadgetType(GadgetsArray[GadgetIndex])) return;
	
	if (IsValid(CurrentGadgetTutorial)) CurrentGadgetTutorial->DestroyTutorial();

	FActorSpawnParameters SpawnParams;
	SpawnParams.SpawnCollisionHandlingOverride = ESpawnActorCollisionHandlingMethod::AlwaysSpawn;
	TSubclassOf<ABaseGadgetTutorial> GadgetTutorialToSpawnInClass = GadgetsArray[GadgetIndex]->GetDefaultObject<AGadgetBase>()->GadgetTutorialClass;
	if (!IsValid(GadgetTutorialToSpawnInClass)) return;
	AActor* GadgetTutorialActor = GetWorld()->SpawnActor<AActor>(GadgetTutorialToSpawnInClass, GadgetTutorialSpawnTransform.GetLocation(), GadgetTutorialSpawnTransform.GetRotation().Rotator(), SpawnParams);
	CurrentGadgetTutorial = Cast<ABaseGadgetTutorial>(GadgetTutorialActor);
}

void AGadgetTutorialStation::CloseWidget()
{
	// close the widget
	if (IsValid(PickGadgetWidget)) PickGadgetWidget->RemoveFromParent();

	CollectGarbage(RF_PendingKill);

	FInputModeGameOnly InputMode;
	APlayerController* PlayerController = GetWorld()->GetFirstPlayerController();
	PlayerController->SetInputMode(InputMode);
	PlayerController->bShowMouseCursor = false;
	PlayerController->bEnableClickEvents = false;
	PlayerController->bEnableMouseOverEvents = false;
}



