import os, sys
from datetime import datetime
from dateutil import relativedelta

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

from time import sleep
from pprint import pprint as ppr
import pytz

from events.models import Organization, Scraper
from events.digitools import getBrowser, createURL, createBasicEvent, getSource

count = 0

venue, created = Organization.objects.get_or_create(
     name="Minnestar",
     city="Minneapolis",
     website="https://minnestar.org/community/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 = ""

# Time Signatures
tz = pytz.timezone("US/Central")
DATETIME_FORMAT = '%B %d, %Y %I:%M %p'
DATETIME_FORMAT_2 = '%B %d %Y'

def get_events(ps, event_type):
    links = ps.xpath('.//*/div[@id="community-calendar-list-view-container"]/a/@href')
    ppr(links)
    for l in links:
        pse = getSource(br, l)
        sleep(1)
        event = {}
        event['calendar'] = venue.calendar
        event['link'] = l
        try:
            starttime = pse.xpath('.//*/time/text()')[0]
            endtime = pse.xpath('.//*/time/@datetime')[1]
            event['dateStamp'] = datetime.strptime(starttime, DATETIME_FORMAT) 
            event['title'] = pse.xpath('.//*/h1[@class="heading-2"]/text()')[0]
            # event['detail-headers'] = pse.xpath('.//*/ul[@class="eo-event-meta"]/li/strong/text()')
            # event['details'] = pse.xpath('.//*/ul[@class="eo-event-meta"]/li/text()')

        except:
            try:
                event['title'] = pse.xpath('.//*/h1[@class="heading-2"]/text()')[0]
                starttime = pse.xpath('.//*/time/text()')[0]
                event['dateStamp'] = datetime.strptime(starttime, DATETIME_FORMAT)
            except Exception as e:
                try:
                    print(e)
                    print('failed event: ', event)
                    starttime = pse.xpath('.//*/time/text()')[0]
                    event['dateStamp'] = datetime.strptime(starttime + ' 2025', DATETIME_FORMAT_2)
                except Exception as e:
                    print(e)
                    print("failed event: ", event)
        ppr(event)
        try:
            createBasicEvent(event, "Ot", venue)
            scraper.items+=1
        except Exception as e:
            print(e)
            print('failed to create: ', event)
        

if len(sys.argv) >= 2:
    arg1 = sys.argv[1]
    br = getBrowser(arg1)
else:
    print("No run_env")
    quit()

ps = getSource(br, venue.website)
get_events(ps, "Ot")
sleep(3)

scraper.save()

# Get Event Page Link(s)
# links = createURL("https://acmecomedycompany.com/the-club/calendar/")  

# for link in links:
#     ps = getSource(br, link)
#     get_events(ps, "Ed")
#     sleep(3)

br.close()