import processing.core.PApplet;
import processing.core.PVector;

// Attraction
// Daniel Shiffman <http://www.shiffman.net>

// A class for a draggable attractive body in our world

public class Attractor {
  PApplet parent;
  float mass;    // Mass, tied to size
  float G;       // Gravitational Constant
  PVector loc;   // Location
  boolean dragging = false; // Is the object being dragged?
  boolean rollover = false; // Is the mouse over the ellipse?
  PVector drag;  // holds the offset for when object is clicked on

  Attractor(PApplet _p, PVector l_,float m_, float g_) {
    parent = _p;
    loc = l_.get();
    mass = m_;
    G = g_;
    drag = new PVector(0.0f,0.0f);
  }

  /*
  void go() {
    render();
    drag();
  }
  */

  PVector calcGravForce(Particle t) {
    PVector dir = PVector.sub(loc,t.getLoc());        // Calculate direction of force
    float d = dir.mag();                              // Distance between objects
    d = PApplet.constrain(d,1.0f,25.0f);              // Limiting the distance to eliminate "extreme" results for very close or very far objects
    dir.normalize();                                  // Normalize vector (distance doesn't matter here, we just want this vector for direction)
    float force = (G * mass * t.getMass()) / (d * d); // Calculate gravitational force magnitude
    dir.mult(force);                                  // Get force vector --> magnitude * direction
    return dir;
  }

  // Method to display
  void display() {
    parent.ellipseMode(PApplet.CENTER);
    parent.stroke(0);
    if (dragging) parent.fill(50);
    else if (rollover) parent.fill(100);
    else parent.fill(200,0,0);
    parent.ellipse(loc.x,loc.y,mass*2,mass*2);
  }

  // The methods below are for mouse interaction
  void clicked(int mx, int my) {
    float d = PApplet.dist(mx,my,loc.x,loc.y);
    if (d < mass) {
      dragging = true;
      drag.x = loc.x-mx;
      drag.y = loc.y-my;
    }
  }

  void rollover(int mx, int my) {
    float d = PApplet.dist(mx,my,loc.x,loc.y);
    if (d < mass) {
      rollover = true;
    } else {
      rollover = false;
    }
  }

  void stopDragging() {
    dragging = false;
  }
  
 

  void drag() {
    if (dragging) {
      loc.x = parent.mouseX + drag.x;
      loc.y = parent.mouseY + drag.y;
    }
  }

}

