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

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

venue, created = Organization.objects.get_or_create(
     name="location",
     city="Minneapolis",
     website="",
    )

event_type = ""

calendar_url = [
    'https://calendar.google.com/calendar/ical/js94epu90r2et31aopons1ifm8%40group.calendar.google.com/public/basic.ics',
    'https://calendar.google.com/calendar/ical/6rpooudjg01vc8bjek1snu2ro0%40group.calendar.google.com/public/basic.ics',
    'https://calendar.google.com/calendar/ical/teflgutelllvla7r6vfcmjdjjo%40group.calendar.google.com/public/basic.ics'
]

objIcalData = requests.get(calendar_url[1])

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')
    if event['dateStamp'] is not None:
        event['dateStamp'] = event['dateStamp'].dt
    if event['dateStart'] is not None:
        try:
            event['dateStart'] = event['dateStart'].dt.astimezone(pytz.utc)
        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'], 
             "DateTime" : event['dateStart'],
             "Location" : event['strLocation'],
        }
        cfpa_events.append(event)
        new_event = DSEvent.objects.update_or_create(
            event_type = event_type,
            show_title = event['strSummary'],
            show_link = event['link'],
            show_date = event['dateStart'],
            show_day = event['dateStart'].date(),
            more_details = event["details"],
            venue = venue
        )



# {'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'}


