import datetime

from platypush.plugins import action
from platypush.plugins.google import GooglePlugin
from platypush.plugins.calendar import CalendarInterface


class GoogleCalendarPlugin(GooglePlugin, CalendarInterface):
    r"""
    Google Calendar plugin.

    In order to use this plugin:

        1. Create your Google application, if you don't have one already, on
           the `developers console <https://console.developers.google.com>`_.

        2. You may have to explicitly enable your user to use the app if the app
           is created in test mode. Go to "OAuth consent screen" and add your user's
           email address to the list of authorized users.

        3. Select the scopes that you want to enable for your application, depending
           on the integrations that you want to use.
           See https://developers.google.com/identity/protocols/oauth2/scopes
           for a list of the available scopes.

        4. Click on "Credentials", then "Create credentials" -> "OAuth client ID".

        5 Select "Desktop app", enter whichever name you like, and click "Create".

        6. Click on the "Download JSON" icon next to your newly created client ID.

        7. Generate a credentials file for the required scope:

            .. code-block:: bash

              mkdir -p <WORKDIR>/credentials/google
              python -m platypush.plugins.google.credentials \
                  'calendar.readonly' \
                  <WORKDIR>/credentials/google/client_secret.json

    """

    scopes = ['calendar.readonly']

    def __init__(self, *args, **kwargs):
        super().__init__(scopes=self.scopes, *args, **kwargs)

    @action
    def get_upcoming_events(self, max_results=10):
        """
        Get the upcoming events. See
        :meth:`platypush.plugins.calendar.CalendarPlugin.get_upcoming_events`.
        """

        now = datetime.datetime.utcnow().isoformat() + 'Z'
        service = self.get_service('calendar', 'v3')
        result = (
            service.events()
            .list(
                calendarId='primary',
                timeMin=now,
                maxResults=max_results,
                singleEvents=True,
                orderBy='startTime',
            )
            .execute()
        )

        events = result.get('items', [])
        return events


# vim:sw=4:ts=4:et:
