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
from selenium import webdriver as wd
from selenium.webdriver.common.by import By

from xvfbwrapper import Xvfb
from lxml import html
import pytz

from events.models import Event as DSEvent, Organization
from digitools import getBrowser, createURL, createBasicEvent, getSource


exit()

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",
    )

tz = pytz.timezone("US/Central")

DATETIME_FORMAT = '%B %d %I:%M%p %Y'
DATETIME_FORMAT = '%B %A %d %I:%M-%I:%M%p'

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

try:
    br.find_element(By.XPATH, '//*[@class="privy-dismiss-content"]').click()
except Exception as e:
    print(e)
    pass

months = br.find_elements(By.XPATH, '//*[@class="sse-display"]')

for month in months:
    month_name = month.find_element(By.XPATH, './/*[@class="sse-size-28"]/u').text.capitalize()
    events = month.find_elements(By.XPATH, './/p')
    for event in events:
        e = {}
        eventTitle = event.text
        try:
            e['title'] = " ".join(eventTitle.split("-")[1].split(" ")[1:-2])
            if 'Music' in eventTitle:
                e['event_type'] = "Mu"
            elif 'The Growth Arc' in eventTitle:
                e['event_type'] = "Ot"
                e['dateTime'] = " ".join([month_name, date, "7:00pm", current_year])
                e['dateStamp'] = datetime.strptime(e['dateTime'], DATETIME_FORMAT)
                e['title'] = "The Growth Arc - Relationship Support Space"
                e['link'] = venue.website
            elif 'Event' in eventTitle:
                e['event_type'] = "Mu"
            else:
                e['event_type'] = "Ot"
            date = eventTitle.split(":")[0].split(" ")[1].replace("th", "").replace("nd", "").replace("rd", "").replace("st", "")
            time = eventTitle.split("-")[1].split(" ")[-2:][0]
            e['dateTime'] = " ".join([month_name, date, time, current_year])
            e['dateStamp'] = datetime.strptime(e['dateTime'], DATETIME_FORMAT)
            e['link'] = venue.website
            createBasicEvent(e, venue)
        except Exception as e:
            print("error ", eventTitle)
            print(e)

sleep(3)
# ppr(events)
br.close()