import datetime, os, shutil
from importlib import import_module

import headerfooter
import makeheader

"""
Code to generate standalone fic pages
"""

def singlepage(ficno,directory,local=False):
    # convert to three-digit number
    if ficno < 10:
        ficnostring = "00" + str(ficno)
    elif ficno < 100:
        ficnostring = "0" + str(ficno)
    else:
        ficnostring = str(ficno)
    # open the file
    ficfile = directory + "." + ficnostring
    fileread = import_module(ficfile)
    # determine if unlocked
    try:
        if fileread.locked:
            singlestatus = False
        else:
            singlestatus = True
    except:
        singlestatus = True
    singlepath = "build/single/" + ficnostring
    if not os.path.isdir(singlepath):
        os.mkdir(singlepath)
    if os.path.exists(singlepath + "/index.html"):
        os.remove(singlepath + "/index.html")
    # write to output file
    output = singlepath + "/index.html"
    headerfooter.headerwrite(output,"Fic no. " + ficnostring,"Fic no. <span id=\"ficno\">" + ficnostring + "</span>","",False,local)
    try:
        if fileread.original:
            makeheader.ficgen(fileread.original,False,output,local)
        else:
            makeheader.ficgen(ficno,False,output,local)
    except:
        makeheader.ficgen(ficno,False,output,local)
    if singlestatus:
        filewrite = open(output, "a")
        filewrite.write("<iframe src=\"")
        if local:
            filewrite.write("/home/mdd/Documents/proj/fic-archive/build/files/" + ficnostring + ".html")
        else:
            filewrite.write("/fic/files/" + ficnostring + ".html")
        filewrite.write("\"></iframe>\n<div class=\"fic\">\n")
        filewrite.close()
        try:
            if fileread.original:
                makeheader.linkgen(fileread.original,output,local)
            else:
                makeheader.linkgen(ficno,output,local)
        except:
            makeheader.linkgen(ficno,output,local)
        filewrite = open(output, "a")
        filewrite.write("</div>\n")
        filewrite.close()
    headerfooter.footerwrite(output,False,local)

"""
Generate all single pages
"""

def allsingles(local=False):
    for filename in os.listdir("build/single"):
        filepath = os.path.join("build/single", filename)
        if os.path.isfile(filepath):
            os.unlink(filepath)
        elif os.path.isdir(filepath):
            shutil.rmtree(filepath)
    ficcount = 500
    while ficcount > 0:
        ficcount -= 1
        if ficcount < 10:
            ficcountstring = "00" + str(ficcount)
        elif ficcount < 100:
            ficcountstring = "0" + str(ficcount)
        else:
            ficcountstring = str(ficcount)
        if os.path.exists("originalsmeta/" + ficcountstring + ".py"):
            singlepage(ficcount,"originalsmeta",local)
        elif os.path.exists("translationsmeta/" + ficcountstring + ".py"):
            singlepage(ficcount,"translationsmeta",local)
        
if __name__ == "__main__":
    allsingles()