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, Scraper

td = relativedelta.relativedelta(hours=5)
odt = datetime.now() + td

venue, created = Organization.objects.get_or_create(
     name="MN Launch",
     city="Minneapolis",
     website="https://mn.gov/launchmn/calendar",
    )

try:
    scraper, created = Scraper.objects.get_or_create(
        name=venue.name,
        website=venue.website,
        items = 0,
        last_ran = datetime.now(),
    )
except Exception as e:
    print(e)
    scraper = Scraper.objects.get(name=venue.name)
    print("Scraper: ", scraper)

event_type = "Ed"

cal_url = "https://timelyapp.time.ly/api/calendars/54705514/export?format=ics&target=copy&start_date=2024-12-13"
calendar_url = 'https://calendar.google.com/calendar/ical/uvkshlggh1h4ck08emab22btkum9hl94%40import.calendar.google.com/public/basic.ics'

objIcalData = requests.get(cal_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')
    if event['dateStamp'] is not None:
        event['dateStamp'] = event['dateStart'].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'],
        }
        cfpa_events.append(event)
        now_now = datetime.now().astimezone(tz)
        try:
            if event['dateStart'] > now_now:
                print(event['strSummary'])
                new_event, created = DSEvent.objects.update_or_create(
                    calendar = 'msp'
                    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
                )
                scraper.items+=1
                if event['strLocation'] != None and event['strLocation'] != 'MN' and event['strLocation'] != 'Online':
                    loc = event['strLocation'].split('@')
                    new_venue_name = loc[0]
                    if len(loc) > 1:
                        address = loc[1].split(",")
                        city = address[1].strip()
                        new_venue, created = Organization.objects.get_or_create(
                            name=new_venue_name,
                            city=city,
                            website="https://mn.gov/launchmn/calendar",
                        )
                        new_event.venue = new_venue
                        new_event.save()
                else:
                    new_event.venue = venue
                    new_event.save()
        except Exception as e:
             print(e)
             print("Event: ", event['dateStart'], event['strSummary'])
             print("Clock: ", now_now)                  
    else:
        print("Failed: ", component.get('DESCRIPTION'))
        
scraper.save()


