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

from datetime import datetime, timedelta
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, Calendar
import events.digitools as digitools
from dateutil import relativedelta
td = relativedelta.relativedelta(hours=5)


venue, created = Organization.objects.get_or_create(
     name="idioki",
     city="Medellin",
     website="https://idioki.com/",
    )
event_type = "Mu"

try:
    scraper, created = Scraper.objects.get_or_create(
        name="idioki",
        website="https://calendar.google.com/calendar/ical/46ae0446724b1b3ee83cbd7dbc0db6a235bf97509ad860ca91eada3c267b5e41%40group.calendar.google.com/public/basic.ics",
        calendar = Calendar.objects.get(shortcode='mde'),
        items = 0,
        new_items = 0,
        last_ran = datetime.now(),
        )
except Exception as e:
    print(e)
    scraper = Scraper.objects.get(name=venue.name)

item_count_start = scraper.items

objIcalData = requests.get(scraper.website)
gcal = iCalendar.from_ical(objIcalData.text)
tz = pytz.timezone("US/Central")

def createEvent(day, date, event, scraper, venue, event_type):
    days = [day-1, day+6, day+13]
    for day in days:
        event['dateStamp'] = date + timedelta(days=day)
        event['dateStart'] = event['dateStamp']
        digitools.createCleanIcalEvent(event, scraper, venue, event_type)
    return

def splitLocation(event):
     loc_split = event['strLocation'].split(',')
     venue_name = loc_split[0]
     venue, created = Organization.objects.get_or_create(
        name=venue_name,
        city="Medellin",
        )
     event['venue'] = venue
     return event

counter = 0

for component in gcal.walk():
    event = {}
    event['scraper'] = scraper
    event['calendar'] = scraper.calendar
    event['strSummary'] = f"{(component.get('SUMMARY'))}"
    event['strDesc'] = component.get('DESCRIPTION')
    event['strLocation'] = str(component.get('LOCATION'))
    # startDate = component.get('DTSTART')
    # startTime = startDate.time()
    event['dateStart'] = component.get('DTSTART')
    event['dateStamp'] = component.get('DTSTAMP')

    if event['strSummary'] != 'None':
        event['details'] = {
            "description" : event['strDesc'], 
            "Location" : event['strLocation'],
        }
    
    if event['dateStamp'] != None:
        event['dateStart'] = event['dateStart'].dt
        event['dateStart'] = datetime.strptime(str(event['dateStart'])[:-6], '%Y-%m-%d %H:%M:%S')
        rules = component.get('RRule')
        if rules['FREQ'][0] == 'WEEKLY':
            if datetime.today().weekday() != 0:
                event = splitLocation(event)
                date = datetime.today().date() - timedelta(days=datetime.today().weekday())
                date = datetime.combine(date, event['dateStart'].time())
                days = ["SU", "MO", "TU", "WE", "TH", "FR", "SA"]
                for day in rules['BYDAY']:
                    day = days.index(day)
                    createEvent(day, date, event, scraper, event['venue'], "Ed")

digitools.updateScraper(scraper, item_count_start)

new_time = datetime.now() - timedelta(days=1)
right_bound_time = datetime.now() + timedelta(days=45)
events = DSEvent.objects.filter(show_date__lte=new_time)
events1 = DSEvent.objects.filter(show_date__gte=right_bound_time)

for e in events:
    e.delete()

for e in events1:
    e.delete()