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
import events.digitools as digitools

count = 0
tz = pytz.timezone("US/Central")
DATETIME_FORMAT = '%a, %b %d %Y %I:%M %p SHOW'
DATETIME_FORMAT_2 = '%a, %b %d %Y %I:%M %p SHOW'

venue, created = Organization.objects.get_or_create(
     name="Icehouse",
     city="Minneapolis",
     website = "https://icehouse.turntabletickets.com",
     is_venue = True
    )

scraper,item_count_start = digitools.getScraper(venue)

def get_events(ps, event_type):
    contents = ps.xpath('.//*/div[@class="performances whitespace-pre-line w-full md:w-3/4"]')
    for c in contents:
        try:
            event = {}
            event['scraper'] = scraper
            event['calendar'] = scraper.calendar
            event['title'] = c.xpath('.//*/h3[@class="text-3xl font-semibold font-heading mr-auto"]/text()')[0]
            event['link'] = venue.website + c.xpath('.//*/a[@class="show-link"]/@href')[0]
            event['date'] = c.xpath('.//*/h4[@class="day-of-week"]/text()')[0]
            year = datetime.now().year
            if "Brunch" in event['title']:
                event['time'] = "11:00 AM SHOW"
            else:
                event['time'] = c.xpath('.//*/div[@class="performance-btn"]/button/text()')[0]

            event['datetime'] = event['date'] + " " + str(year) + " " + event['time']
            try:
                event['dateStamp'] =datetime.strptime(event['datetime'], DATETIME_FORMAT)
            except:
                event['datetime'] = event['date'] + " " + str(year) + " " + "07:00 PM SHOW"
                event['dateStamp'] =datetime.strptime(event['datetime'], DATETIME_FORMAT)
                event['title'] = event['title'] + " (Time Estimated)"
            try:
                digitools.createBasicEvent(event, event_type, venue)
                scraper.items+=1
            except Exception as e:
                print(e)
                quit()
                
        except Exception as e:
            ppr(event)
            print(e)
            quit()

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

ps = digitools.getSource(br, venue.website)
get_events(ps, "Mu")

# ppr(events)
br.close()
digitools.updateScraper(scraper, item_count_start)
