package projectzombie.input;

import static org.lwjgl.glfw.GLFW.GLFW_KEY_0;
import static org.lwjgl.glfw.GLFW.GLFW_KEY_1;
import static org.lwjgl.glfw.GLFW.GLFW_KEY_2;
import static org.lwjgl.glfw.GLFW.GLFW_KEY_3;
import static org.lwjgl.glfw.GLFW.GLFW_KEY_4;
import static org.lwjgl.glfw.GLFW.GLFW_KEY_5;
import static org.lwjgl.glfw.GLFW.GLFW_KEY_6;
import static org.lwjgl.glfw.GLFW.GLFW_KEY_7;
import static org.lwjgl.glfw.GLFW.GLFW_KEY_8;
import static org.lwjgl.glfw.GLFW.GLFW_KEY_9;
import static org.lwjgl.glfw.GLFW.GLFW_KEY_A;
import static org.lwjgl.glfw.GLFW.GLFW_KEY_D;
import static org.lwjgl.glfw.GLFW.GLFW_KEY_E;
import static org.lwjgl.glfw.GLFW.GLFW_KEY_ESCAPE;
import static org.lwjgl.glfw.GLFW.GLFW_KEY_F11;
import static org.lwjgl.glfw.GLFW.GLFW_KEY_LEFT_CONTROL;
import static org.lwjgl.glfw.GLFW.GLFW_KEY_Q;
import static org.lwjgl.glfw.GLFW.GLFW_KEY_RIGHT_CONTROL;
import static org.lwjgl.glfw.GLFW.GLFW_KEY_S;
import static org.lwjgl.glfw.GLFW.GLFW_KEY_W;
import static org.lwjgl.glfw.GLFW.GLFW_RELEASE;
import static projectzombie.input.GameInput.backButton_last;
import static projectzombie.input.GameInput.buttonL;
import static projectzombie.input.GameInput.buttonR;
import static projectzombie.input.GameInput.moveDown;
import static projectzombie.input.GameInput.moveLeft;
import static projectzombie.input.GameInput.moveRight;
import static projectzombie.input.GameInput.moveUp;
import static projectzombie.input.GameInput.move_last;

import org.lwjgl.glfw.GLFWKeyCallbackI;

import gl_engine.vec.Vec2d;
import mainloop.task.IMainloopTask;
import projectzombie.Main;
import projectzombie.input.types.Input;

public class KeyCallback implements GLFWKeyCallbackI, IMainloopTask
{
	private boolean itemDrop_last = false;
	private boolean esc_last = false;
	private boolean fullscreen_last = false;
	private boolean buttonL_last = false;
	private boolean buttonR_last = false;
	private boolean inventory_last = false;
	private boolean lctrl_pressed = false;
	private boolean rctrl_pressed = false;

	@Override
	public void invoke(long window, int key, int scancode, int action, int mods)
	{
		Input input = Main.menu.input;
		boolean pressed = ! ( action == GLFW_RELEASE );
		
		InputMode.Controller = false;
		
		if(key == GLFW_KEY_LEFT_CONTROL) {
			lctrl_pressed = pressed;
		}
		if(key == GLFW_KEY_RIGHT_CONTROL) {
			rctrl_pressed = pressed;
		}
		
		if(key == GLFW_KEY_W) {
			moveUp = pressed;
		}
		
		if(key == GLFW_KEY_S) {
			moveDown = pressed;
		}
		
		if(key == GLFW_KEY_A) {
			moveLeft = pressed;
		}
		
		if(key == GLFW_KEY_D) {
			moveRight = pressed;
		}
		
		if(pressed)
		{
			input.key(key);
			
			if(lctrl_pressed || rctrl_pressed) {
				input.keyCtrl(key);
			}
			
			if(key == GLFW_KEY_1) {
				input.hotbarGoto(true, 0);
			}
			if(key == GLFW_KEY_2) {
				input.hotbarGoto(true, 1);
			}
			if(key == GLFW_KEY_3) {
				input.hotbarGoto(true, 2);
			}
			if(key == GLFW_KEY_4) {
				input.hotbarGoto(true, 3);
			}
			if(key == GLFW_KEY_5) {
				input.hotbarGoto(true, 4);
			}
			if(key == GLFW_KEY_6) {
				input.hotbarGoto(true, 5);
			}
			if(key == GLFW_KEY_7) {
				input.hotbarGoto(true, 6);
			}
			if(key == GLFW_KEY_8) {
				input.hotbarGoto(true, 7);
			}
			if(key == GLFW_KEY_9) {
				input.hotbarGoto(true, 8);
			}
			if(key == GLFW_KEY_0) {
				input.hotbarGoto(true, 9);
			}
		}
		
		if(key == GLFW_KEY_Q) {
			if(pressed) {
				if(!itemDrop_last) {
					itemDrop_last = true;
					input.itemDrop(true);
				}
			}
			else if(itemDrop_last) {
				itemDrop_last = false;
			}
		}
		
		if(key == GLFW_KEY_E) {
			if(pressed) {
				if(!inventory_last) {
					inventory_last = true;
				}
			}
			else if(inventory_last) {
				inventory_last = false;
				input.openInventory();
			}
		}
		
		if(key == GLFW_KEY_ESCAPE) {
			if(pressed) {
				if(!esc_last) {
					esc_last = true;
					input.pause(true);
				}
			}
			else if(esc_last) {
				esc_last = false;
			}
			
			if(pressed) {
				if(!backButton_last) {
					backButton_last = true;
					input.back(true);
				}
			}
			else if(backButton_last) {
				backButton_last = false;
			}
		}
		
		if(key == GLFW_KEY_F11) {
			if(pressed) {
				if(!fullscreen_last) {
					fullscreen_last = true;
					Main.window.toggleFullscreen();
				}
			} else if(fullscreen_last) {
				fullscreen_last = false;
			}
		}
	}

	@Override
	public boolean MainLoopDelay(long millis) {
		return millis > Main.tickrate;
	}

	@Override
	public boolean MainLoopRepeat() {
		return true;
	}

	@Override
	public void MainLoopUpdate()
	{
		Input input = Main.menu.input;
		
		Vec2d move_point = new Vec2d(0, 0);
		
		if(moveUp) {
			move_point.x += 1;
		}
		
		if(moveDown) {
			move_point.x -= 1;
		}
		
		if(moveLeft) {
			move_point.y += 1;
		}
		
		if(moveRight) {
			move_point.y -= 1;
		}
		
		if(move_point.x != 0 || move_point.y != 0) {
			move_last = true;
			double angle = Math.toDegrees(Math.atan2(move_point.y, move_point.x));
			input.move(true, angle);
		}
		
		if(buttonL) {
			buttonL_last = true;
			input.fire(true);
		}
		
		else if(buttonL_last) {
			buttonL_last = false;
			input.fire(false);
		}
		
		if(buttonR) {
			buttonR_last = true;
			input.itemAction(true);
		}
		
		else if(buttonR_last) {
			buttonR_last = false;
			input.itemAction(false);
		}
	}

}
