// Oleg Petruny proprietary.

#pragma once

#include "Engine/LevelScriptActor.h"

#include "LevelBase.generated.h"

/**
 * Expands basic UE level script actor.
 * Has int type level states and events.
 * Brodcasts level instantiation by CustomGameInstance::OnLevelBeginned(FName).
 * On BeginPlay applies last save data if valid,
 * and instantiate all sequencers from array onBeginPlaySequences.
 */
UCLASS(BlueprintType)
class ALevelBase : public ALevelScriptActor
{
    GENERATED_BODY()

public:
    /** Returns lead level script */
    UFUNCTION(BlueprintPure, meta = (DisplayName = "Get Lead Level Script"))
    static ALevelBase* Get();

    /** Iterates throught level states */
    UFUNCTION(BlueprintCallable)
    inline void CallNextState() { ++state; NextState(); }

    /** Calls specific event id in level */
    UFUNCTION(BlueprintImplementableEvent)
    void CallEvent(int32 id);

    inline int32 GetState() { return state; };

protected:
    virtual void BeginPlay() override;

    /** Notifies level to process current state */
    UFUNCTION(BlueprintImplementableEvent)
    void NextState();

    /** Shortcut for iterating from current to expected state */
    UFUNCTION(BlueprintCallable)
    void IterateToState(int32 to);

    void BroadcastNewLevelBeginPlay();
    void StartLevelAnimations();
    void ApplySaveData();

    /** List of level animations for instantiation on level BeginPlay */
    UPROPERTY(EditDefaultsOnly)
    TArray<TSoftObjectPtr<class ULevelSequence>> onBeginPlaySequences;
    /** Cache of actors that hold instantiated animations on BeginPlay */
    TArray<class ALevelSequenceActor*> onBeginPlaySequencesActors;

    /** Current state of level */
    UPROPERTY(BlueprintReadOnly)
    int32 state = -1;
};
