// Oleg Petruny proprietary.


#include "WidgetsManager.h"

#include "Blueprint/UserWidget.h"
#include "Engine/World.h"
#include "InputMappingContext.h"
#include "Kismet/GameplayStatics.h"
#include "UObject/ScriptInterface.h"

#include "CustomGameInstanceBase.h"
#include "Interactable/Interactable.h"
#include "Interactable/Modificators/InteractableModificator.h"
#include "InteractableHintWidgetManager.h"

void UWidgetsManager::Init()
{
    if(auto PC = UGameplayStatics::GetPlayerController(GetWorld(), 0))
    {
        for(auto& widget : permaOverlayWidgets)
        {
            if(auto instance = CreateWidget<UUserWidget>(PC, widget))
            {
                permaOverlayWidgetsInstances.Add(instance);
                instance->AddToViewport();
            }
        }
        for(auto& widget : overlayWidgets)
        {
            if(auto instance = CreateWidget<UUserWidget>(PC, widget))
            {
                overlayWidgetsInstances.Add(instance);
                instance->AddToViewport();
            }
        }
    }

    interactableHintWidgetManager = NewObject<UInteractableHintWidgetManager>(this, interactableHintWidgetManagerClass);
}

void UWidgetsManager::HideOverlayWidgets()
{
    for(auto& widget : overlayWidgetsInstances)
    {
        if(widget.IsValid())
        {
            widget->SetVisibility(ESlateVisibility::Hidden);
        }
    }
}

void UWidgetsManager::ShowOverlayWidgets()
{
    for(auto& widget : overlayWidgetsInstances)
    {
        if(widget.IsValid())
        {
            widget->SetVisibility(ESlateVisibility::Visible);
        }
    }
}

void UWidgetsManager::UpdateOverlayWidgetsOwner()
{
    if(auto PC = UGameplayStatics::GetPlayerController(GetWorld(), 0))
    {
        for(auto& widget : permaOverlayWidgetsInstances)
        {
            if(widget.IsValid())
            {
                widget->SetOwningPlayer(PC);
            }
        }
        for(auto& widget : overlayWidgetsInstances)
        {
            if(widget.IsValid())
            {
                widget->SetOwningPlayer(PC);
            }
        }
    }
}

void UWidgetsManager::ShowInteractionHints(const UInteractableModificator* modificator)
{
    interactableHintWidgetManager->Append(modificator);
}
void UWidgetsManager::HideInteractionHints(const EActivatorType type)
{
    interactableHintWidgetManager->Remove(type);
}