import urllib  # Adds Utility for url grabbing
import re  # adds Regular Expressions
import os  # adds OS functionality
import string  #  adds string functionality


##############################################################################
#################  Some quick checks for directories/files ###################                           
##############################################################################            
        
    
dirList = os.listdir("C:/")

if 'dsp' in dirList or 'DSP' in dirList:
    directory = "C:/DSP/scripts/globals/spells/bluemagic"
elif 'bluemagic' in dirList:
    directory = "C:/bluemagic"
else:
    os.mkdir("C:/bluemagic")
    directory = "C:/bluemagic"
    

check = 0
while check != 1:
    fileList = os.listdir(directory)
    if "names.txt" in fileList:
        print "The files created by this program will be placed in the directory " + directory + ".\n"
        start = raw_input("Please press enter to continue or hit \"CTRL + C\" to cancel.\n\n")
        check = 1
    else:
        start = raw_input("The names.txt file is missing from " + directory + " and therefore I have no names to use! Please add the file and then hit enter!\n\n")

## Get names of all moves to be built!
nameFile = open(directory + "/names.txt",mode="r")


## Grab html Page for the move currently working on. 
for line in nameFile:
    name = line
    webPage = urllib.urlopen("http://wiki.ffxiclopedia.org/wiki/" + name)
    rWebPage = webPage.read()

    mathPage = urllib.urlopen("http://wiki.ffxiclopedia.org/wiki/Calculating_Blue_Magic_Damage")
    mWebPage = mathPage.read()

    cleanName = name.strip()
    cleanName = cleanName.replace('_',' ')
    print "Script for " + cleanName + " is being made..."


## Drop page into a temporary file

    # General Info Page 
    wTempFile = open(directory + "/tempFile.txt","w")
    wTempFile.write(rWebPage)
    wTempFile.close()

    # Mathmatics Page
    mTempFile = open(directory + "/mathmatics.txt","w")
    mTempFile.write(mWebPage)
    mTempFile.close()

## Start Clean up
    rTempFile = open (directory + "/tempFile.txt","r")
    output = open(directory + "/"+str(name).strip()+".lua","w")
  
    
    output.write('---------------------------------------------'+"\n")
    output.write('--        '+str(cleanName)+"\n")
    output.write('--'+"\n")
########################################################################################################   
    noteCnt = 0
    for line in rTempFile:
        if "<dl><dd><b>" in line:                            ##### Cleans up the Description
            description = line.replace("<dl><dd><b>","")
            description = description.replace("</b>","")
            description = description.replace("<b>","")
            description = re.sub('<a href="/wiki/.*?>','',description)
            description = description.replace("</a>","")
            if "font" in description:
                description = description.replace("/font","")
                description = re.sub('<font color=\".*?\">','',description)
            description = re.sub("<.*?>","",description)
            output.write("--  Description: " + description.strip()+"\n")

       

################################################################################
########## Checks for changes based off TP         #############################
################################################################################
            tpLine = description.lower()  ### Defines the string we check for added TP benefits
            
            if "varies" in tpLine:        #### Checks to see if the move changes based off TP  
                
                if "accuracy" in tpLine:
                    tpBonus = "TPMOD_ACC"
                elif "critical" in tpLine:
                    tpBonus = "TPMOD_CRITICAL"           
                elif "attack" in tpLine:
                    tpBonus = "TPMOD_ATTACK"
                elif "damage" in tpLine:
                    tpBonus = "TPMOD_DAMAGE"
                else:    
                  tpBonus = "TPMOD_NONE"


            else:                          #### Sets the default to NO change based off TP
                 tpBonus = "TPMOD_NONE"
                 
