// Copyright Pacificao. All Rights Reserved.

#pragma once

#include "CoreMinimal.h"
#include "Components/ActorComponent.h"
#include "AgrarianTypes.h"
#include "AgrarianBuildingPlacementComponent.generated.h"

DECLARE_DYNAMIC_MULTICAST_DELEGATE_OneParam(FAgrarianBuildPlacedSignature, AActor*, PlacedActor);
DECLARE_DYNAMIC_MULTICAST_DELEGATE_OneParam(FAgrarianBuildPlacementFailedSignature, FText, Reason);

UCLASS(ClassGroup = (Agrarian), BlueprintType, Blueprintable, meta = (BlueprintSpawnableComponent))
class UAgrarianBuildingPlacementComponent : public UActorComponent
{
	GENERATED_BODY()

public:
	UAgrarianBuildingPlacementComponent();

	UPROPERTY(BlueprintAssignable, Category = "Agrarian|Building")
	FAgrarianBuildPlacedSignature OnBuildPlaced;

	UPROPERTY(BlueprintAssignable, Category = "Agrarian|Building")
	FAgrarianBuildPlacementFailedSignature OnBuildPlacementFailed;

	UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Agrarian|Building")
	TSubclassOf<AActor> ActiveBuildClass;

	UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Agrarian|Building")
	TArray<FAgrarianItemStack> PlacementCost;

	UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Agrarian|Building", meta = (ClampMin = "100"))
	float PlacementDistance = 600.0f;

	UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Agrarian|Building", meta = (ClampMin = "0"))
	float PlacementProbeRadius = 75.0f;

	UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Agrarian|Building", meta = (ClampMin = "0"))
	float SurfaceOffset = 2.0f;

	UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Agrarian|Building")
	bool bSnapToGrid = true;

	UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Agrarian|Building", meta = (EditCondition = "bSnapToGrid", ClampMin = "1"))
	float GridSize = 50.0f;

	UFUNCTION(BlueprintCallable, Category = "Agrarian|Building")
	void SetActiveBuildable(TSubclassOf<AActor> BuildClass, const TArray<FAgrarianItemStack>& Cost);

	UFUNCTION(BlueprintCallable, Category = "Agrarian|Building")
	bool GetPlacementPreview(FTransform& OutTransform, FText& FailureReason) const;

	UFUNCTION(BlueprintCallable, Category = "Agrarian|Building")
	bool CanPlaceAtTransform(TSubclassOf<AActor> BuildClass, const FTransform& PlacementTransform, FText& FailureReason) const;

	UFUNCTION(BlueprintCallable, Category = "Agrarian|Building")
	bool PlaceActiveBuildable();

	UFUNCTION(Server, Reliable, BlueprintCallable, Category = "Agrarian|Building")
	void ServerPlaceBuildable(TSubclassOf<AActor> BuildClass, FTransform PlacementTransform);

protected:
	bool HasPlacementCost(FText& FailureReason) const;
	void ConsumePlacementCost();
	void FailPlacement(const FText& Reason);
	FVector SnapLocation(const FVector& Location) const;
};
