/**
 * 	Kepler - Core
 * 	
 * 	All rights reserved.
 *
 * 	Author: Kaj Dijksta
 *
 **/
import keplerEngine from './kepler.js';
import {vector2, vector3} from './math.js';
//import vector2 from './math.js';
//import vector3 from './math.js';
 
/**
 * Event Manager
 * Manage all mouse and keyboard events in this class 
 */
class eventManager {
	
	constructor( viewport ){

		this.tempMouse = new vector2(0,0);
		this.oldMousPos = new vector2(0,0);
		this.mouseVerschil = new vector2(0,0);
		this.mouseDown = new vector3(0,0,0);

		
		$(viewport.canvas).data("viewport", this.viewport);
		
		viewport.canvas.addEventListener("mousemove", function(e){  
			
			kepler.viewport = viewport;
			viewport.events.mouseMoveHandler(e, viewport); 
		
		});
		viewport.canvas.addEventListener("mousedown", function(e){ viewport.events.mouseDownHandler(e, viewport); }); 
		viewport.canvas.addEventListener("mouseup", function(e){ viewport.events.mouseUpHandler(e, viewport); });

		window.addEventListener("keydown", function(e){ viewport.events.keyDownHandler(e, kepler.viewport); });
		window.addEventListener("keyup", function(e){ viewport.events.keyUpHandler(e, kepler.viewport); });
		
		this.lastTime = new Date().getTime();
		this.elapsed = 0;
		this.timeNow = 0;
		this.clientMouse =  new vector2(0,0);
		this.keysDown = false;
		
	}

	setViewport( viewport ){
		
		this.viewport 	= viewport;
		
	}

	/**
	 * Mouse over event handler.
	 * @param {(event)} event.
	**/
	mouseMoveHandler ( e, viewport ) {
	// console.log(viewport);
		this.tempMouse = [ e.screenX, e.screenY ];
		this.clientMouse = [ e.screenX , e.screenY ];
		
		if( this.mouseDown[1] || this.mouseDown[2] )
			this.lastTime  = new Date().getTime();

		if(!this.oldMousPos)
			this.oldMousPos = this.tempMouse;
	};


	/** 
	 * Mouse down event handler.
	 * @param {(event)} event.
	 */
	mouseDownHandler (e, viewport) {
	
		switch(e.button)
		{
			case 0:
				this.mouseDown[1] = true;
				if(this.keysDown  == 16)
				readPixel();
			break;
			case 1:
				this.mouseDown[0] = true; 
			break;
			case 2:
				this.mouseDown[2] = true;
			break;
		}
	}


	/**
	 * Mouse up event handler
	 * @param {(event)} event.
	**/
	mouseUpHandler ( e, viewport ) {

		switch(e.button)
		{
			case 0:
				this.mouseDown[1] = false;
			break;
			case 1:
				this.mouseDown[0] = false;
			break;
			case 2:
				this.mouseDown[2] = false;
			break;
		}
	}

	/**
	 * Key down event handler
	**/
	keyDownHandler ( e, viewport ) {

		var mainPlayer = viewport.mainPlayer;
	
		switch(e.keyCode) {
			case 68: // a
				
				mainPlayer.right = false;
				mainPlayer.left = true;
			break;
			case 65: // d
				mainPlayer.left = false;
				mainPlayer.right = true;
			break;
			case 87: //w
				mainPlayer.backward = false;
				mainPlayer.forward = true;
			break;
			case 83: //s
				mainPlayer.forward = false;
				mainPlayer.backward = true;
			break;
			case 16: //shift
				viewport.mainPlayer.moveSpeed = .39;
				this.keysDown = 16;
			break;
			case 90:
				mainPlayer.down = true;
			break;
			case 32:
				mainPlayer.up = true;
			break;
			case 67://v
		

			break;
			
		} 
		
		this.keysDown = e.keyCode;
		
	} 	

	/**
	 * Key up event handler
	**/
	keyUpHandler(e, viewport)
	{
		
		var mainPlayer = viewport.mainPlayer;
		
		switch(e.keyCode)
		{
			case 65: // a
				//g_animate = false;
			break;
		}
		
		switch(e.keyCode) {
			case 68: // a
				mainPlayer.left = false;
			break;
			case 65: // d
				mainPlayer.right = false;
			break;
			case 87: //w
				mainPlayer.forward = false;
			break;
			case 83: //s
				mainPlayer.backward = false;
			break;
			case 16: // shift
				viewport.mainPlayer.moveSpeed = .3;
			break;
			case 90:
				mainPlayer.down = false;
			break;
			case 32:
				mainPlayer.up = false;

			break;
			case 67://v
		
			break;
		}
		
			this.keysDown = false;
	}

}

export {eventManager as default};
