import time
from time import strftime
import operator

def constructAndCreateMailFile(websiteName, outputFileName, brandCounts, ProgramStartTime, brandsPassed, fileToWriteDirectory):
    ProgramEndTime = strftime("%Y%m%d- %H:%M:%S")
    message = "<p>Below is the detail of current run for "+ websiteName +" </p>" \
                              "<h3> File name with full path (in server): " + outputFileName + "</h3>" \
                              "<p>server: 192.168.1.249 </p>" \
                              "<p>input brand list :"+brandsPassed+"</p>"\
                              "<h2> total records fetched: " + str(sum(brandCounts.values())) + "</h2>"\
              "<p> program started at: " + ProgramStartTime+ "</p>" \
              "<p>program End time: " + ProgramEndTime + "</p>" \
              "<table><tr><th> Brand </th><th>Products</th></tr>"
    for k, v in sorted(brandCounts.items(), key=operator.itemgetter(1), reverse=True):
        message = message + "<tr><td>" + str(k) + "</td><td>" + str(v) + " </td></tr>"
    message = message + "</table><br/>"
    message = message + '<span>This mail is autogenerated from python program </span><p style="color:blue">thanks,<br/>Affine Scraping team</p>'
    html = "<html><head>" + '''
    <style type="text/css" media="screen">
        table{
            background-color: #AAD373;
            empty-cells:hide;
            border: blue 1px solid;
        }
        th {
        background-color: #4CAF50;
        color: white;
    }
        table, th, td {
        border: 1px solid blue;
        text-align:center;
    }
     span {color:red;}
      </style>''' + "</head><body>" + message + "</body></html>"
    f=open(fileToWriteDirectory+"/mailBody.html",'w')
    f.write(html)
    f.close()
    f=open(fileToWriteDirectory+"/mail.config",'w')
    f.write('[mailConfigs]\r\n')
    f.write('mailFile='+fileToWriteDirectory+"/mailBody.html\r\n")
    f.write('outputFile='+outputFileName+"\r\n")
    f.write('subject=scraping completed for '+websiteName+"\r\n")
    f.close()



if __name__ == '__main__':
    pass
