#!/usr/bin/python
#
# Copyright 2012 Google Inc. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#    http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import httplib2
import sys
import os
import string
import time
import datetime

from apiclient.discovery import build
from oauth2client.file import Storage
from oauth2client.client import AccessTokenRefreshError
from oauth2client.client import OAuth2WebServerFlow
from oauth2client.tools import run

# For this example, the client id and client secret are command-line arguments.
client_id = sys.argv[1]
client_secret = sys.argv[2]
client_string = sys.argv[3]
client_longString = sys.argv[4]
client_startString = sys.argv[5]
if len(sys.argv) > 6:
  client_endString = sys.argv[6]
  ev_type = 1
else:
  ev_type = 2



# The scope URL for read/write access to a user's calendar data
scope = 'https://www.googleapis.com/auth/calendar'

# Create a flow object. This object holds the client_id, client_secret, and
# scope. It assists with OAuth 2.0 steps to get user authorization and
# credentials.
flow = OAuth2WebServerFlow(client_id, client_secret, scope)

def main():

  # Create a Storage object. This object holds the credentials that your
  # application needs to authorize access to the user's data. The name of the
  # credentials file is provided. If the file does not exist, it is
  # created. This object can only hold credentials for a single user, so
  # as-written, this script can only handle a single user.
  storage = Storage('credentials.dat')

  # The get() function returns the credentials for the Storage object. If no
  # credentials were found, None is returned.
  credentials = storage.get()

  # If no credentials are found or the credentials are invalid due to
  # expiration, new credentials need to be obtained from the authorization
  # server. The oauth2client.tools.run() function attempts to open an
  # authorization server page in your default web browser. The server
  # asks the user to grant your application access to the user's data.
  # If the user grants access, the run() function returns new credentials.
  # The new credentials are also stored in the supplied Storage object,
  # which updates the credentials.dat file.
  if credentials is None or credentials.invalid:
    credentials = run(flow, storage)

  # Create an httplib2.Http object to handle our HTTP requests, and authorize it
  # using the credentials.authorize() function.
  http = httplib2.Http()
  http = credentials.authorize(http)

  # The apiclient.discovery.build() function returns an instance of an API service
  # object can be used to make API calls. The object is constructed with
  # methods specific to the calendar API. The arguments provided are:
  #   name of the API ('calendar')
  #   version of the API you are using ('v3')
  #   authorized httplib2.Http() object that can be used for API calls
  service = build('calendar', 'v3', http=http)

  try:
    if ev_type == 1:
      event = {
        'summary': client_string,
        'description': client_longString,
        'start': {
          'dateTime': client_startString,
          'timeZone': 'America/New_York'
          },
          'end': {
            'dateTime':client_endString,
            'timeZone': 'America/New_York'
          },
      }
    else:
      year = string.atoi(client_startString.split("-")[0])
      month = string.atoi(client_startString.split("-")[1])
      day = string.atoi(client_startString.split("-")[2])
      start_time = datetime.date(year, month, day)
      one_day = datetime.timedelta(days=1)
      end_time = start_time + one_day
      end_time_str = end_time.strftime("%Y-%m-%d")

      event = {
        'summary': client_string,
        'description': client_longString,
        'start': {
          'date': client_startString,
          },
          'end': {
            'date': end_time_str,
          },
      }

    # request = service.events().quickAdd(calendarId='1ocenl402qcp8eg74ddv44uar4@group.calendar.google.com', text=client_string)
    request = service.events().insert(calendarId='1ocenl402qcp8eg74ddv44uar4@group.calendar.google.com', body=event)
    response = request.execute()


  except AccessTokenRefreshError:
    # The AccessTokenRefreshError exception is raised if the credentials
    # have been revoked by the user or they have expired.
    print ('The credentials have been revoked or expired, please re-run'
           'the application to re-authorize')
  # Remove credentials.dat because it can only handly credentials from one account and fails miserably if you try to 
  # use a different account.
  os.remove('credentials.dat')

if __name__ == '__main__':
  main()