// Oleg Petruny proprietary.


#include "CameraModeBase.h"

#include "Camera/CameraComponent.h"
#include "EnhancedInputSubsystems.h"
#include "GameFramework/Character.h"
#include "GameFramework/PawnMovementComponent.h"
#include "InputMappingContext.h"
#include "Kismet/GameplayStatics.h"

#include "CustomGameInstanceBase.h"
#include "CustomGameUserSettings.h"
#include "MainGameModeBase.h"

ACameraModeBase::ACameraModeBase()
{
    PrimaryActorTick.bCanEverTick = true;
}

void ACameraModeBase::BeginPlay()
{
    Super::BeginPlay();

    auto world = GetWorld();

    //GetMovementComponent()->speed
    //    GetCharacterMovement()->MaxWalkSpeed = moveSpeed;

    auto gameSettings = UCustomGameUserSettings::GetCustomGameUserSettings();

    if(auto camera = FindComponentByClass<UCameraComponent>())
    {
        camera->PostProcessSettings.MotionBlurAmount = gameSettings->bUseMotionBlur ? 1.0f : 0.0f;
    }

    if(auto PC = Cast<APlayerController>(GetController()))
    {
        if(auto inputSubsystem = ULocalPlayer::GetSubsystem<UEnhancedInputLocalPlayerSubsystem>(PC->GetLocalPlayer()))
        {
            if(auto GI = Cast<UCustomGameInstanceBase>(GetWorld()->GetGameInstance()))
            {
                inputSubsystem->ClearAllMappings();
                for(auto& inputContext : GI->inputContexts)
                {
                    inputSubsystem->AddMappingContext(inputContext.LoadSynchronous(), 0);
                }
            }
        }
    }
}

void ACameraModeBase::Tick(float DeltaTime)
{
    Super::Tick(DeltaTime);

    // stabilize move speed by fps
    AddActorLocalOffset(ConsumeMovementInputVector() * DeltaTime);
}

void ACameraModeBase::SetupPlayerInputComponent(UInputComponent* PlayerInputComponent)
{
    Super::SetupPlayerInputComponent(PlayerInputComponent);
}

void ACameraModeBase::SwitchToPlayerPawn()
{
    if(auto gamemode_base = UGameplayStatics::GetGameMode(GetWorld()))
    {
        if(auto gamemode = Cast<AMainGameModeBase>(gamemode_base))
        {
            gamemode->SwitchCameraMode();
        }
    }
}

void ACameraModeBase::ElevatePawn(float value)
{
    AddMovementInput(FVector::UpVector, value);
}

void ACameraModeBase::SwitchRun(bool run)
{
    //if(run)
    //    GetMovement()->MaxWalkSpeed = moveSpeed * runSpeedMultiplier;
    //else
    //    GetCharacterMovement()->MaxWalkSpeed = moveSpeed;
}