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

#pragma once

#include "CoreMinimal.h"
#include "Faye/UI/FayeBaseWidget.h"
#include "GameFramework/PlayerController.h"
#include "InputActionValue.h"
#include "EnhancedActionKeyMapping.h"
#include "InputMappingContext.h"
#include "FayePlayerController.generated.h"

class USUDSScript;
class USUDSDialogue;
class AFayeCameraManager;
class AShelfActor;
class UMainGameWidget;
class APlayerCharacter;

DECLARE_DYNAMIC_MULTICAST_DELEGATE_OneParam(FOnToggleShelveStacking, bool, bActive);
DECLARE_DYNAMIC_MULTICAST_DELEGATE(FOnCoffeeMakingAction);

/**
 * 
 */
UCLASS()
class FAYE_API AFayePlayerController : public APlayerController
{
	GENERATED_BODY()
public:
	UPROPERTY(EditDefaultsOnly, BlueprintReadOnly)
	TSubclassOf<UMainGameWidget> MainWidgetClass;
	
	virtual void SetupInputComponent() override;
	virtual void ClientRestart_Implementation(class APawn* NewPawn) override;
	
	UFUNCTION(BlueprintCallable)
	void CloseWidget(UFayeBaseWidget* Widget);
	void ToggleCoffeeMaking(bool bActive);

	UFUNCTION(BlueprintPure)
	UMainGameWidget* GetMainGameWidget() const {  return MainGameWidget; }
	
	UPROPERTY(BlueprintAssignable)
	FOnToggleShelveStacking OnToggleShelveStacking;
	
	UPROPERTY(BlueprintAssignable)
	FOnCoffeeMakingAction OnCoffeeMakingAction_GrabStarted;
	UPROPERTY(BlueprintAssignable)
	FOnCoffeeMakingAction OnCoffeeMakingAction_GrabRelease;
	
protected:
	
	UPROPERTY()
	APlayerCharacter* PlayerCharacter;
	
	UPROPERTY()
	AFayeCameraManager* FayeCameraManager;
	
	UPROPERTY()
	bool bInMenu = false;
	UPROPERTY()
	UMainGameWidget* MainGameWidget;
	UPROPERTY()
	TArray<UFayeBaseWidget*> ActiveWidgets;
	
	UPROPERTY()
	AShelfActor* ShelfRef = nullptr;
	
	virtual void BeginPlay() override;
	void OpenGenericWidget(UFayeBaseWidget* Widget);
	
	UFUNCTION(BlueprintCallable, Category = "Faye Player Controller")
	void StartDialogueFromScript(UObject* DialogueParticipant, USUDSScript* Script);
	UFUNCTION(BlueprintCallable, Category = "Faye Player Controller")
	void StartDialogue(UObject* DialogueParticipant, USUDSDialogue* Dialogue);
	
	// Only call from Dialogue UI
	UFUNCTION(BlueprintCallable, Category = "Faye Player Controller")
	void EndDialogue();
	
	UFUNCTION(BlueprintCallable)
	void OpenOptionsWidget();
	UFUNCTION(BlueprintCallable)
	void CloseOptionsWidget();
	UFUNCTION(BlueprintCallable)
	void ClosePhoneWidget();
	UFUNCTION(BlueprintCallable)
	void OpenShelvesStackingWidget(AShelfActor* ShelfActorRef);
	UFUNCTION(BlueprintCallable)
	void CloseShelvesStackingWidget();
	
	
	// ---INPUT STUFF START---
	UPROPERTY(EditDefaultsOnly, BlueprintReadOnly)
	UInputMappingContext* MovementInputMappingContext;
	UPROPERTY(EditDefaultsOnly, BlueprintReadOnly)
	UInputAction* MoveInputAction;
	UPROPERTY(EditDefaultsOnly, BlueprintReadOnly)
	UInputAction* LookInputAction;
	UPROPERTY(EditDefaultsOnly, BlueprintReadOnly)
	UInputAction* ZoomInputAction;
	UPROPERTY(EditDefaultsOnly, BlueprintReadOnly)
	UInputAction* InteractInputAction;
	
	UPROPERTY(EditDefaultsOnly, BlueprintReadOnly)
	UInputMappingContext* UiInputMappingContext;
	UPROPERTY(EditDefaultsOnly, BlueprintReadOnly)
	UInputAction* PhoneInputAction;
	UPROPERTY(EditDefaultsOnly, BlueprintReadOnly)
	UInputAction* OptionsInputAction;
	
	UPROPERTY(EditDefaultsOnly, BlueprintReadOnly)
	UInputMappingContext* ShelveStackingMappingContext;
	UPROPERTY(EditDefaultsOnly, BlueprintReadOnly)
	UInputAction* ShelveStackingBookConfirmAction;
	
	UPROPERTY(EditDefaultsOnly, BlueprintReadOnly)
	UInputMappingContext* CoffeeMakingMappingContext;
	UPROPERTY(EditDefaultsOnly, BlueprintReadOnly)
	UInputAction* CoffeeMakingGrabAction;
	
	void OnMove(const FInputActionValue& Value);
	void OnLook(const FInputActionValue& Value);
	void OnZoom(const FInputActionValue& Value);
	void OnInteract(const FInputActionValue& Value);
	
	//UI
	void OnPhoneToggle(const FInputActionValue& Value);
	void OnOptionsToggle(const FInputActionValue& Value);
	
	void OnShelveBookPlacementConfirm(const FInputActionValue& Value);
	
	void OnCoffeeMakingGrabActionPress(const FInputActionValue& Value);
	void OnCoffeeMakingGrabActionRelease(const FInputActionValue& Value);
	// ---INPUT STUFF END---
};
