'''
Created on Jul 11, 2014

@author: Brett Paufler
Copyright Brett Paufler
'''

'''
STEP THREE
(which means two other things should have been done first)
    01 - base pages
    02 - scratchers pages
'''



import os
import re
import pickle

from lotto_class import prizeLevel
from lotto_class import lotteryGame


def getScratcherFilesList(sString="SGAME", directory="lottery",):
    '''
    returns a list of the scratcher game files from the lottery directory
    
    Need to download by running:
        1 - lotto_download_html
        2 - downloadSGame
        
    If no seed files in directory, this returns an empty set
    '''
    print "getScratcherFilesList starting"
    # prepare sString
    if not directory == "":
        directory = ".\\" + directory
    dirListing = os.listdir(directory)
    
    temp = []
    
    #qualify files
    for item in dirListing:
        if item.startswith(sString):
            temp.append(item)
            print item
            
    print "getScratcherFilesList ending\n\n" 
    return temp



def extractPrizeLevels(pL, tP):
    '''takes the list of prizes (pL)
        and returns a prizeList object instead
    '''
    print "extractPrizeLevels(pL) starting"
    pDL=[]
    
    for i in range(len(pL)/5):
        i = 5*i
        
        a = prizeLevel()        
        a.prize = int(pL[i])
        a.odds = int(pL[i+1])
        a.quantity = int(pL[i+2])
        a.claimed = int(pL[i+3])
        a.available = int(pL[i+4])       
        a.returnPerDollar = float(a.prize) / float(tP) / float(a.odds) 

        pDL.append(a)
        
    pDL = sorted(pDL, key=lambda k: k.prize, reverse=True)
    #print pDL
    print "extractPrizeLevels(pL) ending"
    return pDL


def findMaxPrizeStats(pL):
    '''given a list of prizeObjects
        return the max prize amount
    '''
    print "findMaxPrizeStats"
    mP = 0
    mPO = 0
    for p in pL:
        #print '\n\nfor p'
        #print p.fullPrintPrizes()
        #print p.prize
        if p.prize > mP:
            mP = p.prize
            mPO = p.odds
    return mP,mPO

 

def scratchersData():
    '''Goes through the contents of the passed file list
        and returns a composite list of all gameData
        This is what will be analyzed
        
    returns list of scratcherGameObjects
            gD = {'name':gN,
              'price':tP,
              'prizes':extractPrizeLevels(pL)
              }
    '''
    print "scratchersData() starting"
    data = []
    gameFiles=getScratcherFilesList()

    for sFile in gameFiles:
        filePath = ".\\lottery\\" + sFile
        
        page = open(filePath, 'r')
        
        gD = lotteryGame()
        gD.name = "s" + re.search(r'([0-9]{1,5})(?=.html)', sFile).group(0)
        gD.fileName = sFile
    
        for line in page:
            
            if "Ticket Price: $" in line:
                #print line
                tP = re.search(r'Ticket Price: ...', line).group(0)
                tP = tP[-2:]
                gD.price = int(tP)
                #print tP
                        
            if line.startswith("        <tr><td>"):
                #rawGameData              
                rawGD = line                
                rawGD = rawGD.replace(",","")
                rawGD = rawGD.replace("<tr>","")
                rawGD = rawGD.replace(r"</tr>","")
                rawGD = rawGD.replace("<td>","")
                rawGD = rawGD.replace(r"\n","")
                rawGD = rawGD.strip()
                rawGD = rawGD.replace(r"$","")
                rawGD = rawGD.replace(r"Ticket",tP)
                rawGD = rawGD[:-5]
                pL = rawGD.split(r'</td>')
        
        gD.prizes = extractPrizeLevels(pL, tP)
        
        #x, y = findMaxPrizeStats(gD.prizes)
        #gD.maxPrize = x
        #gD.maxPrizeStraightOdds = y      

        #print gD.printLotteryGameData()
        data.append(gD)
    
    
    data.sort(key=lambda x: int(x.name[1:]))
    
    print "\n\nscratchersData() ending\n\n"
    return data
                                
            