################################################################################
########## Checks damage type. In example: Physical, Magical etc.###############
################################################################################
        if 'Notes' in line:
            notes = re.sub('<.*?>','',line)
            notes = notes.replace('<.*?>','')
            noteCnt = 1
            output.write('--  '+notes.strip()+":\n")
        elif noteCnt > 0:
            if "<li>" in line:
                notes = re.sub("<.*?>","",line)
                output.write('--    '+notes)
                noteCnt = noteCnt + 1
                if noteCnt > 6:
                    noteCnt = -1
                    output.write('--')
                 
        elif "<b>Spell Type" in line:
            moveType = line.replace("<b>","")
            moveType = moveType.replace("</b>","")
            moveType = moveType.replace("Spell","")
            moveType = re.sub("<.*?>","",moveType)
            moveType = re.sub("</a>\)<br />","",moveType)
            moveType = re.sub('\(<font color=.*?>','',moveType)
            moveType = moveType.replace("</font>)<br />","")
            output.write("--  "+ moveType.strip() + "\n")
            output.write("--  \n")
            #print "Type is: " + moveType

            moveType = moveType.lower()
            scriptType = 0 
            if "physical" in moveType:
                scriptType = 1
                if "slashing" in moveType:
                    physParam = "DMGTYPE_SLASH"

                elif "blunt" in moveType:
                    physParam = "DMGTYPE_BLUNT"

                elif "pierc" in moveType:
                    physParam = "DMGTYPE_PIERCE"

                else:
                    physParam = "DMGTYPE_H2H"
 
            elif "magical" in moveType:
                scriptType = 2
                

            

    
################################################################################
## Write the script                                                            #
################################################################################
            mathFile = open(directory + "/mathmatics.txt","r")
            prog = 0
            if scriptType == 1:
                
                for check in mathFile:
                    check = check.lower()
                    check = re.sub("<.*?>","",check.strip())

                    if cleanName.lower() in check:
                        prog = 1
                    elif prog == 1:
                        prog = 2
                        tp0 = check
                    elif prog == 2:
                        prog = 3
                        tp150 = check
                        if "-" in tp150:
                            tp150 = tp0
                    elif prog == 3:
                        prog = 4
                        tp300 = check
                        if "-" in tp300:
                            tp300 = tp0
                    elif prog == 4:
                        prog = 5
                        twoH = check
                        if "-" in twoH:
                            twoH = tp0
                    elif prog == 5 :
                        prog = 6
                        hits = check
                    elif prog == 6:
                        prog = 7
                        strn = 0.0
                        dex = 0.0
                        mnd = 0.0
                        vit = 0.0
                        agi = 0.0
                        intl = 0.0
                        chrm = 0.0
                        if " " in check:
                            pri = re.sub(" .*","",check)
                            sec = re.sub(".*? ","",check)
                        else:
                            pri = check
                            sec = 0
                        if "str" in pri:
                            strn = re.sub(".*?_","",pri)
                            strn = float(strn)/100
                        elif "dex" in pri:
                            dex = re.sub(".*?_","",pri)
                            dex = float(dex)/100
                        elif "vit" in pri:
                            vit = re.sub(".*?_","",pri)
                            vit = float(vit)/100
                        elif "agi" in pri:
                            agi = re.sub(".*?_","",pri)
                            agi = float(agi)/100
                        elif "intl" in pri:
                            intl = re.sub(".*?_","",pri)
                            intl = float(intl)/100
                        elif "mnd" in pri:
                            mnd = re.sub(".*?_","",pri)
                            mnd = float(mnd)/100
                        elif "chr" in pri:
                            chrm = re.sub(".*?_","",pri)
                            chrm = float(chrm)/100
                        if sec != 0:
                            if "str" in sec:
                                strn = re.sub(".*?_","",sec)
                                strn = float(strn)/100
                            elif "dex" in sec:
                                dex = re.sub(".*?_","",sec)
                                dex = float(dex)/100
                            elif "vit" in sec:
                                vit = re.sub(".*?_","",sec)
                                vit = float(vit)/100
                            elif "agi" in sec:
                                agi = re.sub(".*?_","",sec)
                                agi = float(agi)/100
                            elif "intl" in sec:
                                intl = re.sub(".*?_","",sec)
                                intl = float(intl)/100
                            elif "mnd" in sec:
                                mnd = re.sub(".*?_","",sec)
                                mnd = float(mnd)/100.0
                            elif "chr" in sec:
                                chrm = re.sub(".*?_","",sec)
                                chrm = float(chrm)/100
                            
                    elif prog > 6 and prog < 8:
                        prog = prog + 1
                    elif prog == 8:
                        prog = 9
                        dCap = check
                    elif prog == 9:
                        prog = 10
                    elif prog == 10:
                        prog = 11                    
                        sc = check
                        if "light" in sc:
                            sc = "SC_LIGHT"
                        elif "dark" in sc:
                            sc = "SC_DARK"                            
                        elif "gravitation" in sc:
                            sc = "SC_GRAVITATION" 
                        elif "distortion" in sc:
                            sc = "SC_DISTORTION" 
                        elif "fragmentation" in sc:
                            sc = "SC_FRAGMENTATION" 
                        elif "fusion" in sc:
                            sc = "SC_FUSION" 
                        elif "compression" in sc:
                            sc = "SC_COMPRESSION" 
                        elif "liquefaction" in sc:
                            sc = "SC_LIQUEFACTION" 
                        elif "inudration" in sc:
                            sc = "SC_INDURATION"                            
                        elif "scission" in sc:
                            sc = "SC_SCISSION" 
                        elif "reverberation" in sc:
                            sc = "SC_REVERBERATION" 
                        elif "detonation" in sc:
                            sc = "SC_DETONATION" 
                        elif "transfixion" in sc:
                            sc = "SC_TRANSFIXION" 
                        elif "impaction" in sc:
                            sc = "SC_IMPACTION"
                    elif prog == 11:
                        prog = 12
                        # Notes 
                    elif prog == 12:
                        prog = 0
                        


            
