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="Acadia Cafe",
     city="Minneapolis",
     website="https://acadiacafe.com",
    )

calendar_url = "https://www.acadiacafe.com/events"

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

def get_events(ps, link):
    contents = ps.xpath('.//*/div[@class="cl-view-month__day__event__title"]')
    print(contents)
    quit()

    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['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['link'] = e.xpath('.//a/@href')[0]
                event['date'] = str(day_num) + ' ' + 'time'
                # event['dateStamp'] = datetime.strptime(dateStamp, DATETIME_FORMAT)
                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)
                    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()




ps = getSource(br, calendar_url)
sleep(5)
get_events(ps, calendar_url)
sleep(5)

# ppr(events)
br.close()