﻿using UnityEngine;

namespace AC.CombatExample
{

	public class CustomInteraction : MonoBehaviour
	{

		// Handles the display of the Interaction menu


		#region Variables

		private PlayerCombat playerCombat;
		private Menu interactionMenu;
		private Menu inventoryMenu;
		private GVar pauseEnemiesVariable;

		#endregion


		#region UnityStandards

		private void Awake ()
		{
			playerCombat = GetComponent <PlayerCombat>();
		}


		private void OnEnable ()
		{
			EventManager.OnHotspotInteract += OnHotspotInteract;
			EventManager.OnInventoryInteract += OnInventoryInteract;
			EventManager.OnMenuTurnOn += OnMenuTurnOn;
			EventManager.OnMenuTurnOff += OnMenuTurnOff;
		}


		private void OnDisable ()
		{
			EventManager.OnHotspotInteract -= OnHotspotInteract;
			EventManager.OnInventoryInteract -= OnInventoryInteract;
			EventManager.OnMenuTurnOn -= OnMenuTurnOn;
			EventManager.OnMenuTurnOff -= OnMenuTurnOff;
		}


		private void Start ()
		{
			interactionMenu = PlayerMenus.GetMenuWithName ("Interaction");
			inventoryMenu = PlayerMenus.GetMenuWithName ("Inventory");
			pauseEnemiesVariable = GlobalVariables.GetVariable ("PauseEnemies");
		}


		private void Update ()
		{
			// If the Player presses InteractionA while in the vicinity of a Hotspot, turn on the Interaction menu
			Hotspot hotspot = KickStarter.playerInteraction.GetActiveHotspot ();
			if (hotspot && !hotspot.IsSingleInteraction () && inventoryMenu.IsOff () && interactionMenu.IsOff ())
			{
				if (playerCombat == null || !playerCombat.IsAiming ())
				{
					if (KickStarter.playerInput.InputGetButtonDown ("InteractionA"))
					{
						interactionMenu.MatchInteractions (hotspot, false);
						interactionMenu.TurnOn ();
					}
				}
			}

			// Pause enemies if we're not in gameplay, or if the Interaction / Inventory menu is on
			if (pauseEnemiesVariable != null)
			{
				pauseEnemiesVariable.BooleanValue = (!KickStarter.stateHandler.IsInGameplay () || interactionMenu.IsOn () || inventoryMenu.IsOn ());
			}
		}

		#endregion


		#region CustomEvents

		private void OnHotspotInteract (Hotspot hotspot, Button button)
		{
			// Always turn off the Interaction menu if we use a Hotspot
			interactionMenu.TurnOff ();
		}


		private void OnInventoryInteract (InvItem invItem, int iconID)
		{
			// Always turn off the Interaction menu if we use an Inventory item
			interactionMenu.TurnOff ();
		}


		private void OnMenuTurnOn (Menu menu, bool isInstant)
		{
			if (menu == interactionMenu)
			{
				if (KickStarter.settingsManager.inputMethod == InputMethod.TouchScreen)
				{
					KickStarter.playerInput.forceGameplayCursor = ForceGameplayCursor.KeepUnlocked;
				}
				else
				{
					KickStarter.playerInput.forceGameplayCursor = ForceGameplayCursor.KeepLocked;
					KickStarter.playerInput.canKeyboardControlMenusDuringGameplay = true;
				}
			}
		}


		private void OnMenuTurnOff (Menu menu, bool isInstant)
		{
			if (menu == interactionMenu)
			{
				KickStarter.playerInput.forceGameplayCursor = ForceGameplayCursor.None;
				KickStarter.playerInput.canKeyboardControlMenusDuringGameplay = false;
			}
		}

		#endregion

	}

}