#!/usr/bin/python3

import os
import shutil


def write_header(fi):
    fi.write('<html>\n')
    fi.write('<head>\n')
    fi.write('<link rel="stylesheet" type="text/css" href="styles.css">\n')
    fi.write('</head>\n')
    fi.write('<body>\n')


def write_footer(fi):
    fi.write('</body>\n')
    fi.write('</html>')


def write_menu(fi, dirs):
    for dirname in dirs:
        index_name = dirname + '.html'
        fi.write('<a href="' + index_name + '">')
        fi.write(dirname)
        fi.write('</a>')
        fi.write('<br />\n')


def write_flat_menu(fi, dirs):
    for dirname in dirs:
        index_name = dirname + '.html'
        fi.write('<a href="' + index_name + '">')
        fi.write(dirname)
        fi.write('</a>')
        fi.write('\n')


if __name__ == "__main__":

    print("Creating homepage for PNG files")

    # where to store the generated html page
    output = 'html'
    images_dir_name = 'images'
    svgs_dir_name = 'svgs'

    # directory with svgs in subdirectories.
    # used to build the list of files presented
    svgdir = 'svgs'
    # directory with pngs in subdirectories
    # generated by convert.py, will be copied over
    pngdir = 'pngs'
    # resources used by the homepage, will be copied over
    resources = 'resources'

    # paths to index.html and images subdir
    main_index = os.path.join(output, 'index.html')
    images = os.path.join(output, images_dir_name)
    svgs = os.path.join(output, svgs_dir_name)

    # create the output directory
    os.makedirs(output, exist_ok=True)

    # copy images if not present yet
    if not os.path.isdir(images):
        shutil.copytree(pngdir, images)

    # copy svgs if not present yet
    if not os.path.isdir(svgs):
        shutil.copytree(svgdir, svgs)

    # copy resources
    for file in os.listdir(resources):
        shutil.copy(os.path.join(resources, file), output)

    # get a list of all subdirectories (icon classes)
    dirs = os.listdir(svgdir)
    dirs.sort()

    # create the main index file
    fmi = open(main_index, 'w')
    write_header(fmi)
    write_menu(fmi, dirs)
    fmi.write('<br />')

    # create an index for each icon class
    for dirname in dirs:
        index_name = dirname + '.html'
        index = os.path.join(output, index_name)

        fi = open(index, 'w')
        write_header(fi)
        write_flat_menu(fi, dirs)
        fi.write('<br />\n')

        # find corresponding png subdir for the svg subdir
        svg_subdir = os.path.join(svgdir, dirname)

        # iterate contained svgs
        files = os.listdir(svg_subdir)
        files.sort()
        for file in files:
            name, extension = os.path.splitext(file)
            f = os.path.join(images_dir_name, dirname, 'drawable-xhdpi')
            simple = os.path.join(f, name + '_simple.png')
            opacity = os.path.join(f, name + '_opacity.png')

            # figure out name and extension
            svg = os.path.join(svg_subdir, file)
            name, extension = os.path.splitext(file)

            # only take 48px svgs to save some time
            if not "48px" in name:
                continue

            svg = os.path.join(svgs_dir_name, dirname, file)

            fi.write('<div class="icongroup">\n')
            fi.write('<img class="icon" src="' + simple + '" />\n')
            fi.write('<img class="icon" src="' + opacity + '" />\n')
            fi.write('<a href="' + svg + '"/>\n')
            fi.write('<img class="button" src="download.png" />\n')
            fi.write('</a>\n')
            fi.write('</div>\n')

        write_footer(fi)
        fi.close()

    write_footer(fmi)
    fmi.close()