def extractDrawGamesData():
    '''
    '''
    print "\n\nextractDrawGamesData starting"
    
    with open(r'.\lottery\DRAWdrawGames.html', 'r') as drawFile:
        rS = drawFile.read()
    
    pV = re.findall('h2(.*?)h2', rS, re.DOTALL)
    pV = [p for p in pV if len(pV) < 40]
    pV = [p for p in pV if r"$" in p]
    pV = [p for p in pV if not r"<br>" in p]
    
    mL = []
    for p in pV:
        p = p[1:-2].strip()
        p = p.replace(" MILLION", "000000")
        p = p.replace(",", "")
        print p
        p = p.replace("$", "")
        mL.append(p)
    
    print mL
    pB = int(mL[0]) # powerBall
    mM = int(mL[1]) # megaMillion
    sL = int(mL[2]) # superLotto
    fF = int(mL[3]) # fantasyFive
    dF = int(mL[4]) # dailyFour
    dT = int(mL[5]) # dailyThree
    dD = int(mL[6]) # dailyDerby
    
    return (pB, mM, sL, fF, dF, dT, dD)
    
#print extractDrawGamesData()    

def secondParimutualPrize(fileName):
    '''finds the second place powerBall & megaMillions parimutual amount
    very brittle, not dialed in at all
    '''
    print "finding powerBallFive()"
    with open(fileName, 'r') as drawFile:
        rS = drawFile.read()

    rS = rS[rS.find("Detailed Draw"):]
    rS = rS[rS.find(r'5</b></td><td>'):1000]
 
    if rS[14] == "0":
        rS = rS[25:37]
        print rS
        rS = rS.replace(",","")
        rS = rS.replace("<","")
        rS = rS.replace(">","")
        rS = rS.replace("t","")
        rS = rS.replace("d","")
        rS = rS.replace(r"/","")
        rS = rS.replace("r","")
        print "Powerball Five Match %s" % rS
        return int(rS)
    else:
        print "Powerball Five previous Winner returning 200000"
        return 200000
    print rS
#powerBallFive()    

