package edu.oswego.tiltandtumble.worldObjects;

import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.Sprite;
import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.physics.box2d.Body;
import com.badlogic.gdx.physics.box2d.Contact;
import com.badlogic.gdx.physics.box2d.BodyDef.BodyType;
import com.badlogic.gdx.physics.box2d.Shape;


import edu.oswego.tiltandtumble.collisionListener.BallCollisionListener;
import edu.oswego.tiltandtumble.levels.Level;
import edu.oswego.tiltandtumble.levels.UnitScale;

public class AttractorForce extends AbstractWorldObject implements WorldUpdateable, BallCollisionListener {

	public static final BodyType BODY_TYPE = BodyType.StaticBody;
	public static final boolean IS_SENSOR = true;
	
	//in meters per second
	public static final float DEFAULT_SPEED = 5f;
	
	private final float speed;
	private final UnitScale scale;


	private boolean collidingWithBall = false;
	private Ball ball;
    
	// TODO: we will will need to simulate radial gravity for this
	public AttractorForce(Body body, float speed, UnitScale scale) {
		super(body);
		this.speed = speed;
		this.scale = scale;
	}

	@Override
	public void update(float delta) {
		if(collidingWithBall) {
		float distanceToTravel = speed * delta;
		float distanceToMiddle = ball.getMapX() - scale.metersToPixels(body.getPosition().x);
		float radius = distanceToMiddle;
		float tempX, tempY;
		Vector2 ballPosition = new Vector2();
		ballPosition.x = ball.getMapX();
		ballPosition.y = ball.getMapY();
		Vector2 ballDistance = new Vector2(0,0);
		ballDistance.add(ballPosition);
		ballDistance.sub(body.getPosition());
		float finalDistance = ballDistance.len();
		
			tempY = ballDistance.y;
			tempX = ballDistance.x;
			ballDistance.x = tempY;
			ballDistance.y = tempX;
			
			float vectorSum = Math.abs(ballDistance.x) + Math.abs(ballDistance.y);
			ballDistance.mul((1/vectorSum)*radius/finalDistance);
			body.applyForce(ballDistance, body.getWorldCenter(), true);
		
		/*
          float a = body.getLocalCenter().x - ball.getMapX();
          float b = body.getLocalCenter().y - ball.getMapY();
          float c = (float) Math.sqrt(Math.pow(a, 2)+Math.pow(b, 2));
           
           float d = c / speed;
           float aa = a / d;
           float bb = b / d;
           
           Vector2 ballPoint = new Vector2(ball.getMapX(), ball.getMapY());
           Vector2 force = new Vector2(aa, bb);
           
           body.applyForce(force, ballPoint, true);
*/
		}
	}

	@Override
	public void handleBeginCollision(Contact contact, Ball ball) {
		collidingWithBall = true;
		this.ball = ball;
	}

	@Override
	public void handleEndCollision(Contact contact, Ball ball) {
		collidingWithBall = false;
		this.ball = null;
		
	}
}
@@ -45,95 +45,8 @@
 s;%0A%0A
-import com.badlogic.gdx.graphics.Texture;%0Aimport com.badlogic.gdx.graphics.g2d.Sprite;%0A
 impo
@@ -127,55 +127,8 @@
 dy;%0A
-import com.badlogic.gdx.physics.box2d.Contact;%0A
 impo
@@ -221,15 +221,16 @@
 x2d.
-Shape;%0A
+Contact;
 %0A%0Aim
@@ -304,54 +304,8 @@
 er;%0A
-import edu.oswego.tiltandtumble.levels.Level;%0A
 impo
@@ -570,17 +570,16 @@
 = true;%0A
-%09
 %0A%09//in m
@@ -642,17 +642,16 @@
 D = 5f;%0A
-%09
 %0A%09privat
@@ -769,20 +769,16 @@
 l ball;%0A
-    
 %0A%09// TOD
@@ -1493,18 +1493,16 @@
 .len();%0A
-%09%09
 %0A%09%09%09temp
@@ -1602,19 +1602,16 @@
  tempX;%0A
-%09%09%09
 %0A%09%09%09floa
@@ -1738,25 +1738,34 @@
 e);%0A
+%0A
 %09%09%09b
-ody
+all
 .apply
-Forc
+LinearImpuls
 e(ba
@@ -1778,42 +1778,29 @@
 ance
+.x
 , b
-ody.getWorldCenter(), true
+allDistance.y
 );%0A
-%09%09
 %0A%09%09/
@@ -1995,27 +1995,16 @@
 b, 2));%0A
-           
 %0A       
@@ -2086,27 +2086,16 @@
  b / d;%0A
-           
 %0A       
@@ -2211,27 +2211,16 @@
 a, bb);%0A
-           
 %0A       
@@ -2524,15 +2524,13 @@
 = null;%0A
-%09%09
 %0A%09%7D%0A%7D

apply the force to the ball rather than the attractor center.