/**
   * Adds time to timesheets from two given lists and removes them from the lists
   *
   * @param primaryTimesheets //A list of timesheets that are currently getting the hours
   * @param currentTimesheets //A list of timesheets that are still active
   */
  private void cleanUpRemainingTimeSheets(
      ArrayList<Time_Sheet> primaryTimesheets, ArrayList<Time_Sheet> currentTimesheets) {
    Timestamp latestTimePayedFor = primaryTimesheets.get(primaryTimesheets.size() - 1).getEndTime();

    // removes all the timesheets from current time sheets, that ended before the last primary time
    // sheet
    removeOldTimeSheets(currentTimesheets, latestTimePayedFor);

    // adds time to the remaining primary time sheets
    addTimeToPrimaryTimeSheets(
        primaryTimesheets,
        currentTimesheets,
        primaryTimesheets.get(primaryTimesheets.size() - 1).getEndTime());

    // Adds time to the remaining current time sheets
    for (int j = currentTimesheets.size() - 1; j >= 0; j--) {

      Time_Sheet currenTempTS = currentTimesheets.get(j);
      long millis = currenTempTS.getEndTime().getTime() - latestTimePayedFor.getTime();
      int minutes = (int) (millis / (60 * 1000));
      currenTempTS.addMinute(minutes);
      latestTimePayedFor = currenTempTS.getEndTime();
    }
    currentTimesheets.clear();
  }
  /**
   * Add time to primary timesheets and move them to current timesheets if they're still active
   *
   * @param primaryTimesheets A list of timesheets that are currently getting the hours
   * @param currentTimesheets A list of timesheets that are still active
   * @param currentTime The current time to calculate hours and such from;
   */
  private void addTimeToPrimaryTimeSheets(
      ArrayList<Time_Sheet> primaryTimesheets,
      ArrayList<Time_Sheet> currentTimesheets,
      Timestamp currentTime) {
    // Do for all time sheets in primary time sheet
    for (int j = 0; j < primaryTimesheets.size(); j++) {

      Timestamp tempEnd = currentTime;
      Time_Sheet currentPrimaryTS = primaryTimesheets.get(j);
      // If the primary timesheet ended before the new current time, then remove it, add minutes and
      // check if any current timesheets are still active.
      if (currentPrimaryTS.getEndTime().getTime() <= currentTime.getTime()) {
        tempEnd = currentPrimaryTS.getEndTime();
        long millis =
            currentPrimaryTS.getEndTime().getTime()
                - currentPrimaryTS.getStartTimeForCurrentTimeAtAlarm().getTime();
        int minutes = (int) (millis / (60 * 1000));
        // split the time between all the primary time sheets
        for (Time_Sheet myTS : primaryTimesheets) {
          myTS.addMinute((minutes / primaryTimesheets.size()));
          myTS.setStartTimeForCurrentTimeAtAlarm(currentPrimaryTS.getEndTime());
        }
        boolean hasAddedNewTimeSheet = false;
        // If it's the last primary time sheet. Then move the last added current time sheet to
        // primary time sheets.
        if (primaryTimesheets.size() == 1 && !currentTimesheets.isEmpty()) {

          removeOldTimeSheets(currentTimesheets, currentPrimaryTS.getEndTime());

          primaryTimesheets.add(currentTimesheets.get(currentTimesheets.size() - 1));
          j++;
          primaryTimesheets.get(j).setStartTimeForCurrentTimeAtAlarm(currentPrimaryTS.getEndTime());
          currentTimesheets.remove(currentTimesheets.size() - 1);
          hasAddedNewTimeSheet = true;
        }
        // Remove the primary time sheet we have calculated time for
        if (hasAddedNewTimeSheet) {
          primaryTimesheets.remove(j - 1);
          j--;
        } else {
          primaryTimesheets.remove(j);
        }
        j--;

      } else { // if the primary time sheet ended after the current time, then add minutes and move
               // it to current time sheets

        long millis =
            tempEnd.getTime() - currentPrimaryTS.getStartTimeForCurrentTimeAtAlarm().getTime();
        int minutes = (int) (millis / (60 * 1000));

        currentPrimaryTS.addMinute(minutes);
        currentPrimaryTS.setStartTimeForCurrentTimeAtAlarm(currentTime);
        currentTimesheets.add(currentPrimaryTS);

        primaryTimesheets.remove(j);
        j--;
      }
    }
  }