from __future__ import print_function
import httplib2
import os
import sys
import dateutil.parser as dateparse

try:
    from apiclient import discovery
    import oauth2client
    from oauth2client import client
    from oauth2client import tools
except ImportError:
    print("""
Google API Modules are required:
pip install --upgrade google-api-python-client
Then visit the link below and follow the instructions to setup your API credentials:
https://developers.google.com/google-apps/calendar/quickstart/python
client_secret.json should be placed in ~/.hotbot
    """)

import datetime
import readline
import argparse
from jsonconf import jsonconf
import uuid
from random import randint
from croniter import croniter

import logging

log = logging.getLogger(name='__name__')
handler = logging.StreamHandler()
handler.setFormatter(logging.Formatter('%(levelname)s - %(message)s'))
log.addHandler(handler)
log.setLevel(logging.INFO)

# pip install --upgrade google-api-python-client
# https://console.developers.google.com/start/api?id=calendar
# https://console.developers.google.com/apis/

SCOPES = 'https://www.googleapis.com/auth/calendar'
CLIENT_SECRET_FILE = 'client_secret.json'
APPLICATION_NAME = 'HOTBot'


def get_short_unique():
    return str(uuid.uuid4())[:8]


def default_input(prompt, default=None):
    # https://chistera.yi.org/~dato/blog/entries/2008/02/14/python_raw_input_with_an_editable_default_value_using_readline.html
    if not default:
        default = ""

    def pre_input_hook():
        readline.insert_text(default)
        readline.redisplay()

    prompt += ": "
    readline.set_pre_input_hook(pre_input_hook)
    try:
        return raw_input(prompt)
    finally:
        readline.set_pre_input_hook(None)


def number_ordinal(n):
    # http://stackoverflow.com/questions/9647202/ordinal-numbers-replacement/20007730#20007730
    return "%d%s" % (n, "tsnrhtdd"[(n/10 % 10 != 1)*(n % 10 < 4) * n % 10::4])