#!/usr/bin/env python3
from pathlib import Path

from _generate_resources import *

ignored_files = ['qt.conf', 'resources.qrc', 'resources_autogenerated.qrc', 'windows.rc',
        'generate_resources.py', '_generate_resources.py']
ignored_directories = ['__pycache__']

def isNotIgnored(file):
    return str(file) not in ignored_files

all_files = list(filter(isNotIgnored, \
    filter(Path.is_file, Path('.').glob('**/*'))))
image_files = list(filter(isNotIgnored, \
    filter(Path.is_file, Path('.').glob('**/*.png'))))

with open('./resources_autogenerated.qrc', 'w') as out:
    out.write(resources_header)
    for file in all_files:
        out.write(f"    <file>{str(file)}</file>\n")
    out.write(resources_footer)

with open('../src/autogenerated/ResourcesAutogen.cpp', 'w') as out:
    out.write(source_header)
    for file in sorted(image_files):
        var_name = str(file.with_suffix("")).replace("/",".")
        out.write(f'    this->{var_name}')
        out.write(f' = QPixmap(":/{file}");\n')
    out.write(source_footer)

def writeHeader(out, name, element, indent):
    if isinstance(element, dict):
        if name != "":
            out.write(f"{indent}struct {{\n")
        for (key, value) in element.items():
            writeHeader(out, key, value, indent + '    ')
        if name != "":
            out.write(f"{indent}}} {name};\n");
    else:
        out.write(f"{indent}QPixmap {element};\n")

with open('../src/autogenerated/ResourcesAutogen.hpp', 'w') as out:
    out.write(header_header)

    elements = {}
    for file in sorted(image_files):
        elements_ref = elements
        directories = str(file).split('/')[:-1]
        filename = file.stem
        for directory in directories:
            if directory not in elements_ref:
                if directory not in ignored_directories:
                    elements_ref[directory] = {}
            elements_ref = elements_ref[directory]
        elements_ref[filename] = filename

    writeHeader(out, "", elements, '')

    out.write(header_footer)

