#include "color_tools.h"
#include <Arduino.h>

#define MAX_HUE 360.0


/************************* CLASS - HUE PALLET DISPENSER ************************/

HUE_PALLET_DISPENSER::HUE_PALLET_DISPENSER(void){
  Initialize(0, 360, 6);
}

void HUE_PALLET_DISPENSER::Initialize(float hue, float range, int colSteps){
  this->range = range;
  this->hueSteps = colSteps;

  this->startHue = hue - this->range / 2;
  if(this->startHue < 0.0){
    this->startHue += MAX_HUE;
  }else if(this->startHue > MAX_HUE){
    this->startHue -= MAX_HUE;
  }

  this->hueIndex = 0;

  if(this->hueSteps <= 1){
    this->hueIncrement = 0.0;
    this->currHue = hue;
  }else{
    this->hueIncrement = this->range / (this->hueSteps - 1);
  }
}


int HUE_PALLET_DISPENSER::GetNextPalletHue(void){
  if(this->hueSteps > 1){
    this->currHue = this->startHue + this->hueIndex * this->hueIncrement;
    if(this->currHue < 0){
      this->currHue += MAX_HUE;
    }else if(this->currHue > MAX_HUE){
      this->currHue -= MAX_HUE;
    }

    // TODO Remove later
    //rgbpixel_t p = HUEtoRGB(huePallet.currHue);
    //Log.traceln("<anim> index: %d, hue= %F, col: %d, %d, %d", huePallet.hueIndex, huePallet.currHue, p.red, p.grn, p.blu);

    this->hueIndex = ++this->hueIndex % this->hueSteps;
    return round(this->currHue);
  }else{
    return round(this->currHue);
  }
}

float HUE_PALLET_DISPENSER::PeekNextPalletHue(int hueOffset){
  float tempHue = this->startHue + (this->hueIndex + hueOffset) * this->hueIncrement;

  if(tempHue < 0){
    tempHue += MAX_HUE;
  }else if(tempHue > MAX_HUE){
    tempHue -= MAX_HUE;
  }
  
  return tempHue;
}

void HUE_PALLET_DISPENSER::SetHueIndex(int hueIndex){
  this->currHue = hueIndex;
}
