#include "UnrealProject.h"
#include "CharacterDependentSubMenus.h"
#include "DefaultGameInstance.h"
#include "CharacterSubMenu.h"
#include "CharacterCarousel.h"
#include "EffectSelector.h"
#include "SkillWidget.h"

void UCharacterDependentSubMenu::OnEnter(class UMenuScreenBase* screen)
{
	// Find character menu
	auto submenus = screen->GetSubMenus();
	for(int32 i = submenus.Num() - 1; i >= 0; i--)
	{
		if(submenus[i]->IsA<UCharacterSubMenu>())
		{
			characterSubMenu = Cast<UCharacterSubMenu>(submenus[i]);
			characterCarousel = characterSubMenu->GetCarousel();
			displayCharacter = characterCarousel->GetCharacterModel(characterCarousel->selectedCharacterIndex);
		}
	}

	Super::OnEnter(screen);
}

void UCharacterDependentSubMenu::OnSuspend(USubMenu* newSubMenu, bool loseFocus)
{
	USkillWidget* sw = Cast<USkillWidget>(newSubMenu);
	UEffectSelector* es = Cast<UEffectSelector>(newSubMenu);
	if(es || sw)
		USubMenu::OnSuspend(newSubMenu, loseFocus); // Prevent menu panel animations
	else
		Super::OnSuspend(newSubMenu, loseFocus);
}
void UCharacterDependentSubMenu::OnRestore(USubMenu* removedMenu)
{
	USkillWidget* sw = Cast<USkillWidget>(removedMenu);
	UEffectSelector* es = Cast<UEffectSelector>(removedMenu);
	if(es || sw)
		USubMenu::OnRestore(removedMenu); // Prevent menu panel animations
	else
		Super::OnRestore(removedMenu);
}

FCharacterSave UCharacterDependentSubMenu::GetCharacterSave() const
{
	static FCharacterSave dummy;
	if(!characterCarousel)
		return dummy;
	UDefaultGameInstance* inst = Cast<UDefaultGameInstance>(GetGameInstance());
	UCharacterSettings* settings = inst->GetCharacterSettings();
	return settings->characterSaves[characterCarousel->selectedCharacterIndex];
}
void UCharacterDependentSubMenu::SetCharacterSave(const FCharacterSave& save)
{
	if(!characterCarousel)
		return;
	UDefaultGameInstance* inst = Cast<UDefaultGameInstance>(GetGameInstance());
	UCharacterSettings* settings = inst->GetCharacterSettings();
	settings->characterSaves[characterCarousel->selectedCharacterIndex] = save;
}
