// Oleg Petruny proprietary.

#include "FishingManager.h"

#include "EnhancedInputComponent.h"
#include "InputMappingContext.h"

#include "CustomPlayerController.h"
#include "MainGameModeBase.h"
#include "PlayerBase.h"

AFishingManager::AFishingManager()
{
    static ConstructorHelpers::FObjectFinder<UInputMappingContext> asset{ TEXT("/Script/EnhancedInput.InputMappingContext'/Game/Input/Minigame/IMC_Minigame_Fishing.IMC_Minigame_Fishing'") };
    context = asset.Object;
}

void AFishingManager::Start(APlayerBase* playerPawn, FMinigameEndCallback delegate)
{
    if(!playerPawn)
        return;

    AMinigame::Start(playerPawn, delegate);

    player->LockPlayer(FPlayerLock::All());

    auto input = ACustomPlayerController::GetInput();
    auto& mapping = context.LoadSynchronous()->GetMapping(0);
    handler1 = input->BindAction(mapping.Action, ETriggerEvent::Started, this, &AFishingManager::OnInputHold).GetHandle();
    handler2 = input->BindAction(mapping.Action, ETriggerEvent::Completed, this, &AFishingManager::OnInputUnhold).GetHandle();
}

void AFishingManager::End()
{
    auto input = ACustomPlayerController::GetInput();
    input->RemoveBindingByHandle(handler1);
    input->RemoveBindingByHandle(handler2);

    player->UnlockPlayer(FPlayerLock::All());

    AMinigame::End();
}
