// Oleg Petruny proprietary.


#include "CustomGameInstanceBase.h"

#include "EnhancedInputLibrary.h"
#include "InputMappingContext.h"

#include "CustomGameUserSettings.h"
#include "Interactable/Activators/InCameraInteractableActivator.h"
#include "Interactable/Activators/RaycastInteractableActivator.h"

void UCustomGameInstanceBase::Init()
{
    // IN FUTURE ASSIGN FROM CONTENT LOADER MEMBER
    interactionsActivators.Add(URaycastInteractableActivator::StaticClass());
    interactionsActivators.Add(UInCameraInteractableActivator::StaticClass());

    UGameInstance::Init();

    ApplyMouseSettings();
}

void UCustomGameInstanceBase::ApplyMouseSettings()
{
    if(auto gameSettings = UCustomGameUserSettings::GetCustomGameUserSettings())
    {
        for(auto context_it = defaultInputContexts.begin(); context_it != defaultInputContexts.end(); ++context_it)
        {
            auto& mappings = (*context_it).LoadSynchronous()->GetMappings();
            for(auto mapping_it = mappings.begin(); mapping_it != mappings.end(); ++mapping_it)
            {
                if((*mapping_it).Key == EKeys::Mouse2D)
                {
                    for(auto modifiers_it = (*mapping_it).Modifiers.begin(); modifiers_it != (*mapping_it).Modifiers.end(); ++modifiers_it)
                    {
                        if(auto negate_modifier = Cast<UInputModifierNegate>(*modifiers_it))
                        {
                            negate_modifier->bY = !gameSettings->bMouseInverted;
                        }
                        if(auto scalar_modifier = Cast<UInputModifierScalar>(*modifiers_it))
                        {
                            scalar_modifier->Scalar = FVector{ gameSettings->GetMouseSensetivity() * 0.5 };
                        }
                    }
                }
            }
            UEnhancedInputLibrary::RequestRebuildControlMappingsUsingContext((*context_it).LoadSynchronous());
        }
    }
}