#ifndef BLOB_H
#define BLOB_H

#include "lp_vector2.h"
#include "lp_pointmass2.h"
#include "lp_stick2.h"
#include "lp_joint2.h"
#include "lp_array.h"
#include "env.h"

#define NUM_POINTMASSES 40

class Blob
{
public:
  Blob();
  Blob(const float xPos, const float yPos, const float aRadius);

  void Move(const float dt);
  void Satisfy(Env& env);
  void CollisionTest(Blob& another);
  void SetForce(LP_Vector2& aForce);
  void AddForce(LP_Vector2& aForce);
  float GetRadius()
  {
    return radius;
  }
  LP_Vector2& GetPosition()
  {
    return middle.currPos;
  }

  void Draw();

private:
  LP_Pointmass2 middle;
  float radius;
  LP_Array<LP_Stick2, NUM_POINTMASSES> sticks;
  LP_Array<LP_Stick2*, NUM_POINTMASSES> sticksRandomAccess;
  LP_Array<LP_Joint2, NUM_POINTMASSES*5> joints;
  LP_Array<LP_Joint2*, NUM_POINTMASSES*5> jointsRandomAccess;
  LP_Array<LP_Pointmass2, NUM_POINTMASSES> pointmasses;
  LP_Array<LP_Pointmass2*, NUM_POINTMASSES> pointmassesRandomAccess;

  int PrivClampIndex(int index, const int highBound) const;
  void PrivComputeMiddle(LP_Vector2& aMiddle);
  float PrivComputeVolume();
  void PrivPumpBlob();
  static bool PrivSimpleDistanceTest(Blob& a, Blob& b);
  static void PrivFindSeparationPlane(Blob& a, Blob& b, LP_Vector2& aMiddle, LP_Vector2& resultA, LP_Vector2& resultB);
  static bool PrivLineIntersect(LP_Vector2& lineA, LP_Vector2& lineB, LP_Vector2& segA, LP_Vector2& segB, LP_Vector2& result);

  void PrivDrawArc(float radius, float startDeg, float numDegs, bool filled);
  void PrivDrawFace();
  void PrivDrawBody();
  void PrivDrawMouth();
  void PrivDrawEyes();

  enum
  {
    MOUTH_OPEN,
    MOUTH_CLOSED
  };

  enum
  {
    EYES_OPEN,
    EYES_BLINK,
  };

  int mouthStyle;
  int eyeStyle;
};

#endif
