using System;

using RobocraftX.Common;
using RobocraftX.Common.Input;
using Svelto.ECS;

using GamecraftModdingAPI.Utility;

namespace GamecraftModdingAPI.Input
{
    public static class FakeInput
    {
		private static readonly FakeInputEngine inputEngine = new FakeInputEngine();

        /// <summary>
        /// Customize the player input.
        /// </summary>
        /// <param name="input">The custom input.</param>
        /// <param name="playerID">The player. Omit this to use the local player.</param>
		public static void CustomInput(LocalInputEntityStruct input, uint playerID = uint.MaxValue)
		{
			if (playerID == uint.MaxValue)
            {
                playerID = inputEngine.GetLocalPlayerID();
            }
			inputEngine.SendCustomInput(input, playerID);
		}

		public static LocalInputEntityStruct GetInput(uint playerID = uint.MaxValue)
		{
			if (playerID == uint.MaxValue)
            {
                playerID = inputEngine.GetLocalPlayerID();
            }
			return inputEngine.GetInput(playerID);
		}

        /// <summary>
        /// Fake a GUI input.
		/// Omit any parameter you do not want to affect.
		/// Parameters that end with "?" don't do anything... yet.
        /// </summary>
        /// <param name="playerID">The player. Omit this to use the local player.</param>
        /// <param name="hotbar">Select the hotbar slot by number.</param>
        /// <param name="commandLine">Toggle the command line?</param>
        /// <param name="escape">Open escape menu?</param>
        /// <param name="enter">Page return?</param>
        /// <param name="debug">Toggle debug display?</param>
        /// <param name="next">Select next?</param>
        /// <param name="previous">Select previous?</param>
        /// <param name="tab">Tab?</param>
        /// <param name="colour">Toggle to hotbar colour mode?</param>
        /// <param name="hotbarPage">Select the hotbar page by number?</param>
        /// <param name="quickSave">Quicksave?</param>
        /// <param name="paste">Paste?</param>
		public static void GuiInput(uint playerID = uint.MaxValue, int hotbar = -1, bool commandLine = false, bool escape = false, bool enter = false, bool debug = false, bool next = false, bool previous = false, bool tab = false, bool colour = false, int hotbarPage = -1, bool quickSave = false, bool paste = false)
		{
			if (playerID == uint.MaxValue)
            {
                playerID = inputEngine.GetLocalPlayerID();
            }
			ref LocalInputEntityStruct currentInput = ref inputEngine.GetInputRef(playerID);
			//Utility.Logging.CommandLog($"Current sim frame {currentInput.frame}");
			// set inputs
			switch(hotbar)
			{
				case 0: currentInput.guiMask |= RobocraftX.Common.Input.GuiInput.Hotbar_0; break;
				case 1: currentInput.guiMask |= RobocraftX.Common.Input.GuiInput.Hotbar_1; break;
				case 2: currentInput.guiMask |= RobocraftX.Common.Input.GuiInput.Hotbar_2; break;
				case 3: currentInput.guiMask |= RobocraftX.Common.Input.GuiInput.Hotbar_3; break;
				case 4: currentInput.guiMask |= RobocraftX.Common.Input.GuiInput.Hotbar_4; break;
				case 5: currentInput.guiMask |= RobocraftX.Common.Input.GuiInput.Hotbar_5; break;
				case 6: currentInput.guiMask |= RobocraftX.Common.Input.GuiInput.Hotbar_6; break;
				case 7: currentInput.guiMask |= RobocraftX.Common.Input.GuiInput.Hotbar_7; break;
				case 8: currentInput.guiMask |= RobocraftX.Common.Input.GuiInput.Hotbar_8; break;
				case 9: currentInput.guiMask |= RobocraftX.Common.Input.GuiInput.Hotbar_9; break;
				default: break;
			}
			if (commandLine) currentInput.guiMask |= RobocraftX.Common.Input.GuiInput.ToggleCommandLine;
			if (escape) currentInput.guiMask |= RobocraftX.Common.Input.GuiInput.Escape;
			if (enter) currentInput.guiMask |= RobocraftX.Common.Input.GuiInput.Return;
			if (debug) currentInput.guiMask |= RobocraftX.Common.Input.GuiInput.ToggleDebugDisplay;
			if (next) currentInput.guiMask |= RobocraftX.Common.Input.GuiInput.SelectNext;
			if (previous) currentInput.guiMask |= RobocraftX.Common.Input.GuiInput.SelectPrev;
			if (tab) currentInput.guiMask |= RobocraftX.Common.Input.GuiInput.Tab;
			switch (hotbarPage)
            {
                case 1: currentInput.guiMask |= RobocraftX.Common.Input.GuiInput.HotbarPage1; break;
				case 2: currentInput.guiMask |= RobocraftX.Common.Input.GuiInput.HotbarPage2; break;
				case 3: currentInput.guiMask |= RobocraftX.Common.Input.GuiInput.HotbarPage3; break;
				case 4: currentInput.guiMask |= RobocraftX.Common.Input.GuiInput.HotbarPage4; break;
				case 5: currentInput.guiMask |= RobocraftX.Common.Input.GuiInput.HotbarPage5; break;
				case 6: currentInput.guiMask |= RobocraftX.Common.Input.GuiInput.HotbarPage6; break;
				case 7: currentInput.guiMask |= RobocraftX.Common.Input.GuiInput.HotbarPage7; break;
				case 8: currentInput.guiMask |= RobocraftX.Common.Input.GuiInput.HotbarPage8; break;
				case 9: currentInput.guiMask |= RobocraftX.Common.Input.GuiInput.HotbarPage9; break;
				case 10: currentInput.guiMask |= RobocraftX.Common.Input.GuiInput.HotbarPage10; break;
				default: break;
            }
			if (quickSave) currentInput.guiMask |= RobocraftX.Common.Input.GuiInput.QuickSave;
			if (paste) currentInput.guiMask |= RobocraftX.Common.Input.GuiInput.PasteSelection;
		}

