# 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

PAGE_SIZE = 15
# EVENTS
@cache.cached(key_prefix=make_cache_key)
def search_event(search, pageNumber):
    return api.search_event(search, pageNumber)

def update_event_info(event_id):
    if event_id:
        event_http = api.get_event_info(event_id)
        if event_http:
            event_db = db_event.get_event(event_id)
            if not event_db:
                event_tt = api.fetch_timetable(event_id)
                db_event.store_event(event_http, event_tt)
            elif utc.localize(event_db['modificationDate']) < dt_from_indico(event_http['modificationDate']):
                db_event.remove_event(event_id)
                event_tt = api.fetch_timetable(event_id)
                db_event.store_event(event_http, event_tt)


def _filter_events(user_id, now, events):
    for event in events:
        if utc.localize(now) - dt_from_indico(event['startDate']) < timedelta(days=30):
            db_event.store_cached_event(user_id, now, event)

def _filter_future_events(user_id, now, events):
    cached = False
    for event in events:

# 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


PAGE_SIZE = 15
# EVENTS
@cache.cached(key_prefix=make_cache_key)
def search_event(search, pageNumber):
    return api.search_event(search, pageNumber)

def update_event_info(event_id):
    if event_id:
        event_http = api.get_event_info(event_id)
        if event_http:
            event_db = db_event.get_event(event_id)
            if not event_db:
                event_tt = api.fetch_timetable(event_id)
                db_event.store_event(event_http, event_tt)
            elif utc.localize(event_db['modificationDate']) < dt_from_indico(event_http['modificationDate']):
                db_event.remove_event(event_id)
                event_tt = api.fetch_timetable(event_id)
                db_event.store_event(event_http, event_tt)


def _filter_events(user_id, now, events):
    for event in events:
        if utc.localize(now) - dt_from_indico(event['startDate']) < timedelta(days=30):
            db_event.store_cached_event(user_id, now, event)

def _filter_future_events(user_id, now, events):
    cached = False
    for event in events:

        if dt_from_indico(event['startDate']) - utc.localize(now) > timedelta(days=1) and not db_event.is_cached(user_id, event["id"]):