package dynamic.future.events;

import com.google.api.client.auth.oauth2.draft10.AccessTokenResponse;
import com.google.api.client.googleapis.auth.oauth2.draft10.GoogleAccessProtectedResource;
import com.google.api.client.googleapis.auth.oauth2.draft10.GoogleAccessTokenRequest;
import com.google.api.client.googleapis.auth.oauth2.draft10.GoogleAuthorizationRequestUrl;
import com.google.api.client.http.HttpTransport;
import com.google.api.client.http.javanet.NetHttpTransport;
import com.google.api.client.json.jackson.JacksonFactory;
import com.google.api.services.calendar.Calendar;
import com.google.api.services.calendar.model.Event;
import com.google.api.services.calendar.model.Events;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;

public class GoogleCalendar {

    static Calendar service = null;

    public static void login(String clientId, String clientSecret, String appName) throws IOException {

        HttpTransport httpTransport = new NetHttpTransport();
        JacksonFactory jsonFactory = new JacksonFactory();

        // Or your redirect URL for web based applications.
        String redirectUrl = "urn:ietf:wg:oauth:2.0:oob";
        String scope = "https://www.googleapis.com/auth/calendar";

        // Step 1: Authorize -->
        String authorizationUrl = new GoogleAuthorizationRequestUrl(clientId, redirectUrl, scope)
                .build();

        // Point or redirect your user to the authorizationUrl.
        System.out.println("Go to the following link in your browser:");
        System.out.println(authorizationUrl);

        // Read the authorization code from the standard input stream.
        BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
        System.out.println("What is the authorization code?");
        String code = in.readLine();
        // End of Step 1 <--

        // Step 2: Exchange -->
        AccessTokenResponse response = new GoogleAccessTokenRequest.GoogleAuthorizationCodeGrant(httpTransport, jsonFactory,
                clientId, clientSecret, code, redirectUrl).execute();
        // End of Step 2 <--

        GoogleAccessProtectedResource accessProtectedResource = new GoogleAccessProtectedResource(
                response.accessToken, httpTransport, jsonFactory, clientId, clientSecret,
                response.refreshToken);

        service = Calendar.builder(httpTransport, jsonFactory)
                .setApplicationName(appName)
                .setHttpRequestInitializer(accessProtectedResource)
                .build();

    }





    public static ArrayList<Event> allEvents(String calendarId){

        ArrayList<Event> all_events = new ArrayList<Event>();

        try {
            Events events = service.events().list(calendarId).execute();
            if(events != null && events.getItems() != null)
                all_events.addAll(events.getItems());

            while (true) {
                String pageToken = events.getNextPageToken();
                if (pageToken != null && !pageToken.isEmpty()) {
                    Events more_events = service.events().list(calendarId).setPageToken(pageToken).execute();
                    if(more_events != null && more_events.getItems() != null)
                        all_events.addAll(more_events.getItems());
                } else {
                    break;
                }
            }

        } catch (IOException e) {
            e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
        }

        return all_events;

    }

    public static void clearCalendar(String calendarId) {

        try {
            ArrayList<Event> events = allEvents(calendarId);
            for (Event event : events) {
                service.events().delete(calendarId, event.getId()).execute();
            }
        } catch (IOException e) {
            e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
        }

    }



    public static void listEvents(String calendarId){

        try {
            Events events = service.events().list(calendarId).execute();

            while (true) {
                for (Event e : events.getItems()) {
                    System.out.println(e.getSummary());
                }
                String pageToken = events.getNextPageToken();
                if (pageToken != null && !pageToken.isEmpty()) {
                    events = service.events().list(calendarId).setPageToken(pageToken).execute();
                } else {
                    break;
                }
            }

        } catch (IOException e) {
            e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
        }

    }

    public static Event AddEvent(String calendarId, Event e){

        try {
            Event createdEvent = service.events().insert(calendarId, e).execute();
            return createdEvent;
        } catch (IOException e1) {
            e1.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
        }
        return null;

    }



}



