import sys
import csv
from collections import defaultdict

def loadData(csvfile):

    listRawData = list()
    if csvfile == '-':
        fh = sys.stdin
    else:
        fh = open(csvfile, 'r')

    try:
        csvreader = csv.reader(fh, delimiter=',')
        for nRow, row in enumerate(csvreader):
            if nRow == 0:
                header = row
            else:
                listRawData.append(row)
    except Exception, sErr:
        print sErr
        sys.exit(1)
    finally:
        fh.close()

    return header, listRawData

def getColumns(header):
    # CSV columns
    # HomeTeam = Home Team
    # AwayTeam = Away Team
    # FTR = Full Time Result (H=Home Win, D=Draw, A=Away Win)
    # Date = Date

    # B365H = Bet365 home win odds
    # B365D = Bet365 draw odds
    # B365A = Bet365 away win odds
    # BSH = Blue Square home win odds
    # BSD = Blue Square draw odds
    # BSA = Blue Square away win odds
    # BWH = Bet&Win home win odds
    # BWD = Bet&Win draw odds
    # BWA = Bet&Win away win odds
    # GBH = Gamebookers home win odds
    # GBD = Gamebookers draw odds
    # GBA = Gamebookers away win odds
    # IWH = Interwetten home win odds
    # IWD = Interwetten draw odds
    # IWA = Interwetten away win odds
    # LBH = Ladbrokes home win odds
    # LBD = Ladbrokes draw odds
    # LBA = Ladbrokes away win odds
    # PSH = Pinnacle Sports home win odds
    # PSD = Pinnacle Sports draw odds
    # PSA = Pinnacle Sports away win odds
    # SOH = Sporting Odds home win odds
    # SOD = Sporting Odds draw odds
    # SOA = Sporting Odds away win odds
    # SBH = Sportingbet home win odds
    # SBD = Sportingbet draw odds
    # SBA = Sportingbet away win odds
    # SJH = Stan James home win odds
    # SJD = Stan James draw odds
    # SJA = Stan James away win odds
    # SYH = Stanleybet home win odds
    # SYD = Stanleybet draw odds
    # SYA = Stanleybet away win odds
    # VCH = VC Bet home win odds
    # VCD = VC Bet draw odds
    # VCA = VC Bet away win odds
    # WHH = William Hill home win odds
    # WHD = William Hill draw odds
    # WHA = William Hill away win odds

    dictCol = dict()
    dictCol['nColHomeTeam'] = header.index("HomeTeam")
    dictCol['nColAwayTeam'] = header.index("AwayTeam")
#    dictCol['nColsOddsHome'] = [i for i in range(header.index("B365H"), range(header.index("B365H") + 3*13, 3)]
#    dictCol['nColsOddsAway'] = [i for i in range(header.index("B365A"), range(header.index("B365A") + 3*13, 3)]
#    dictCol['nColsOddsDraw'] = [i for i in range(header.index("B365D"), range(header.index("B365D") + 3*13, 3)]
    dictCol['nColResult'] = header.index("FTR")
    dictCol['date'] = header.index("Date")

    dictOddCol = dict()

    try:
        dictOddCol['Bet365'] = {'nColOddsHome' : header.index("B365H"),
                                'nColOddsAway' : header.index("B365A"),
                                'nColOddsDraw' : header.index("B365D"),}
    except ValueError:
        pass

    try:
        dictOddCol['Blue Square'] = {'nColOddsHome' : header.index("BSH"),
                                     'nColOddsAway' : header.index("BSA"),
                                     'nColOddsDraw' : header.index("BSD"),}
    except ValueError:
        pass