/*
 * Copyright 2012, kaj dijkstra , 
 * Author, Kaj Dijkstra.
 * All rights reserved.
 *
 */
 
 
import {math, vector3, matrix4} from './math.js';

/**
 * Player object
**/
class player
{
	
	constructor( engine ) {
		
		this.position = [0,0,0];
		this.groundHeight = this.position[1];
		
		//var math = this.engine.math;

		this.velocity = new vector3( 0.1, -0.1, 0.1 );
		
		this.yaw = 0;
		this.pitch = 0;
		this.roll = 0;
		this.strafe = false;
		
		this.right = false;
		this.left = false;
		this.backward = false;
		this.forward = false;
		this.up = false;;
		this.down = false;;
		
		this.moveSpeed = .02;
		this.bounceSpeed = 0.2;
		this.gravity = 1.32;
		
		this.direction;

		this.damping = 0.6;
		
		
		
	}
	
	setViewport( viewport ){
		
		this.viewport = viewport;
		this.gl = viewport.gl;
	}
	
	/**
	 * update object
	**/
	update() {
		
		
		if(this.viewport.mainCamera.mode == "orbits") {
			var orientation = matrix4.rotationY( math.degToRad(this.yaw-180) );
			var groundHeight = this.groundHeight;
			
			if(this.position[1] < groundHeight) {
				if(this.forward)
					this.velocity[1] = 20;
				else 
					this.velocity[1] *= -this.damping;
					
				if(this.velocity[1] <= 0.2)
					this.velocity[1] = 0;
					
					this.position[1] = groundHeight - 0.01;
				
			} else {
				this.velocity[1] -= this.gravity;
			}
			
			if(Math.abs(this.velocity[0]) <= .2)
				this.velocity[0] = 0;
			if(Math.abs(this.velocity[2]) <= .2)
				this.velocity[2] = 0;
				
				
			var leftVector = new vector3(-1,0,0); 
			var rightVector = new vector3(1,0,0);
			var forwardVector = new vector3(0,0,1);
			var backwardVector = new vector3(0,0,-1);
			var totalVector = new vector3(0,0,0);

			var forward = matrix4.transformDirection(orientation, forwardVector);

			if(this.forward) {
				totalVector = vector3.add(totalVector, forward);
			}
			
			if(this.backward) {
				var backward = matrix4.transformDirection(orientation, backwardVector);
				totalVector = vector3.add(totalVector, backward);
			}
			
			
			if(this.strafe) 
			{
				if(this.left) {
					
					var left = matrix4.transformDirection(orientation, leftVector);
					totalVector = vector3.add(totalVector, left);
					console.log("left", left);
				}
				
				if(this.right) {
					var right = matrix4.transformDirection(orientation, rightVector);
					totalVector = vector3.add(totalVector, right);
				}
			} else {
					if(this.left)
					{
						this.yaw+=7;
						totalVector = vector3.add(totalVector, rightVector );
					}

					if(this.right)
					{
						this.yaw-=7;
						totalVector = vector3.add(totalVector,leftVector );
					}
			}
			
			
			//console.log(totalVector, this.moveSpeed, this.position, this.velocity);
			
			
			if(totalVector[0]+totalVector[1]+totalVector[2] != 0) {
				var normalizedTotal = vector3.normalize(totalVector);
				totalVector = vector3.mulScalarVector(this.moveSpeed, normalizedTotal);
				this.position = vector3.add(totalVector, this.position);
				
				//this.smoothTransition();
			} 
			
			//intergration position
			//this.positionTransform.identity();
			
			console.log(this.position, this.velocity);
			this.position = vector3.add(this.position, this.velocity);
			//this.positionTransform.translate(this.position);
			
			//update camera
			kepler.mainCamera.center[0] = this.position[0];
			kepler.mainCamera.center[1] = this.groundHeight;
			kepler.mainCamera.center[2] = this.position[2];

			//update rotation
			//this.transform.identity();
			//this.transform.rotateY(toRad(this.yaw));
		} else {

			var direction = vector3.normalize(vector3.sub(this.viewport.mainCamera.eye, this.viewport.mainCamera.center));
			
			direction[1] = 0;
			
			var totalVector = [0,0,0];
			var leftVector = [1,0,0]; 
			var rightVector = [-1,0,0];
			var downVector = [0,-1,0];
			var upVector = [0,1,0];
			
			
			var orientation = matrix4.rotationY(math.degToRad(this.viewport.mainCamera.yaw-180));
			
			if(this.forward) {
				totalVector = vector3.add(totalVector, vector3.negativeVector(direction) );
			}
			
			if(this.backward) {
				totalVector = vector3.add(totalVector, direction);
			}

			if(this.left) {
				
				var left = matrix4.transformDirection(orientation, leftVector);
				totalVector = vector3.add(totalVector, left);
			}
			
			if(this.right) {
				var right = matrix4.transformDirection(orientation, rightVector);
				totalVector = vector3.add(totalVector, right);
			}
		
			if(this.down) {
				var down = matrix4.transformDirection(orientation, downVector);
				totalVector = vector3.add(totalVector, down);
			}
			
			if(this.up) {
				var up = matrix4.transformDirection(orientation, upVector);
				totalVector = vector3.add(totalVector, up);
			}
			
			this.direction = totalVector;
			
			totalVector = vector3.mulScalarVector(this.moveSpeed, totalVector);
			
			this.viewport.mainCamera.center = vector3.add(totalVector, this.viewport.mainCamera.center);
		}
	}
} 

export {player as default};