def initializeDrawGames():
    '''returns a list of gameObjects
    for the various lottery draw games

    Only the high prize is dynamic
    rest are hard set and mostly guesses
    
    '''
    print "initializeDrawGames() started"
    pBP, mMP, sLP, fFP, dFP, dTP, dDP = extractDrawGamesData()


    pB = lotteryGame()
    pB.name = "powerBall"
    pB.fileName = "DRAW-powerball.html"
    pB.price = 2
    pB.prizes.append(prizeLevel(prize=pBP, odds=175223510))
    second = secondParimutualPrize(r'.\lottery\DRAW-powerball.html')
    pB.prizes.append(prizeLevel(prize=second, odds=5153633))
    pB.prizes.append(prizeLevel(prize=10000, odds=648976))
    pB.prizes.append(prizeLevel(prize=75, odds=19088))
    pB.prizes.append(prizeLevel(prize=75, odds=12245))
    pB.prizes.append(prizeLevel(prize=6, odds=360))
    pB.prizes.append(prizeLevel(prize=6, odds=706))
    pB.prizes.append(prizeLevel(prize=3, odds=111))
    pB.prizes.append(prizeLevel(prize=3, odds=55))
    for p in pB.prizes:
        p.returnPerDollar = float(p.prize) / float(pB.price) / float(p.odds)


    mM = lotteryGame()
    mM.name = "megaMillions"
    mM.fileName = "DRAW-megaMillions.html"
    mM.price = 1
    mM.prizes.append(prizeLevel(prize=mMP, odds=258890850))
    second = secondParimutualPrize(r'.\lottery\DRAW-megaMillions.html')
    mM.prizes.append(prizeLevel(prize=second, odds=18492204))
    mM.prizes.append(prizeLevel(prize=6500, odds=739688))
    mM.prizes.append(prizeLevel(prize=500, odds=52835))
    mM.prizes.append(prizeLevel(prize=40, odds=10720))
    mM.prizes.append(prizeLevel(prize=7, odds=766))
    mM.prizes.append(prizeLevel(prize=5, odds=473))
    mM.prizes.append(prizeLevel(prize=2, odds=56))
    mM.prizes.append(prizeLevel(prize=1, odds=21))
    for p in mM.prizes:
        p.returnPerDollar = float(p.prize) / float(mM.price) / float(p.odds)     


    sL = lotteryGame()
    sL.name = "superLotto"
    sL.fileName = "DRAW-superLotto.html"
    sL.price = 1
    sL.prizes.append(prizeLevel(prize=sLP, odds=41416353))
    sL.prizes.append(prizeLevel(prize=20000, odds=1592937))
    sL.prizes.append(prizeLevel(prize=1000, odds=197221))
    sL.prizes.append(prizeLevel(prize=90, odds=7585))
    sL.prizes.append(prizeLevel(prize=50, odds=4810))
    sL.prizes.append(prizeLevel(prize=10, odds=185))
    sL.prizes.append(prizeLevel(prize=10, odds=361))
    sL.prizes.append(prizeLevel(prize=2, odds=74))
    sL.prizes.append(prizeLevel(prize=1, odds=49))
    for p in sL.prizes:
        p.returnPerDollar = float(p.prize) / float(sL.price) / float(p.odds)

    fF = lotteryGame()
    fF.name = "fantasy5"
    fF.fileName = "DRAW-fantasy5.html"
    fF.price = 1
    fF.prizes.append(prizeLevel(prize=fFP, odds=575757))
    fF.prizes.append(prizeLevel(prize=400, odds=3387))
    fF.prizes.append(prizeLevel(prize=15, odds=103))
    fF.prizes.append(prizeLevel(prize=1, odds=10))
    for p in fF.prizes:
        p.returnPerDollar = float(p.prize) / float(fF.price) / float(p.odds)


    dD = lotteryGame()
    dD.name = "dailyDerby"
    dD.fileName = "DRAW-dailyDerby.html"
    dD.price = 2
    dD.prizes.append(prizeLevel(prize=dDP, odds=1320000)) #tri & time
    dD.prizes.append(prizeLevel(prize=4500, odds=147000)) #exact & time
    dD.prizes.append(prizeLevel(prize=455, odds=13200)) #win & time
    dD.prizes.append(prizeLevel(prize=45, odds=1321)) #trifecta
    dD.prizes.append(prizeLevel(prize=24, odds=147)) #exacta
    dD.prizes.append(prizeLevel(prize=2, odds=13.2)) #win
    dD.prizes.append(prizeLevel(prize=47, odds=1001)) #time only
    for p in dD.prizes:
        p.returnPerDollar = float(p.prize) / float(dD.price) / float(p.odds)


    d3 = lotteryGame()
    d3.name = "daily3"
    d3.fileName = "DRAW-daily3.html"
    d3.price = 1
    d3.prizes.append(prizeLevel(prize=500, odds=1000))
    for p in d3.prizes:
        p.returnPerDollar = float(p.prize) / float(d3.price) / float(p.odds)

    d4 = lotteryGame()
    d4.name = "daily4"
    d4.fileName = "DRAW-daily4.html"
    d4.price = 1
    d4.prizes.append(prizeLevel(prize=5000, odds=10000))
    for p in d4.prizes:
        p.returnPerDollar = float(p.prize) / float(d4.price) / float(p.odds)


    print "initializeDrawGames() finished\n\n"
    rL = [pB, mM, sL, fF, dD, d3, d4]
    return rL


def initializeALLGames():
    '''return a list of all lottery game objects
    '''
    print "initializeALLGames() starting"
    gD = initializeDrawGames()
    dD = scratchersData()
    for d in dD:
        gD.append(d)
        
    with open(r'.\lottery\01-lottoPickleData.txt', 'wb') as f:
        pickle.dump(gD,f)
        
    with open(r'.\lottery\02-lottoEasyReadData.txt', 'wb') as f:
        for g in gD:
            print "writing to file %s" % g.name
            f.write("GAME: %s\n" % g.name)
            f.write(g.fileName)
            f.write("\nTicket Price: %d\n" % g.price)
            #f.write("maxPrizeRecursiveOdds %f\n" % g.maxPrizeRecursiveOdds)
            f.write("prizes\n")
            for p in g.prizes:
                f.write("P: %d, O: %d, Q: %d, C: %d, A: %d, RPD: %f\n" % (
                    p.prize, p.odds, p.quantity, p.claimed, p.available, p.returnPerDollar))
            f.write('\n\n')


    
    print "initializeALLGames() ending"
    return gD
    
if __name__ == '__main__':
    print "downloading all scratchers"            
    initializeALLGames()