import logging
import urllib

import pytz

from datetime import datetime, timedelta

from api.models import RoadClosure, RoadClosureStatus
from bs4 import BeautifulSoup
from dateutil import tz
from dateutil import parser

logger = logging.getLogger(__name__)


def parse_date(date_string):
    tzTex = pytz.timezone('US/Central')
    try:
        datetime = tzTex.localize(parser.parse(date_string))
    except Exception as e:
        datetime = tzTex.localize(parser.parse(date_string.split(', ', 1)[1]))
    print(datetime)
    return datetime


def get_road_closure():

    url = "http://www.cameroncounty.us/spacex/"
    user_agent = 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/35.0.1916.47 Safari/537.36'
    request = urllib.request.Request(url, headers={'User-Agent': user_agent})
    try:
        response = urllib.request.urlopen(request)
    except Exception as e:
        logger.error(e)
        return
    if response.code != 200:
        logger.error("Received bad response code %s" % response.code)
        return
    html_content = response.read()

    # Parse the html content
    soup = BeautifulSoup(html_content, "lxml")
    tableRows = []
    gdp_table = soup.find("table")
    gdp_table_data = gdp_table.tbody.find_all("tr")  # contains 2 rows
    for row in gdp_table_data:
        aux = ''
        for cell in row.find_all("td"):
            aux += str(cell.text.strip()) + '|'
        tableRows.append(aux[:-1])

    closures = []
    for row in tableRows:
        try:
            row = row.replace('a.m.', 'AM')
            row = row.replace('p.m.', 'PM')

            start_date = row.split('|')[1]
            closure_time = row.split('|')[2]
            start_time = closure_time.split(' to ')[0].strip()
            closure_end = closure_time.split(' to ')[1].strip()

            start_string = start_date + ' ' + start_time
            start_datetime = parse_date(start_string)

            now = datetime.now(tz=pytz.utc)

            if '–' in closure_end:
                end_date = closure_end.split('–')[0].strip()
                end_time = closure_end.split('–')[1].strip()
                end_date = datetime.strptime(end_date, '%B %d').replace(year=now.year).strftime('%A, %B %d, %Y')
                end_string = end_date + ' ' + end_time
            else:
                end_string = start_date + ' ' + closure_end
            end_datetime = parse_date(end_string)
            if end_datetime < start_datetime:
                end_datetime += timedelta(days=1)

            if end_datetime < now:
                continue

            name = row.split('|')[0]
            status = row.split('|')[3].replace('Closure', '').strip()
            closures.append([start_datetime, end_datetime, name, status])
        except Exception as e:
            logger.error(row)
            logger.error(e)

    logger.info("Found %s closures" % len(closures))
    for closure in closures:
        window_start = closure[0].astimezone(tz.tzutc())
        window_end = closure[1].astimezone(tz.tzutc())
        status_text = closure[3]
        status, created = RoadClosureStatus.objects.get_or_create(name=status_text)
        try:
            obj = RoadClosure.objects.get(window_start__exact=window_start,
                                          window_end__exact=window_end)
            obj.status = status
        except RoadClosure.DoesNotExist:
            obj = RoadClosure.objects.create(title=closure[2],
                                             window_start=window_start,
                                             window_end=window_end,
                                             status=status)
            logger.info("Creating new Road Closure %s" % obj)
        obj.save()

@@ -503,28 +503,8 @@
 %5D))%0A
-    print(datetime)%0A
     
@@ -1978,16 +1978,116 @@
 tz.utc)%0A
+            if 'of' in closure_end:%0A                closure_end = closure_end.split('of')%5B0%5D.strip()
 %0A       
@@ -2632,43 +2632,170 @@
 =1)%0A
-%0A            if end_datetime %3C now:
+            print(f%22start: %7Bstart_datetime%7D end: %7Bend_datetime%7D%22)%0A%0A            if end_datetime %3C now:%0A                print('skipping as end date is in the past')
 %0A   

Fix scraper again (#213)