		public static void ActionInput(uint playerID = uint.MaxValue, bool toggleMode = false, bool forward = false, bool backward = false, bool up = false, bool down = false, bool left = false, bool right = false, bool sprint = false, bool toggleFly = false, bool alt = false, bool primary = false, bool secondary = false, bool tertiary = false, bool primaryHeld = false, bool secondaryHeld = false, bool toggleUnitGrid = false, bool ctrl = false, bool toggleColourMode = false, bool scaleBlockUp = false, bool scaleBlockDown = false, bool rotateBlockClockwise = false, bool rotateBlockCounterclockwise = false, bool cutSelection = false, bool copySelection = false, bool deleteSelection = false)
		{
			if (playerID == uint.MaxValue)
            {
                playerID = inputEngine.GetLocalPlayerID();
            }
            ref LocalInputEntityStruct currentInput = ref inputEngine.GetInputRef(playerID);
			//Utility.Logging.CommandLog($"Current sim frame {currentInput.frame}");
			// set inputs
			if (toggleMode) currentInput.actionMask |= RobocraftX.Common.Input.ActionInput.ToggleTimeRunningMode;
			if (forward) currentInput.actionMask |= RobocraftX.Common.Input.ActionInput.Forward;
			if (backward) currentInput.actionMask |= RobocraftX.Common.Input.ActionInput.Backward;
			if (up) currentInput.actionMask |= RobocraftX.Common.Input.ActionInput.Up;
			if (down) currentInput.actionMask |= RobocraftX.Common.Input.ActionInput.Down;
			if (left) currentInput.actionMask |= RobocraftX.Common.Input.ActionInput.Left;
			if (right) currentInput.actionMask |= RobocraftX.Common.Input.ActionInput.Right;
			if (sprint) currentInput.actionMask |= RobocraftX.Common.Input.ActionInput.Sprint;
			if (toggleFly) currentInput.actionMask |= RobocraftX.Common.Input.ActionInput.SwitchFlyMode;
			if (alt) currentInput.actionMask |= RobocraftX.Common.Input.ActionInput.AltAction;
			if (primary) currentInput.actionMask |= RobocraftX.Common.Input.ActionInput.PrimaryAction;
			if (secondary) currentInput.actionMask |= RobocraftX.Common.Input.ActionInput.SecondaryAction;
			if (tertiary) currentInput.actionMask |= RobocraftX.Common.Input.ActionInput.TertiaryAction;
			if (primaryHeld) currentInput.actionMask |= RobocraftX.Common.Input.ActionInput.PrimaryActionHeld;
			if (secondaryHeld) currentInput.actionMask |= RobocraftX.Common.Input.ActionInput.SecondaryActionHeld;
			if (toggleUnitGrid) currentInput.actionMask |= RobocraftX.Common.Input.ActionInput.ToggleUnitGrid;
			if (ctrl) currentInput.actionMask |= RobocraftX.Common.Input.ActionInput.CtrlAction;
			if (toggleColourMode) currentInput.actionMask |= RobocraftX.Common.Input.ActionInput.ToggleColourMode;
			if (scaleBlockUp) currentInput.actionMask |= RobocraftX.Common.Input.ActionInput.ScaleBlockUp;
			if (scaleBlockDown) currentInput.actionMask |= RobocraftX.Common.Input.ActionInput.ScaleBlockDown;
			if (rotateBlockClockwise) currentInput.actionMask |= RobocraftX.Common.Input.ActionInput.RotateBlockClockwise;
			if (rotateBlockCounterclockwise) currentInput.actionMask |= RobocraftX.Common.Input.ActionInput.RotateBlockAnticlockwise;
			if (cutSelection) currentInput.actionMask |= RobocraftX.Common.Input.ActionInput.CutSelection; 
			if (copySelection) currentInput.actionMask |= RobocraftX.Common.Input.ActionInput.CopySelection;
			if (deleteSelection) currentInput.actionMask |= RobocraftX.Common.Input.ActionInput.DeleteSelection;
		}

        public static void Init()
		{
			GameEngineManager.AddGameEngine(inputEngine);
			MenuEngineManager.AddMenuEngine(inputEngine);
		}
    }
}