// 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", 3, 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;
  if (parts[0].countsToGo < parts[1].countsToGo) {
    smallest = parts[0].countsToGo;
    secondSmallest = parts[1].countsToGo;
  }
  else {
    smallest = parts[1].countsToGo;
    secondSmallest = parts[0].countsToGo;
  }
  for (int i = 0; i < 5; i++) {
    if (smallest > parts[i].countsToGo) {
      secondSmallest = smallest;
      smallest = parts[i].countsToGo;
    }
    else if (parts[i].countsToGo < secondSmallest) {
      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:

}