'''
HTML utilities

Routines for generating HTML output

@author: <NAME> (retoomey)
'''

# System import
import os

# Imports from my library
import w2py.resource as w2res
import log

def genHTMLHeader(f, title):
    """ Generate a header for any HTML file we create """
    f.write("<!DOCTYPE html>\n")
    f.write("<!Generated by w2py library ")
    f.write("https://github.com/retoomey/wdss2python>\n")
    f.write("<!<NAME> <EMAIL>>\n")
    f.write("<html>\n")
    f.write("<head>\n")
    f.write("<meta charset=\"UTF-8\">\n")
    f.write("<title>{0}</title>\n".format(title))
    f.write("<style>\n")
    f.write("table, th, td {\n")
    f.write("   border: 1px solid black;\n")
    f.write("}\n")
    f.write("</style>\n")
    f.write("</head>\n")
    f.write("<body>\n")

def genHTMLFooter(f):
    """ Generate a footer for any HTML file we create. 
        Simple for the moment """
    f.write("</body>\n</html>\n")
    f.close()

def genHTMLLink(f, link, linkname):
    """ Generate a HREF link """
    f.write("<a href=\"{0}\">{1}</a>".format(link, linkname)) 

def genHTMLTableItem(f, rowlist):
    """ Generate table row given a list of strings """
    f.write("  <tr>\n") 
    for item in rowlist:
        f.write("    <td>{0}</td>\n".format(item))
    f.write("  </tr>\n")

def genHTMLTablePair(f, key, value):
    """ Generate a table row with two items, the second in bold """
    genHTMLTableItem(f, [key, "<b>{0}</b>".format(value)]) 
    
def genHTMLListItem(f, link, linkname):
    """ Generate one item in our table of contents HTML file """
    f.write("<li>")
    genHTMLLink(f, link, linkname)
    f.write("\n")

def genFileBase(f):
    """ Given a filename, generate a safe 'base' name for
        HTML and PNG filenames """
    baseName = w2res.getBaseMulti(f)
    baseName = "R"+w2res.removeGDBCharacters(baseName)
    return baseName

def genNavigation(f, indexLocation, percentWidth, prevFile="", nextFile=""):
    """ Generate navigation links in a table """
    # Navigation links in a table so they stay in consistent location
    # As user clicks on them
    f.write("<table style=\"width:{0}%\">\n".format(percentWidth))
    f.write("<tr>")
    
    # Return to table of contents if we have one
    if indexLocation:
        f.write("<td>")
        genHTMLLink(f, indexLocation, "Return to index")
        f.write("</td>")
    
    # Generate previous and next file links...
    if prevFile:
        f.write("<td>")
        genHTMLLink(f, prevFile+".html", "Previous: "+prevFile)
        f.write("</td>")
        
    if nextFile:
        f.write("<td>")
        genHTMLLink(f, nextFile+".html", "Next: "+nextFile)
        f.write("</td>")
    f.write("</tr>\n</table><br>")   
       
def genHTMLFile(D, outHTMLFolder, outputHTMLFile, inputPNGFile,
                indexLocation="", prevFile="", nextFile=""):  
    """ Given one of our DataType objects, image file and html location, 
        generate one HTML page """
    workhtml = w2res.getTempFile(outHTMLFolder, outputHTMLFile)
    percentWidth = 95
    
    # Guess we 'could' use so5mething like beautiful, but our
    # html should be pretty simple,
    # it's the actual gathering of stats, etc. that was the fun part, lol
    # We'll keep the tags separate for clarity for now
    f = open(workhtml, "w")
    
    timestamp = D.getTime()
    tName = D.getTypeName()
    fileName = D.getFileName()
    width = D.getImageWidth()
    height = D.getImageHeight()
    
    # Document header
    genHTMLHeader(f, "{0}-{1}".format(tName, timestamp))
    
    # Generate statistics table.  We can add more later, this is
    # just proof of concept
    f.write("<table>\n")
    genHTMLTablePair(f, "Datafile:", os.path.basename(fileName))
    genHTMLTablePair(f, "Type info:", tName)
    genHTMLTablePair(f, "Timestamp:", timestamp)
    iLink = "<a href=\"{0}\">{1}</a><br>\n".format(inputPNGFile, inputPNGFile)
    genHTMLTablePair(f, "Image:", iLink)
    genHTMLTablePair(f, "Cells:", "{0} w x {1} h".format(width, height))
    f.write("</table>\n")
    
    # Generate the navigation between pages
    genNavigation(f, indexLocation, percentWidth, prevFile, nextFile)

    # Insert the PNG file into the page, make it clickable if we have nextFile
    # This will let user go to next quickly 
    if nextFile:
        f.write("<a href=\"{0}\">".format(nextFile+".html"))
    image = "<img src=\"{0}\" width=\"{1}%\" style=\"border:1px solid black\">\n"
    f.write(image.format(inputPNGFile, percentWidth))
    if nextFile:
        f.write("</a>")
    
    # Another navigation for convenience at bottom of page    
    genNavigation(f, indexLocation, percentWidth, prevFile, nextFile)
  
    log.info("Wrote html page to "+outputHTMLFile)
    genHTMLFooter(f)