import os, sys
from datetime import datetime
from dateutil import relativedelta
from time import sleep
import pytz
from lxml import html

import django
sys.path.append('../')
os.environ['DJANGO_SETTINGS_MODULE'] = 'ds_events.settings'
django.setup()

from xvfbwrapper import Xvfb
from selenium import webdriver as wd

from events.models import Event as DSEvent, Organization

tz = pytz.timezone("US/Central")
td = relativedelta.relativedelta(months=1)
odt = datetime.now() + td


def getSource(browser, link):
    browser.get(link)
    sleep(3)
    ps = html.fromstring(browser.page_source)
    return ps

def getBrowser(run_env):
    if run_env == 'dev':
        print("Chrome is a go!")
        # chromeOptions = wd.ChromeOptions()
        # chromeOptions.binary_location = "/Application/Google\ Chrome.app"
        # chromeDriver = "/opt/homebrew/bin/chromedriver"
        # br = wd.Chrome(chromeDriver, options=chromeOptions)
        br = wd.Chrome()
        return br
    elif run_env == "def":
        print("Firefox go vroom")
        br = wd.Firefox()
        return br
    elif run_env == "prod":
        start_cmd = "Xvfb :91 && export DISPLAY=:91 &"
        xvfb = Xvfb()
        os.system(start_cmd)
        xvfb.start()
        print("started Xvfb")
        br = wd.Firefox()
        return br
    else:
        print("Failed", sys.argv, arg1)
        quit()

def createBasicURL(site_url):
    month = datetime.now().month
    next_month = odt.month
    year = datetime.now().year
    print(month, next_month, year)
    links = [
        site_url + str(month) + "/" + str(year),
        site_url + str(next_month) + "/" + str(year)
    ]
    print(links)
    return links

def createURLNoZero(site_url):
    month = datetime.now().month
    next_month = odt.month
    year = datetime.now().year
    links = [
        site_url + str(year) + "/" + month,
    ]
    if next_month == "1":
        links.append(site_url + str(int(year)+1) + "/" + next_month)
    else:
        links.append(site_url + str(year) + "/" + next_month)
    print(links)
    return links

def createURL(site_url):
    month = datetime.now().month
    if month < 10:
        month = "0" + str(month)
    else:
        month = str(month)
    next_month = odt.month
    if next_month < 10:
        next_month = "0" + str(next_month)
    else:
        next_month = str(next_month)
    year = datetime.now().year
    links = [
        site_url + str(year) + "/" + month,
    ]
    if next_month == "01":
        links.append(site_url + str(int(year)+1) + "/" + next_month)
    else:
        links.append(site_url + str(year) + "/" + next_month)
    print(links)
    return links

def createDashURL(site_url):
    month = datetime.now().month
    if month < 10:
        month = "0" + str(month)
    else:
        month = str(month)
    next_month = odt.month
    if next_month < 10:
        next_month = "0" + str(next_month)
    else:
        next_month = str(next_month)
    year = datetime.now().year
    print(month, next_month, year)
    links = [
        site_url + month + "-"  + str(year),
        site_url + next_month + "-"  + str(year) 
    ]
    print(links)
    return links

def createBasicEvent(event, event_type, venue):
    new_event, created = DSEvent.objects.update_or_create(
        calendar = event['calendar'],
        event_type = event_type,
        show_title = event['title'],
        show_link = event['link'],
        show_date = event['dateStamp'],
        show_day = event['dateStamp'],
        venue = venue
    )
    print("New Event: ", new_event)
