package drive.api;

import com.google.api.services.drive.Drive;
import com.google.api.services.drive.model.Change;
import com.google.api.services.drive.model.ChangeList;
import com.google.inject.Inject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;
import java.util.List;
import java.util.ArrayList;

/**
 * ChangeService - Retrieve the list of changes
 *
 * David Maignan <davidmaignan@gmail.com>
 */
public class ChangeService {

    private static Logger logger = LoggerFactory.getLogger(ChangeService.class);

    private DriveService driveService;

    public ChangeService(){}

    @Inject
    public ChangeService(DriveService driveService){
        this.driveService = driveService;
    }

    /**
     * Retrieve a list of Change resources.
     *
     * @param startChangeId ID of the change to start retrieving subsequent changes from or {@code null}.
     *
     * @return List of Change resources.
     */
    public List<Change> getAll(Long startChangeId) throws IOException {
        List<Change> result = new ArrayList<Change>();
        Drive.Changes.List request = driveService.getDrive().changes().list();

        if (startChangeId != null) {
            request.setStartChangeId(startChangeId);
        }
        do {
            try {
                ChangeList changes = request.execute();

                result.addAll(changes.getItems());
                request.setPageToken(changes.getNextPageToken());
            } catch (IOException e) {
                System.out.println("An error occurred: " + e);
                request.setPageToken(null);
            }
        } while (request.getPageToken() != null &&
                request.getPageToken().length() > 0);

        return result;
    }

    public Change get(String changeId) {
        Change result = null;

        try {
            Change change = driveService.getDrive().changes().get(changeId).execute();

            result = change;
        } catch (IOException e) {
            logger.error(e.getMessage());
        }

        return result;
    }
}

@@ -159,24 +159,83 @@
 ChangeList;%0A
+import com.google.api.services.drive.model.StartPageToken;%0A
 import com.g
@@ -523,17 +523,16 @@
 rvice %7B%0A
-%0A
     priv
@@ -604,17 +604,16 @@
 class);%0A
-%0A
     priv
@@ -643,16 +643,42 @@
 ervice;%0A
+    private String fields;
 %0A    pub
@@ -803,24 +803,89 @@
 iveService;%0A
+        fields = %22changes,kind,newStartPageToken,nextPageToken%22;%0A
     %7D%0A%0A    /
@@ -957,24 +957,25 @@
 am start
-ChangeId
+PageToken
  ID of t
@@ -1136,18 +1136,20 @@
 All(
-Lo
+Stri
 ng start
 Chan
@@ -1144,24 +1144,25 @@
 ng start
-ChangeId
+PageToken
 ) throws
@@ -1224,19 +1224,14 @@
 ist%3C
-Change
 %3E();%0A
+%0A
     
@@ -1306,111 +1306,43 @@
 ist(
-);%0A%0A        if (startChangeId != null) %7B%0A            request.setStartChangeId(startChangeId);%0A        %7D
+startPageToken).setFields(fields);%0A
 %0A   
@@ -1417,33 +1417,32 @@
 uest.execute();%0A
-%0A
                 
@@ -1470,12 +1470,14 @@
 .get
-Item
+Change
 s())
@@ -1854,68 +1854,43 @@
 lic 
-Change
+StartPageToken
  get
-(
 St
-ring changeId) %7B%0A        Change result = null;%0A
+artPageToken()%7B
 %0A   
@@ -1916,23 +1916,14 @@
     
-Change change =
+return
  dri
@@ -1960,58 +1960,34 @@
 .get
-(changeId).execute();%0A%0A            result = change
+StartPageToken().execute()
 ;%0A  
@@ -2006,34 +2006,41 @@
 h (IOException e
+xception
 )
- 
 %7B%0A            lo
@@ -2054,22 +2054,43 @@
 ror(
-e.getMessage()
+%22Cannot retrieve getStartPageToken%22
 );%0A 
@@ -2106,32 +2106,30 @@
 %0A        return 
-result
+null
 ;%0A    %7D%0A%7D%0A

Update to V3