// 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 context.LoadSynchronous();
}

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

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

    OnStart();
}
