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 events.models import Event as DSEvent, Organization
from digitools import getBrowser, createDashURL, createBasicEvent, getSource

venue, created = Organization.objects.get_or_create(
     name="Palmer's Bar",
     city="Minneapolis",
     website="https://palmers-bar.com",
    )

calendar_url = "https://palmers-bar.com"

DATETIME_FORMAT = '%d %m %Y %I:%M%p'

def get_events(ps, link):
    contents = ps.xpath('.//*/td')
    for c in contents:
        try:
            # day_num = c.xpath('.//*/div[@class="marker-daynum"]/text()')[0]
            events = c.xpath('.//*/li')
            # print(events)
            for e in events:
                event = {}
                event_link = calendar_url + e.xpath('.//a/@href')[0]
                ps = getSource(br, event_link)
                new_event = ps.xpath('.//*/h1[@class="eventitem-column-meta"]')
                event['title'] = new_event.xpath('.//*/h1[@class="event-title"]')
                event['date'] = new_event.xpath('.//*/time[@class="event-date"]')
                event['time'] = new_event.xpath('.//*/time[@class="event-time-12hr-start"]')
                event['link'] = event_link

                # event['month'] = link.split("month=")[1].split("-")[0]
                # event['year'] = link.split("month=")[1].split("-")[1]
                # event['title'] = e.xpath('.//h1/a[@class="flyoutitem-link"]/text()')
                # event['time'] = e.xpath('.//div[@class="flyoutitem-datetime flyoutitem-datetime--12hr"]/text()')
                # event['date'] = str(day_num) + ' ' + 'time'
                # event['dateStamp'] = datetime.strptime(dateStamp, DATETIME_FORMAT)
                ppr(event)
                if len(event['title']):
                    nevent = {}
                    nevent['title'] = "".join(event['title']).strip()
                    event['time'] = event['time'][0].strip().split(" –")[0]
                    nevent['link'] = "https://palmers-bar.com" + e.xpath('.//a/@href')[0]
                    event['dateStamp'] = str(day_num) + ' ' + event['month'] + ' ' + event['year'] + ' ' + event['time']
                    nevent['dateStamp'] = datetime.strptime(event['dateStamp'], DATETIME_FORMAT)
                    ppr(nevent)
                    # createBasicEvent(nevent, 'Mu', venue)
        except Exception as e:
            continue

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

links = createDashURL("https://palmers-bar.com/?view=calendar&month=")

for link in links:
    ps = getSource(br, link)
    get_events(ps, link)
    sleep(5)

# ppr(events)
br.close()