import requests, os, sys
from icalendar import Calendar as iCalendar, Event

from datetime import datetime
from dateutil import relativedelta
td = relativedelta.relativedelta(hours=5)

from pprint import pprint as ppr
import pytz

import django
sys.path.append('../')
os.environ['DJANGO_SETTINGS_MODULE'] = 'ds_events.settings'
django.setup()

from events.models import Event as DSEvent, Organization
from dateutil import relativedelta


def createEvent(event, Organization, event_type):
    new_event, created = DSEvent.objects.update_or_create(
                        event_type = event_type,
                        show_title = event['strSummary'],
                        show_link = venue.website,
                        show_date = event['dateStart']-td,
                        show_day = event['dateStart']-td,
                        more_details = event["details"],
                        venue = venue
                    )
    return new_event, created


def createVenue(event):
    venue, created = Organization.objects.get_or_create(
        name = event['venue'],
        address = event['address'],
        city = event['city'],
    )
    return venue, created

td = relativedelta.relativedelta(hours=5)


event_type = "Ot"

calendar_url = 'https://calendar.google.com/calendar/ical/c_g1i6cbb3glhu6or0hu8kemah7k%40group.calendar.google.com/public/basic.ics'

objIcalData = requests.get(calendar_url)

gcal = iCalendar.from_ical(objIcalData.text)

cfpa_events = []
tz = pytz.timezone("US/Central")

for component in gcal.walk():
    event = {}
    event['strSummary'] = f"{(component.get('SUMMARY'))}"
    event['strDesc'] = component.get('DESCRIPTION')
    event['strLocation'] = component.get('LOCATION')
    event['dateStart'] = component.get('DTSTART')
    event['dateStamp'] = component.get('DTSTAMP')
    event['RepeatRule'] = component.get('RRULE')
    if event['dateStamp'] is not None:
        event['dateStamp'] = event['dateStamp'].dt
    if event['dateStart'] is not None:
        try:
            event['dateStart'] = event['dateStart'].dt
        except Exception as e:
             event['dateStart'] = event['dateStart'].dt
    event['dateEnd'] = (component.get('DTEND'))
    if event['dateEnd'] is not None:
            event['dateEnd'] = event['dateEnd'].dt
    else:
        event['dateEnd'] = event['dateStart']
    if event['strSummary'] != 'None':
        event['details'] = {
             "description" : event['strDesc'], 
             "Location" : event['strLocation'],
        }
        try:
            event['venue'] = event['strLocation'].split(",")[0].strip()
            event['address'] = event['strLocation'].split(",")[1].strip()
            event['city'] = event['strLocation'].split(",")[2].strip()
            try:
                event['state'] = event['strLocation'].split(",")[3].split(' ')[1]
                event['zip'] = event['strLocation'].split(",")[3].split(' ')[2]
            except Exception as error:
                pass
        except Exception as error:
            pass
        cfpa_events.append(event)
        # print(event)
        now_now = datetime.now().astimezone(pytz.utc)
        try:
            if event['dateStart'] > now_now:
                if not event['address'][0].isdigit():
                    continue
                venue, created = createVenue(event)
                new_event, created = createEvent(event, venue, event_type)
                print(new_event)  
        except Exception as e:
            try:
                if event['dateStart'] > now_now.date():
                    if not event['address'][0].isdigit():
                        continue
                    venue, created = createVenue(event)
                    new_event, created = createEvent(event, venue, event_type)
                    print(new_event) 
            except Exception as e:
                ppr(event)
                print(e)


# new_events = []
# for event in cfpa_events:
#     now_now = datetime.now().astimezone(pytz.utc)
#     try:
#         if event['dateStart'] > now_now:
#             new_events.append(event)
#     except Exception as e:
#         try:
#             if event['dateStart'] > now_now.date():
#                 new_events.append(event)
#         except Exception as e:
#             print(e)
#             ppr(event)


# {'dateEnd': datetime.datetime(2022, 10, 22, 18, 30, tzinfo=<UTC>),
#  'dateStamp': datetime.datetime(2023, 3, 23, 1, 57, 45, tzinfo=<UTC>),
#  'dateStart': datetime.datetime(2022, 10, 22, 17, 30, tzinfo=<UTC>),
#  'details': {'DateTime': datetime.datetime(2022, 10, 22, 17, 30, tzinfo=<UTC>),
#              'Location': vText('b'''),
#              'description': None},
#  'strDesc': None,
#  'strLocation': vText('b'''),
#  'strSummary': 'Nia Class with Beth Giles'}


