// Oleg Petruny proprietary.

#pragma once

#include "GameFramework/Actor.h"

#include "AgeOfWarUnit.generated.h"

USTRUCT(BlueprintType)
struct FAgeOfWarUnitStats
{
    GENERATED_BODY()

    UPROPERTY(EditAnywhere, BlueprintReadWrite)
    FText name = FText::GetEmpty();

    UPROPERTY(EditAnywhere, BlueprintReadWrite)
    int32 price = 100;

    UPROPERTY(EditAnywhere, BlueprintReadWrite)
    int32 health = 5;

    UPROPERTY(EditAnywhere, BlueprintReadWrite)
    float moveSpeed = 1.0f;

    UPROPERTY(EditAnywhere, BlueprintReadWrite)
    int32 attackDamage = 1;

    UPROPERTY(EditAnywhere, BlueprintReadWrite)
    int32 attackUnitDistance = 1;

    UPROPERTY(EditAnywhere, BlueprintReadWrite)
    float attackStartRange = 100;

    UPROPERTY(EditAnywhere, BlueprintReadWrite)
    float attackRate = 1.0f;

    UPROPERTY(EditAnywhere, BlueprintReadWrite)
    int32 unitSize = 1;

};

UENUM(BlueprintType)
enum class UnitTeam : uint8
{
    Player,
    AI
};

UCLASS(Blueprintable, MinimalAPI, Abstract)
class AAgeOfWarUnit : public AActor
{
    GENERATED_BODY()

public:
    AAgeOfWarUnit();
    virtual void Tick(float deltaTime) override;

    UFUNCTION(BlueprintPure)
    const FAgeOfWarUnitStats& GetStats();

    UFUNCTION(BlueprintCallable)
    void UpdateStats();

    const FAgeOfWarUnitStats* stats = nullptr;
    class AAgeOfWarManager* manager = nullptr;

    UPROPERTY(EditAnywhere, BlueprintReadWrite)
    UnitTeam team;

protected:
    virtual void BeginPlay() override;
    virtual void EndPlay(const EEndPlayReason::Type EndPlayReason) override;

    UFUNCTION(BlueprintImplementableEvent)
    void OnUpdateStats();

    void Damage(const int32 damage);

    AAgeOfWarUnit* forwardUnit = nullptr;

    UPROPERTY(EditAnywhere, BlueprintReadWrite)
    class UBoxComponent* root;
    UPROPERTY(EditAnywhere, BlueprintReadWrite)
    class UBoxComponent* allyblocker;

    UPROPERTY(EditAnywhere)
    class USceneComponent* traceStart;

private:
    int32 health;
    float attackStartRange;
    FTimerHandle attackTimer;
    double lastAttackTimestamp = 0;

    UWorld* world;
    bool killed = false;
};

