import csv
import PyPDF2
import re
import datetime
from datetime import date, timedelta

from fitness import Fitness
from dbConnection import DB
import toolBox

def eval(individual):
    ''' Evaluates best ind timetable'''

    with open('timetable.csv', 'w') as csvfile1:
        writer = csv.writer(csvfile1)
        writer.writerow(['Line', 'Capacity', 'Headway', 'Departure time'])
        for trip, item in enumerate(individual):
            if trip % 7 == 0:
                [writer.writerow(individual[trip+i]) for i in range(7)]
                writer.writerow([])


def evaluateTimetable():
    ''' Evaluates how well a current static timetable does in terms of waiting time. The purpose is to give some kind of
    comparison metric with the timetable as generated by GA.
    '''

    pdfFileObj = open('H1_web.pdf', 'rb')
    pdfReader = PyPDF2.PdfFileReader(pdfFileObj)
    numPages = pdfReader.numPages
    pagesText = []

    for i in range(2):
        pdfReader = PyPDF2.PdfFileReader(pdfFileObj)
        pageObj = pdfReader.getPage(i)
        txt = pageObj.extractText()
        timeRegex = re.compile(r'\d{2}\.\d{2}')   # escape dot to match
        mo = timeRegex.findall(txt)
        pagesText += mo

    departures = list()
    for index in range(len(pagesText)):
        if index % 4 == 0:
            for i in range(4):
                departures.append(pagesText[index+i])

    departures[:] = ['2015 12 09 ' + x for x in departures]
    length = len(departures)
    items = []
    for i in range(8):
        item = departures.pop()
        items.append(item) 
    items.reverse()
    #departures[:] = items + departures

    individual = list()
    for t in departures:
        individual.append([1, 120, 1, datetime.datetime.strptime(t, '%Y %m %d %H.%M')]) 

    phenotype = []
    ind = []
    for q in range(len(individual)):
        try:
            if q % 4 == 0:
                ind.append(individual[q])
                t = []
                for x in range(4):
                    t.append(individual[q+x])
                #phenotype.append(t)
        except IndexError, e:
            t[:] = []
            for x in range(4):
                t.append(individual[x+0])
            #phenotype.append(t)

    print ind


if __name__ == "__main__":
    evaluateTimetable()
