import sys, os

sys.path.append("/var/lib/django")

from django.core.management import setup_environ
from pbp_com import settings

setup_environ(settings)

import re
import urllib
import urllib2

import time
from datetime import datetime, timedelta
import dateutil

from almanac.models import Day, AstroChart, TideChart, TideDay, Tide
from directory.models import Place

year = "2011"
place = Place.objects.get(slug__exact="bar-harbor-public-wharf")


def get_table(year="", location=place.point.town.name):
    parser = re.compile("\d\d/\d\d/\d\d\d\d")

    location = location.replace(" ", "%20")
    url = (
        "http://tidesandcurrents.noaa.gov/get_predictions.shtml?year=%s&stn=3320+Bar%sHarbor"
        % (year, location)
    )
    post_data = {}

    data = urllib.urlencode(post_data)
    req = urllib2.Request(url, data)
    response = urllib2.urlopen(req)
    page = response.read()
    data = page.split("\n")

    table = []
    for line in data:
        if parser.match(line):
            table.append(line)

    return table


""" End of get_table() """


def fix_time(str, today, chart):
    """if chart.dst_start < today.date < chart.dst_end:
        delta=timedelta(hours=1)
    else:
        delta=timedelta(hours=0)"""
    return time.strftime(
        "%H:%M", datetime(*time.strptime(str, "%I:%M%p")[0:5]).timetuple()
    )


""" End of fix_time() """


"""### THE HEAVY LIFTING ### """
tides = get_table(year)

tide_table = []

try:
    chart = TideChart.objects.get(year__exact=year, place__exact=place)
except TideChart.DoesNotExist:
    raise Exception(
        "Currently you have to manually add a tide chart to hold this info before in can be imported."
    )

try:
    astro_chart = AstroChart.objects.get(year__exact=year)
except AstroChart.DoesNotExist:
    raise Exception(
        "You must create an astronomical chart for the year you wish to input tide data for."
    )

for entry in tides:
    row = entry.split(" ")
    row = [content for content in row if content != ""]
    try:
        day = Day.objects.get(date__exact=datetime.strptime(row[0], "%m/%d/%Y").date())
    except Day.DoesNotExist:
        raise Exception(
            "Tide data must be loaded after astronomical data for any given date."
        )

    tideday = TideDay.objects.create(day=day, tide_chart=chart)
    tideday.tide_1 = Tide.objects.create(
        time=fix_time(row[2], day, astro_chart), level=row[4], state=row[5]
    )
    tideday.tide_2 = Tide.objects.create(
        time=fix_time(row[6], day, astro_chart), level=row[8], state=row[9]
    )
    tideday.tide_3 = Tide.objects.create(
        time=fix_time(row[10], day, astro_chart), level=row[12], state=row[13]
    )
    try:
        tideday.tide_4 = Tide.objects.create(
            time=fix_time(row[14], day, astro_chart), level=row[16], state=row[17]
        )
    except:
        pass

    """ Load the data into the DB"""
    tideday.save()
