// Oleg Petruny proprietary.

#include "QuickTimeEventWidget.h"

#include "Animation/WidgetAnimation.h"

#include "QuickTimeEvent.h"
#include "QuickTimeEventWidgetManager.h"

void UQuickTimeEventWidget::NativeConstruct()
{
    FWidgetAnimationDynamicEvent onTimerFinish;
    onTimerFinish.BindDynamic(this, &UQuickTimeEventWidget::RemoveEvent);
    BindToAnimationFinished(eventAnimation, onTimerFinish);

    FWidgetAnimationDynamicEvent onRemove;
    onRemove.BindDynamic(this, &UQuickTimeEventWidget::RemoveFromManager);
    BindToAnimationFinished(removeAnimation, onRemove);

    return Super::NativeConstruct();
}

void UQuickTimeEventWidget::Show()
{
    SetVisibility(ESlateVisibility::Visible);
    const float playbackSpeed = eventAnimation->GetEndTime() / duration;

    PlayAnimation(eventAnimation, 0, 1, EUMGSequencePlayMode::Forward, playbackSpeed, false);
}

void UQuickTimeEventWidget::RemoveFromManager()
{
    manager->RemoveEvent(id);
}

EQuickTimeEventResult UQuickTimeEventWidget::_Process(const bool released)
{
    Process(released);

    return result;
}

EQuickTimeEventResult UQuickTimeEventWidget::_ProcessBeforeRemoving()
{
    ProcessBeforeRemoving();

    return result;
}

void UQuickTimeEventWidget::RemoveEvent()
{
    if(removing)
        return;

    removing = true;
    PlayAnimation(removeAnimation, 0, 1, EUMGSequencePlayMode::Forward, 1, false);
}
