#include "BattleAbility.h"

#include <stdint.h>

using namespace std;

BattleAbility :: BattleAbility ()
{
  resetRechargeTime(0);
}

BattleAbility :: ~BattleAbility ()
{
}

//set the recharge time as if the battle started.
//Optional parameter indicates if there is a one-time recharge boost at the start of battle.
void BattleAbility :: resetRechargeTime (uint8_t rt, uint8_t startTime)
{
  rechargeTime=rt;
  timeSpentRecharging=rt+startTime;

  if (startTime!=0)
    oneTimeRechargeBoost=true;
}

//adds to the timeSpentRecharging
void BattleAbility :: addTime (uint8_t amount)
{
  //if the ability is already recharged, don't overcharge it
  if (abilityIsRecharged())
    return;

  if (timeSpentRecharging + amount > 255)
    timeSpentRecharging=255;
  else
    timeSpentRecharging+=amount;
}

//returns true if the ability has recharged
bool BattleAbility :: abilityIsRecharged()
{
  return (timeSpentRecharging>=rechargeTime);
}

//call when power has been used to reset the timeSpentRecharging and oneTimeRechargeBoost
//if oneTimeRechargeBoost, then timeSpentRecharging-=rechargeTime
//otherwise timeSpentRecharging=0
//oneTimeRechargeBoost=false
void BattleAbility :: useRecharge()
{
  if (oneTimeRechargeBoost)
    timeSpentRecharging-=rechargeTime;
  else
    timeSpentRecharging=0;
  oneTimeRechargeBoost=false;
}


//Recharges the fraction the power is recharging, rounded to the nearest 16th.
uint8_t BattleAbility :: getRechargeFraction ()
{
  if (abilityIsRecharged())
    return 0;

  if ((16*(rechargeTime-timeSpentRecharging)/rechargeTime) < 1)
    return 1;

  return (16*(rechargeTime-timeSpentRecharging)/rechargeTime);
}

uint8_t BattleAbility ::getRechargeTimeRemaining()
{
  if (abilityIsRecharged())
    return 0;

  return (rechargeTime-timeSpentRecharging);
}