#########################################################
### Magical type script                   ###############
#########################################################
            elif scriptType == 2:

                for line in mathFile:
                    line = line.lower()
                    if name in line:
                        prog = 1
                    elif prog == 1:
                        prog = 2
                        element = re.sub("<.*?>","",line)
                    elif prog == 2:
                        prog = 3
                        modifier = re.sub("<.*?>","",line)
                    elif prog == 3:
                        prog = 4
                        multiplier = re.sub("<.*?>","",line)
                    elif prog == 4:
                        prog = 5
                        Tmultiplier = re.sub("<.*?>","",line)
                    elif prog <= 5 and prog > 8:
                        prog = prog + 1
                    elif prog == 8:
                        notes = re.sub("<.*?>","",line)

                
            mathFile.close()
################################################################################
########## Adds the required files for the script made           ###############
################################################################################
        if noteCnt == -1:
            output.write('---------------------------------------------'+"\n")
            output.write("require(\"/scripts/globals/magic\");\nrequire(\"/scripts/globals/status\");\nrequire(\"/scripts/globals/bluemagic\");\n")
            output.write('---------------------------------------------'+"\n")
            output.write("-- OnSpellCast\n")
            output.write('---------------------------------------------'+"\n\n")
            output.write('function onSpellCast(caster,target,spell)\n')
            output.write('    local params = {};\n')
            output.write('    params.tpmod = ' + tpBonus +'; params.dmgtype = ' + physParam + '; params.scattr = ' + sc + ';\n')
            output.write('    params.numhits = ' + str(hits) + ';\n')
            output.write('    params.multiplier = '+str(tp0)+'; params.tp150 = '+str(tp150)+'; params.tp300 = '+str(tp300)+'; params.azuretp = '+str(twoH)+'; params.duppercap = '+str(dCap)+';\n')
            output.write('    params.str_wsc = '+str(strn)+'; params.dex_wsc = '+str(dex)+'; params.vit_wsc = '+str(vit)+'; params.agi_wsc = '+str(agi)+'; params.int_wsc = '+str(intl)+'; params.mnd_wsc = '+str(mnd)+'; params.chr_wsc = '+str(chrm)+';\n')
            output.write('    damage = BluePhysicalSpell(caster, target, spell, params);\n')
            output.write('    damage = BlueFinalAdjustments(caster, target, spell, damage, params);\n')
            output.write('\n\n    return damage;\n')
            noteCnt = 0
    output.write('end;')
    output.close() ### Closes the script the file made
    print cleanName + " has been created!"        
    
    print "\n\n"
nameFile.close()
rTempFile.close()
os.remove(directory + "/tempFile.txt")
finish = raw_input("Please hit enter to close the program")

    


