import datetime
import csv
import re

def CleanTime (Time):
    # Ensure the time string is of the form NN:NN.NN
    TimeList = re.findall('\d+', Time)
    TimeLen = len(TimeList)
    if TimeLen == 3:
        Time = TimeList[0] + ":" + TimeList[1] + "." + "{0:0<2}".format(TimeList[2])
    elif TimeLen == 2:
        Time = TimeList[0] + "." + "{0:0<2}".format(TimeList[1])
    elif TimeLen == 1:
        Time = TimeList[0] + ".00"
    else:
        # Yes - the spaces are necessary.
        # NT needs to be left justified,
        # while if we have a time it is
        # right justified. Who makes this
        # stuff up?
        Time = "NT      "
    return Time

def EmitC1 (TeamName):
    # TeamName[:6] is substitute for Team Code...and is presumed unique
    #
    print "{0:2}{1:1}{2:8}{3:6}{4:30}{5:16}{6:22}{7:22}{8:20}{9:2}{10:10}{11:3}{12:1}{13:6}{14:1}{15:10}\r".format(
    "C1","9", "", "MD"+TeamName[:4], TeamName, TeamName[:16], "", "", "", "MD", "", "USA", "1", "", "", "")

def EmitC2 (TeamName):
    print "{0:2}{1:1}{2:8}{3:6}{4:30}{5:12}{6:6}{7:6}{8:5}{9:6}{10:6}{11:16}{12:45}{13:1}{14:10}\r".format(
    "C2","9","", "MD" + TeamName[:4], TeamName, "", "", "", "", "", "", TeamName[:16], "", "", "")

def GenUSSNum (FirstName, LastName, Birthdate):
    if len(FirstName) < 3:
        FirstName = FirstName.ljust(3, '*')
    if len(LastName) < 4:
        LastName = LastName.ljust(4, '*')
    return Birthdate + FirstName[:3] + '*' + LastName[:4]

def EmitD0 (FirstName, LastName, School, Year, Gender, Event, Time):
    FullName = LastName + ", " + FirstName

    # Look-up table for Class
    Class = {'9':'FR','10':'SO', '11':'JR', '12':'SR'}
    Strokes = {'Freestyle': 1, 'Backstroke':2, 'Breaststroke':3, 'Butterfly':4, 'Individual':5}

    # Parse out the event
    EventList = Event.split()
    EventNum = EventList[0]
    EventDist = EventList[2]
    EventStroke = Strokes[EventList[4]]
    
    print "{0:2}{1:1}{2:8}{3:28}{4:12}{5:1}{6:3}{7:8}{8:2}{9:1}{10:1}{11:>4}{12:1}{13:>4}{14:4}{15:8}{16:>8}{17:1}{18:63}\r".format(
    "D0","9","", FullName[:28],"0101","A","USA","01011995",Class[Year],Gender[:1],Gender[:1], EventDist, EventStroke, EventNum, "UNOV", "04222012", CleanTime(Time), "Y", "")

def EmitD3 (FirstName, LastName):
    # Yup - another grand idea
    # This record has the USSNum as 14 Characters
    #
    USSNum = GenUSSNum(FirstName, LastName, "010195")
    print "{0:2}{1:14}{2:15}{3:129}\r".format("D3",USSNum,FirstName[:15],"")

def EmitSwimmer (FirstName, LastName, School, Year, Gender, Event1, Time1, Event2, Time2):
    # One per splash so we emit two D0 records and a D3.
    # We have to emit as D0, D3, D0 .... yeah it is stupid
    EmitD0(FirstName, LastName, School, Year, Gender, Event1, Time1)
    EmitD3(FirstName, LastName)
    EmitD0(FirstName, LastName, School, Year, Gender, Event2, Time2)
    
ifile  = open('Entries-sanitized.csv', "rU")
reader = csv.DictReader(ifile)
CreateDate = datetime.date.today()

# Emit A0 - file description
print "{0:2}{1:1}{2:8}{3:2}{4:30}{5:20}{6:10}{7:20}{8:12}{9:8}{10:42}{11:2}{12:3}\r".format(
    "A0", "9", "3.0", "01", "", "Python Importer", "v0.01", "warbaugh@gmail.com", "4105551212",
    CreateDate.strftime("%m%d%Y"),"", "MD", "")

# Emit B1 - Meet record
print "{0:2}{1:1}{2:8}{3:30}{4:22}{5:22}{6:20}{7:2}{8:10}{9:3}{10:1}{11:8}{12:8}{13:4}{14:8}{15:1}{16:10}\r".format(
    "B1","9","","Howard Cty Swimming Champs","","","Columbia","MD","21045","USA","1","04222012","04222012","", "", "Y","")
 
# Emit B2 - Meet host
print "{0:2}{1:1}{2:8}{3:30}{4:22}{5:22}{6:20}{7:2}{8:10}{9:3}{10:12}{11:28}\r".format(
    "B2","9","","Columbia Aquatics","","","Columbia","MD","","USA","","")

Schools = {}
Swimmers = {}

for row in reader:
    #
    # Columns are: Timestamp,Last Name,First Name,Email,Cell Phone,School,Grade,Available for Relays,Gender,MEvent 1,MTime 1,MEvent 2,MTime 2,WEvent 1,WTime 1,WEvent 2,WTime 2
    #
    # Check school name, and if it is new emit a Team id record (C1)
    # and team stat record (C2)
    #
    School = Schools.get(row['School'])
    if School != row['School']:
        # This is the first entry for School
        # emit C1 and C2 records
        School = row['School']
        Schools[School] = School
        EmitC1(School)
        EmitC2(School)

    # School C1/C2 records have been emitted already
    # so emit D3 and D0 records for individual
    Gender = row['Gender']
    if Gender == 'Male':
        Event1 = row['MEvent 1']
        Time1 = row['MTime 1']
        Event2 = row['MEvent 2']
        Time2 = row['MTime 2']
    else:
        Event1 = row['WEvent 1']
        Time1 = row['WTime 1']
        Event2 = row['WEvent 2']
        Time2 = row['WTime 2']
        
    EmitSwimmer(row['First Name'], row['Last Name'], row['School'], row['Grade'], row['Gender'], Event1, Time1, Event2, Time2)

    

# Emit Z0 record
print "{0:2}{1:1}{2:8}{3:2}{4:147}\r".format("Z0","9","","01","")

    
ifile.close()
