// structure search with auto for loop

// 2nd smallest not working

struct MaintenanceSchedule {
  const char *label;
  uint16_t presets;
  uint16_t countsToGo;
  uint16_t leastRemaining;
  bool needsMaintenance;
};

MaintenanceSchedule parts[] = {
  { "Pusher", 33, 5, 0, false },  // maintain every 7 cycles
  { "Rocker", 9, 0, 0, false },       // maintain every 10 cycles
  { "Plate", 7, 0, 0, false },  // maintain every 3 cycles
  { "Zerks", 10, 0, 0, false },       // maintain every 2 cycles
  { "Cylinder", 13, 0, 0, false },    // maintain every 2 cycles
};

int firstSecond[2];

const int NUM_OF_ELEMENTS = sizeof(parts) / sizeof(parts[0]);
int currentCount = 1;

void setup() {
  Serial.begin(115200);
Serial.println("presets");
  for (auto togo : parts) {
    if (currentCount <= togo.presets) {
      Serial.println(togo.presets);
    }
  }
  
  //
  // record counts to go for each item
  //
  for (auto &lowest : parts) { // ampersand needed for write access to struct
    if ( currentCount < lowest.presets) {
      lowest.countsToGo = lowest.presets - currentCount;
    }
    else lowest.countsToGo = lowest.presets - currentCount % lowest.presets;
  }
  Serial.println("\n================\n");
  Serial.println("counts to go");
// print counts to go
  for (auto togo : parts) {
    if (currentCount <= togo.countsToGo) {
      Serial.println(togo.countsToGo);
    }
  }

  // add code to print data of lowest and second item
  int smallest=INT16_MAX;
  int secondSmallest=INT16_MAX;
 for(int i=0;i<5;i++){
  if(parts[i].countsToGo<smallest){
    smallest=parts[i].countsToGo;
  }
 }
 for(int i=0;i<5;i++){
  if(parts[i].countsToGo<secondSmallest&& parts[i].countsToGo>smallest){
    secondSmallest=parts[i].countsToGo;
  }
 }
  

  Serial.println("\n================\n");
  Serial.println(smallest);
  Serial.println(secondSmallest);


}// end of setup

void loop() {
  // put your main code here, to run repeatedly:

}