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


current_year = str(datetime.now().year)

venue, created = Organization.objects.get_or_create(
     name="Piller Forum",
     city="Minneapolis",
     website="https://www.pilllar.com/pages/events",
     is_venue = True
    )

scraper,item_count_start = digitools.getScraper(venue)

event_type = "Mu"

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

def get_events(ps, event_type):
    contents = ps.xpath('.//*/div[@class="sse-column sse-half sse-center"]')
    for c in contents:
        try:
            event = {}
            event['scraper'] = scraper
            event['calendar'] = scraper.calendar
            event['link'] = venue.website
            # time = c.xpath('.//*/span[@class="vp-time"]/text()')[0].strip()
            date = c.xpath('.//h1[@class="sse-size-64"]/text()')[0]
            if len(date) > 1:
                print(date)
                year = datetime.now().year
                event_date = date + " " + str(year)
                event['title'] = c.xpath('.//p/span/b/text()')[0]
                details = c.xpath('.//p/text()')
                if 'Music' in details[-1]:
                    event_time = c.xpath('.//p/text()')[-1].split("Music")[1].strip()
                    event_type = "Mu"
                    event_dt = event_date + " " + event_time + " PM"
                    event['dateStamp'] = datetime.strptime(event_dt, DATETIME_FORMAT_night)
                elif len(details) == 1:
                    try:
                        event_time = details[0].split("-")[0].strip()
                        event_dt = event_date + " " + event_time + ' PM'
                        event['dateStamp'] = datetime.strptime(event_dt, DATETIME_FORMAT_night)
                        event_type = "Ot"
                    except Exception as e:
                        event_time = details[0].split("-")[0].strip()
                        event_dt = event_date + " " + event_time
                        event['dateStamp'] = datetime.strptime(event_dt, DATETIME_FORMAT_2)
                        event_type = "Ot"
            digitools.createBasicEvent(event, event_type, venue)
        except Exception as e:
            print(e)

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, event_type)
sleep(3)

br.close()

digitools.updateScraper(scraper, item_count_start)
