#include "DefensePoint.h"

DefensePoint::DefensePoint()
{
  clear();
}

DefensePoint::~DefensePoint()
{
  clear();
}

void DefensePoint::clear()
{
  timeElapsed=0;
  lastTimePositionWasCalled=0;
  startTime=0;
  stopReverseTime=0;
  reverse=DEFENSE_POINT_NORMAL;
  hitPoints=5;
  posX=0;
  pfX.clear();
  posY=0;
  pfY.clear();
}

void DefensePoint::setHitPoints(int hp)
{
  hitPoints=hp;
}

void DefensePoint::addFormulaX(PositionFormula & pfs)
{
  pfX.push_back(pfs);
}

void DefensePoint::addFormulaY(PositionFormula & pfs)
{
  pfY.push_back(pfs);
}

//sets startTime to timeRightNow
void DefensePoint::init(int timeRightNow)
{
  startTime=timeRightNow;
  lastTimePositionWasCalled=timeRightNow;
  timeElapsed=0;
}

//Go through all the formulae
//returns true if any formula applies to this point at the given time tim.
bool DefensePoint::setPosition(FixedInt speedMultiplier)
{
  FixedInt pos;
  bool formulaAppliesX=false, formulaAppliesY=false;
  int timeRightNow, timeDifference;

  timeRightNow=SDL_GetTicks();
  timeDifference=timeRightNow-lastTimePositionWasCalled;
  lastTimePositionWasCalled=timeRightNow;
  
  //If reverse,
  if (reverse==DEFENSE_POINT_REVERSE)
    {      
      //unless that would remove the point from the field,
      setPositionHelper(timeElapsed-timeDifference, pfX.begin(), pfX.end(), formulaAppliesX);
      setPositionHelper(timeElapsed-timeDifference, pfY.begin(), pfY.end(), formulaAppliesY);

      if (formulaAppliesY || formulaAppliesX)
	//subtract the difference
	timeElapsed-=timeDifference;

      //   (don't add for last half second?)
    }

  if (reverse!=DEFENSE_POINT_NORMAL)
    {
      //if time is past stopReverseTime then reverse=false
      if (timeRightNow > stopReverseTime)
	  reverse=DEFENSE_POINT_NORMAL;
    }
  //If not reverse
  //   add to elapsed time
  else
    timeElapsed+=(speedMultiplier*timeDifference).toInt();

  pos=0;
  pos=setPositionHelper(timeElapsed, pfX.begin(), pfX.end(), formulaAppliesX);
  posX=pos.toInt();

  pos=0;
  pos=setPositionHelper(timeElapsed, pfY.begin(), pfY.end(), formulaAppliesY);
  posY=pos.toInt();
  return (formulaAppliesX || formulaAppliesY);
}

FixedInt DefensePoint::setPositionHelper(int tim, vector<PositionFormula>::iterator startIter, vector<PositionFormula>::iterator endIter, bool & formulaApplies)
{
  FixedInt ans=0, formulaAns;
  formulaApplies=false;
  while (startIter!=endIter)
    {
      if (startIter->position(tim, formulaAns))
        formulaApplies=true;
      startIter++;
      ans+=formulaAns;
    }
  return ans;
}

int DefensePoint::getPositionX()
{
  return posX;
}

int DefensePoint::getPositionY()
{
  return posY;
}

int DefensePoint::getHP()
{
  return hitPoints;
}

void DefensePoint::addHP(int change)
{
  hitPoints+=change;
}

//sets reverse flags and will reverse for the next second.
void DefensePoint::startReversing (int timeRightNow, int activity)
{
  reverse=activity;
  stopReverseTime=timeRightNow+1000;
}

int DefensePoint::getStopReverseTime()
{
  return stopReverseTime;
}
