/* global gapi */

import moment from 'moment';

//  Actions
import CalendarActions from '../actions/CalendarActions';

//  Stores
import SettingsStore from '../stores/SettingsStore';

var utils;

class CalendarAPIUtils {

    constructor() {
        utils = this;

        // Your Client ID and API key can be retrieved from your project in the Google
        // Developer Console, https://console.developers.google.com
        this.client_id = '737977399720-tj5tl7jvqmt5jh3kp72j02i62uetlkkn.apps.googleusercontent.com';
        this.scopes = ["https://www.googleapis.com/auth/calendar.readonly"];
    }

    /* Call the google API to get the list of calendars */
    getCalendarList() {

        //  Create the request
        let request = gapi.client.calendar.calendarList.list();

        //  Execute the request and get the response
        request.execute(function (resp) {
            let cals = resp.items;
            if (cals.length > 0) {
                //  Call the action to receive the data:
                CalendarActions.recieveCalendarList(cals);
            } else {
                console.log("There was a problem getting the list of calendars");
            }
        });
    }

    /* Call the google API to get the list of events for a calendarid */
    getCalendarEvents(calendarId) {

        //  Set the min/max times for event display:
        let timeMin = (new Date()).toISOString();
        let tempTime = moment().endOf('day').toDate();
        let timeMax = tempTime.toISOString();

        //  Create the request
        let requestParams = {
            'calendarId': calendarId,
            /* Can be 'primary' or a given calendarid */
            'timeMin': timeMin,
            'timeMax': timeMax,
            'showDeleted': false,
            'singleEvents': true,
            'orderBy': 'startTime'
        };

        //  console.log("Requesting calendar events: ", requestParams);
        let request = gapi.client.calendar.events.list(requestParams);

        request.then(function (resp) {
            CalendarActions.recieveCalendarData(resp.result, calendarId);
        }, function (reason) {
            // Handle error
            /* If we get a strange response we attempt to re-authenticate */
            console.log("Got a strange response: ", reason, " - Trying to re-authenticate");
            utils.authorizeCalendar();
        });

    }

    /* Get authorization from the google API for the given scope(s) */
    authorizeCalendar() {
        try{
            gapi.auth.authorize({
                'client_id': this.client_id,
                'scope': this.scopes,
                'immediate': true
            }, utils.handleCalendarAuthResult);
        } catch(e){
            console.log("There was a problem attempting to authorize the Google calendar")
        }
    }

    /* Initiate auth flow in response to user clicking authorize button. */
    handleCalendarAuthClick(event) {
        gapi.auth.authorize({
            'client_id': this.client_id,
            'scope': this.scopes,
            'immediate': false
        }, utils.handleCalendarAuthResult);
        return false;
    }

    /* Handle the google API authorization response */
    handleCalendarAuthResult(authResult) {

        if (authResult && !authResult.error) {
            //  Indicate that the auth check is complete:
            CalendarActions.recieveCalendarAuthCheckResult(true);

            // Load the calendar API ...
            gapi.client.load('calendar', 'v3', function () {

                //  Then load the list of calendars
                utils.getCalendarList();

                //  Get the latest calendar information if we have a calendar selected:
                let settings = SettingsStore.getSettings();
                if (settings.calendarid !== "") {
                    utils.getCalendarEvents(settings.calendarid);
                }
            });

        } else {
            // We're not authorized yet.  We should
            // allow the user to authorize
            CalendarActions.recieveCalendarAuthCheckResult(false);
        }
    }
}

export default new CalendarAPIUtils();
@@ -2484,24 +2484,54 @@
 alendar() %7B%0A
+        let calutils = this;%0A%0A
         try%7B
@@ -2838,18 +2838,119 @@
 calendar
-%22)
+.  Reattempting in 10 seconds...%22);%0A            window.setTimeout(calutils.authorizeCalendar(), 10000);
 %0A       

Include retry logic for google calendar