// Fill out your copyright notice in the Description page of Project Settings.

#pragma once

#include "CoreMinimal.h"
#include "EnhancedInputSubsystemInterface.h"
#include "GadgetTutorial/BaseGadgetTutorial.h"
#include "GameFramework/Actor.h"
#include "GadgetBase.generated.h"

UCLASS()
class ENDLESSVENDETTA_API AGadgetBase : public AActor
{
	GENERATED_BODY()

	APlayerController* PlayerController;

	FTimerHandle CooldownHandle;
	void StartGadgetCooldown();
	bool GadgetOnCooldown = false;

	FTimerHandle InUseHandle;
	void StartInUseTimer();
	bool GadgetInUse = false;

	void ReadyGadget()
	{
		UE_LOG(LogTemp, Warning, TEXT("Gadget is now ready to be used again"));
		GadgetOnCooldown = false;
	}

protected:
	UPROPERTY(EditDefaultsOnly, Category = "Gadget")
	FVector EquippedOffset = FVector(0, 0, 0);

	UPROPERTY(EditDefaultsOnly, Category = "Gadget")
	FRotator GadgetRotation = FRotator(0, 0, 0);
	
	UPROPERTY(EditDefaultsOnly, CAtegory = "Gadget")
	FVector UnequippedOffset = FVector(0, 0, -1000);

	UPROPERTY(EditDefaultsOnly, cATEGORY = "Gadget")
	float GadgetMaxUptime;

	UPROPERTY(EditDefaultsOnly, Category = "Gadget")
	float CooldownTime;

	UPROPERTY(EditDefaultsOnly, CAtegory = "Gadget")
	bool UnequippableWhenInUse = false;

	UPROPERTY(EditDefaultsOnly, Category = "Gadget")
	UInputMappingContext* GadgetMappingContext;
	
	UPROPERTY(EditDefaultsOnly, Category = "Gadget")
	UInputAction* ActivateAction;

	UPROPERTY(EditDefaultsOnly, Category = "Gadget")
	UTexture2D* GadgetIcon;

	UPROPERTY(EditDefaultsOnly, Category = "Gadget")
	FString GadgetDesc;

	UPROPERTY(EditDefaultsOnly, Category = "Gadget")
	FString GadgetName;
	

	// Used by child classes to run custom gadget behaviour
	virtual void Activate();

	// Starts Cooldown and automatically unequips gadget, used by child classes to implement custom behaviour
	virtual void FinishedUsing();
	
	bool GadgetCantBeUsed()
	{
		return GadgetOnCooldown|| GadgetInUse;
	}
	
	// Called when the game starts or when spawned
	virtual void BeginPlay() override;

public:
	bool Equipped = false;

	void Equip();
	
	// Return true if unequip succeeds
	bool Unequip();

	FVector GetUnequippedOffset()
	{
		return UnequippedOffset;
	}

	FRotator GetGadgetSpawnRotation()
	{
		return GadgetRotation;
	}

	UTexture2D* GetGadgetIcon()
	{
		return IsValid(GadgetIcon) ? GadgetIcon : nullptr;
	}

	FString GetGadgetDesc()
	{
		return GadgetDesc;	
	}

	FString GetGadgetName()
	{
		return GadgetName;
	}

	bool IsUnequippableInUse()
	{
		return UnequippableWhenInUse;
	}

	bool IsInUse()
	{
		return GadgetInUse;
	}

	UPROPERTY(EditDefaultsOnly, Category = "Gadget")
	TSubclassOf<ABaseGadgetTutorial> GadgetTutorialClass;
	
	// Sets default values for this actor's properties
	AGadgetBase();

	// Called every frame
	virtual void Tick(float DeltaTime) override;

};
