package home;

import java.awt.AWTException;
import java.awt.MouseInfo;
import java.awt.Point;
import java.awt.PointerInfo;
import java.awt.Robot;
import java.awt.Toolkit;
import java.awt.event.KeyEvent;
import java.util.ArrayList;

import util.WAV;
import world.GameObject;

public class Player extends GameObject {
	
	public Player() {
		
	}
	
	public void update() {
		walk();
	}
	
	private double speed = 1;
	
	private double walkCycleIncrement = .035;
	
	private double fallAcceleration = Double.MIN_VALUE;
	
	private void walk() {
		
		ArrayList<Integer> keysDown = input.getKeysDown();
		
		processMouseGround();
		
		boolean isWalking = false;
		boolean running = false;
		
		for(int i = 0; i < keysDown.size(); i++) {
			
			if(fallAcceleration == Double.MIN_VALUE) {
				
				// walk
				
				if(keysDown.get(i) == KeyEvent.VK_W) {
					camera.move(speed);
					isWalking = true;
				}
	
				if(keysDown.get(i) == KeyEvent.VK_S) {
					camera.move(-speed);
					isWalking = true;
				}
	
				if(keysDown.get(i) == KeyEvent.VK_A) {
					camera.strafeHorizontal(-speed);
					isWalking = true;
				}
	
				if(keysDown.get(i) == KeyEvent.VK_D) {
					camera.strafeHorizontal(speed);
					isWalking = true;
				}
				
				// running
				
				if(keysDown.get(i) == KeyEvent.VK_SHIFT)
					running = true;
				
				// running
				
				if(keysDown.get(i) == KeyEvent.VK_SPACE)
					fallAcceleration = 5;
				
				if(camera.getYPosition() != 60)
					camera.setYPosition(60);
			}
		}
		
		if(running)
			speed = 5;
		
		else
			speed = 2;
		
		//stepping sound
		
		if(isWalking)
			
			if(running)
				walkCycleCount += walkCycleIncrement * 1.5;
			
			else
				walkCycleCount += walkCycleIncrement;
		
		// staying withing map borders
		
		if(camera.getXPosition() < -4850)
			camera.setXPosition(-4850);
		
		if(camera.getZPosition() < -4850)
			camera.setZPosition(-4850);
		
		if(camera.getXPosition() > 4850)
			camera.setXPosition(4850);
		
		if(camera.getZPosition() > 4850)
			camera.setZPosition(4850);
		
		if(camera.getYPosition() > 1500)
			camera.setYPosition(1500);
		
		// falling
		
		if(fallAcceleration != Double.MIN_VALUE) {
			
			camera.setYPosition(camera.getYPosition() + fallAcceleration);
			
			fallAcceleration -= .3;
			
			if(camera.getYPosition() < 40) {
				
				fallAcceleration = Double.MIN_VALUE;
				
				new WAV("/audio/FX/Land.wav").play(1);
			}
		}
		
		if(fallAcceleration == Double.MIN_VALUE) {
			
			if(camera.getYPosition() < 60)
				camera.setYPosition(camera.getYPosition() + .15);
			
			if(camera.getYPosition() < 30)
				camera.setYPosition(60);
			
			walkCycle();
		}
	}
	
	Robot robot;
	
	private void processMouseGround() {
		
		if(robot == null)
			
			try {
				robot = new Robot();
			}
			
			catch (AWTException exception) {
				exception.printStackTrace();
			}
		
		PointerInfo pointer = MouseInfo.getPointerInfo();
		Point point = pointer.getLocation();
		
		double rotateFactor = 1000;
		
		if(camera.getYAngle() == 1)
			
			camera.rotate((point.getX() - Toolkit.getDefaultToolkit().getScreenSize().getWidth() / 2) / rotateFactor,
					(point.getY() - Toolkit.getDefaultToolkit().getScreenSize().getHeight() / 2) / -rotateFactor);
		
		else
			
			camera.rotate(-(point.getX() - Toolkit.getDefaultToolkit().getScreenSize().getWidth() / 2) / rotateFactor,
					(point.getY() - Toolkit.getDefaultToolkit().getScreenSize().getHeight() / 2) / -rotateFactor);
		
		if(camera.getYAngle() == -1)
			
			camera.rotate(0, (point.getY() - Toolkit.getDefaultToolkit().getScreenSize().getHeight() / 2) / rotateFactor);
		
		robot.mouseMove((int)(Toolkit.getDefaultToolkit().getScreenSize().getWidth() / 2),
				(int)(Toolkit.getDefaultToolkit().getScreenSize().getHeight()) / 2);
	}
	
	private double walkCycleCount;
	
	private void walkCycle() {
		
		if(walkCycleCount >= 1) {
			
			new WAV("/audio/FX/Walk.wav").play(1);
			
			walkCycleCount = 0;
		}
	}
	
	public void interact(GameObject object) {
		 
	}
}