// Oleg Petruny proprietary.


#include "Minigame.h"

#include "EnhancedInputSubsystems.h"
#include "InputMappingContext.h"

#include "PlayerBase.h"

AMinigame::AMinigame()
{
    PrimaryActorTick.bStartWithTickEnabled = false;
    PrimaryActorTick.bCanEverTick = true;
}

void AMinigame::End()
{
    if(callback.IsBound())
        callback.Execute(result, score);

    OnEnd();
}

void AMinigame::Restart()
{}

UInputMappingContext* AMinigame::GetInputMappings()
{
    return input.LoadSynchronous();
}

void AMinigame::Start(APlayerBase* playerPawn, FMinigameEndCallback delegate)
{
    player = playerPawn;
    callback = delegate;

    if(input)
        if(auto PC = Cast<APlayerController>(playerPawn->GetController()))
            if(auto inputSubsystem = ULocalPlayer::GetSubsystem<UEnhancedInputLocalPlayerSubsystem>(PC->GetLocalPlayer()))
                inputSubsystem->AddMappingContext(input.LoadSynchronous(), 0);

    OnStart();
}
