# Path: indicomobile/util/date_time.py
# def dt_from_indico(dt_dict):
#     dt = datetime.combine(datetime.strptime(dt_dict['date'], "%Y-%m-%d"),
#                           datetime.strptime(dt_dict['time'].split('.')[0], "%H:%M:%S").time())
#     return timezone(dt_dict['tz']).localize(dt)
# 
# Path: indicomobile/core/cache.py
# def setup_caching(app):
# def make_cache_key(*args, **kwargs):


from pytz import utc
from datetime import timedelta, datetime
from flask import session as flask_session, abort
from indicomobile.util.date_time import dt_from_indico
from indicomobile.core.cache import cache, make_cache_key
import urllib
import indicomobile.db.event as db_event
import indicomobile.db.session as db_session
import indicomobile.db.contribution as db_contribution
import indicomobile.core.indico_api as api
import indicomobile.core.favorites as favorites
def _get_future_events(user_id, now, last_start_date):
    days = 1
    date = (last_start_date + timedelta(days=days)).strftime("%Y-%m-%d")
    futureEvents, existMoreFutureEvents = api.get_future_events(user_id, date, date)
    while not _filter_future_events(user_id, now, futureEvents) and existMoreFutureEvents:
        days += 1
        date = (last_start_date + timedelta(days=days)).strftime("%Y-%m-%d")
        futureEvents, existMoreFutureEvents = api.get_future_events(user_id, date, date)
    return existMoreFutureEvents


def _get_cached_events(user_id, now, page):
    db_event.remove_cached_events(user_id, now - timedelta(hours=2))
    cached_events = list(db_event.get_cached_events(user_id, page, PAGE_SIZE))
    existMoreFutureEvents = True
    if not cached_events:
        last_event = db_event.get_last_event_cached(user_id)
        if not last_event:
            existMoreFutureEvents =_get_today_events(user_id, now)
            cached_events = list(db_event.get_cached_events(user_id, page,  PAGE_SIZE))
        else:
            existMoreFutureEvents =_get_future_events(user_id, now, last_event["event_start_date"])
        cached_events = list(db_event.get_cached_events(user_id, page,  PAGE_SIZE))
    while len(cached_events) < PAGE_SIZE and existMoreFutureEvents:
        last_event = db_event.get_last_event_cached(user_id)
        existMoreFutureEvents = _get_future_events(user_id, now, last_event["event_start_date"] if last_event else now)
        cached_events = list(db_event.get_cached_events(user_id, page,  PAGE_SIZE))
    return [event["event"] for event in cached_events]

def get_ongoing_events(page=1):

# Path: indicomobile/util/date_time.py
# def dt_from_indico(dt_dict):
#     dt = datetime.combine(datetime.strptime(dt_dict['date'], "%Y-%m-%d"),
#                           datetime.strptime(dt_dict['time'].split('.')[0], "%H:%M:%S").time())
#     return timezone(dt_dict['tz']).localize(dt)
# 
# Path: indicomobile/core/cache.py
# def setup_caching(app):
# def make_cache_key(*args, **kwargs):


# Path: indicomobile/core/event.py
from pytz import utc
from datetime import timedelta, datetime
from flask import session as flask_session, abort
from indicomobile.util.date_time import dt_from_indico
from indicomobile.core.cache import cache, make_cache_key
import urllib
import indicomobile.db.event as db_event
import indicomobile.db.session as db_session
import indicomobile.db.contribution as db_contribution
import indicomobile.core.indico_api as api
import indicomobile.core.favorites as favorites

def _get_future_events(user_id, now, last_start_date):
    days = 1
    date = (last_start_date + timedelta(days=days)).strftime("%Y-%m-%d")
    futureEvents, existMoreFutureEvents = api.get_future_events(user_id, date, date)
    while not _filter_future_events(user_id, now, futureEvents) and existMoreFutureEvents:
        days += 1
        date = (last_start_date + timedelta(days=days)).strftime("%Y-%m-%d")
        futureEvents, existMoreFutureEvents = api.get_future_events(user_id, date, date)
    return existMoreFutureEvents


def _get_cached_events(user_id, now, page):
    db_event.remove_cached_events(user_id, now - timedelta(hours=2))
    cached_events = list(db_event.get_cached_events(user_id, page, PAGE_SIZE))
    existMoreFutureEvents = True
    if not cached_events:
        last_event = db_event.get_last_event_cached(user_id)
        if not last_event:
            existMoreFutureEvents =_get_today_events(user_id, now)
            cached_events = list(db_event.get_cached_events(user_id, page,  PAGE_SIZE))
        else:
            existMoreFutureEvents =_get_future_events(user_id, now, last_event["event_start_date"])
        cached_events = list(db_event.get_cached_events(user_id, page,  PAGE_SIZE))
    while len(cached_events) < PAGE_SIZE and existMoreFutureEvents:
        last_event = db_event.get_last_event_cached(user_id)
        existMoreFutureEvents = _get_future_events(user_id, now, last_event["event_start_date"] if last_event else now)
        cached_events = list(db_event.get_cached_events(user_id, page,  PAGE_SIZE))
    return [event["event"] for event in cached_events]

def get_ongoing_events(page=1):

    user_id = unicode('all_public')