#!/usr/bin/env python

import re
import sys

snappy_ver = "v3.0"

html_header_string = """\
<html>
<head>
<title>Snappy Assembly</title>
<style>
BODY {
  margin-bottom: 200px;
}
TABLE TD {
  vertical-align: middle;
}
H2 {
  margin-bottom: 5px;
  margin-top: 24px;
  font-size: 20pt;
}
LI.section {
  font-size: 20pt;
  font-weight: bold;
}
H3 {
  margin-left: -15px;
  margin-bottom: 5px;
  margin-top: 18px;
  font-size: 16pt;
}
LI.step {
  padding-left: 15px;
  margin-left: 0;
  font-size: 16pt;
  font-weight: bold;
  list-style-type: none;
}
DIV.desc {
  margin-bottom: 15px;
  font-size: 12pt;
  font-weight: normal;
}
OL {
  margin-left: 30px;
}
UL {
  padding-left: 5px;
}
</style>
</head>
<body>
<h1>Snappy RepRap Assembly Instructions</h1>
<ol>
"""


class GenAssemblyIndex(object):
    indexfile = "docs/assembly/index.html"
    markdownfile = "wiki/{0}-Assembly.md".format(snappy_ver)
    sourcefile = "full_assembly.scad"
    modules = []
    modinfo = {}

    def write_index(self):
        with open(self.indexfile, "w") as f:
            f.write(html_header_string)

            for mod_eng in self.modules:
                f.write('<li class="section">')
                f.write('<h2>{0}</h2>\n'.format(mod_eng))

                stepcnt = len(self.modinfo[mod_eng])
                if stepcnt > 1:
                    f.write('<ul>\n')

                for stepinfo in self.modinfo[mod_eng]:
                    if stepcnt > 1:
                        f.write('<li class="step">')
                        f.write('<h3>Step {step}</h3>\n'.format(**stepinfo))

                    f.write(
                        '<div class="desc">{desc}</div>\n'
                        '<table>'
                        '<tr>'
                        '<td class="befor">'
                        '<img src="{module}_before.png">'
                        '</td>'
                        '<td class="arrow"><img src="arrow.png"></td>'
                        '<td class="after"><img src="{module}_after.png"></td>'
                        '</tr>'
                        '</table>\n'
                        .format(**stepinfo)
                    )

                    if stepcnt > 1:
                        f.write('</li>\n')

                if stepcnt > 1:
                    f.write('</ul>\n')

                f.write('</li>\n')

            f.write('<li class="section">\n')
            f.write('<h2>{0}</h2>\n'.format("RAMPS Wiring"))
            f.write('<div class="desc">\n')
            f.write('<p>Heres a diagram of what needs to be connected where on a RAMPS 1.4 controller board.</p>\n')
            f.write('<p><a href="RAMPS_Wiring_For_Snappy.png"><img width="800" height="600" src="RAMPS_Wiring_For_Snappy.png"></a></p>')
            f.write('<p>Click on the image to enlarge.</p>\n\n')
            f.write('</div>\n')
            f.write('</li>\n')

            f.write('<li class="section">\n')
            f.write('<h2>{0}</h2>\n'.format("Marlin Firmware for RAMPS"))
            f.write('<div class="desc">\n')
            f.write('You can find Marlin firmware pre-configured for the Snappy with a RAMPS 1.4 controller at\n')
            f.write('<a href="https://github.com/revarbat/snappy-reprap/tree/v3.0/firmware">https://github.com/revarbat/snappy-reprap/tree/v3.0/firmware</a>\n')
            f.write('</div>\n')
            f.write('</li>\n')

            f.write('</ol>\n')
            f.write('</body>\n')
            f.write('</html>\n')

    def write_markdown(self):
        with open(self.markdownfile, "w") as f:
            f.write("# Snappy RepRap Assembly Instructions\n\n")

            for mod_eng in self.modules:
                f.write('## {0}\n\n'.format(mod_eng))

                stepcnt = len(self.modinfo[mod_eng])
                for stepinfo in self.modinfo[mod_eng]:
                    stepinfo['base'] = (
                        'https://raw.githubusercontent.com/'
                        'revarbat/snappy-reprap/{0}/docs/assembly/'
                    ).format(snappy_ver)

                    if stepcnt > 1:
                        f.write('### Step {step}\n\n'.format(**stepinfo))

                    f.write(
                        '{desc}\n\n'
                        'Before | After\n'
                        '------ | -----\n'
                        '![{module} Step {step} Before]'
                        '({base}{module}_before.png) | '
                        '![{module} Step {step} After]'
                        '({base}{module}_after.png)\n\n'
                        .format(**stepinfo)
                    )
            f.write('## {0}\n\n'.format("RAMPS Wiring"))
            f.write('Heres a diagram of what needs to be connected where on a RAMPS 1.4 controller board.\n\n')
            f.write('[![RAMPS 1.4 Wiring Diagram]({0}-RAMPS_Wiring_For_Snappy.png)]({0}-RAMPS_Wiring_For_Snappy.png)\n'.format(snappy_ver))
            f.write('Click on the image to enlarge.\n\n')

            f.write('## {0}\n\n'.format("Marlin Firmware for RAMPS"))
            f.write('You can find Marlin firmware pre-configured for the Snappy with a RAMPS 1.4 controller at\n')
            f.write('https://github.com/revarbat/snappy-reprap/tree/{0}/firmware\n'.format(snappy_ver))



    def process_module(self, module, desc):
        print("module: {0}".format(module))
        step = 1
        mod_eng = module.replace('_', ' ') \
            .title() \
            .replace('Xy', 'XY') \
            .replace('Yz', 'YZ')
        mod_split = mod_eng.split(" ")
        if mod_split[-1].isdigit():
            step = int(mod_split[-1])
            mod_eng = " ".join(mod_split[:-1])
        if mod_eng not in self.modules:
            self.modules.append(mod_eng)
            self.modinfo[mod_eng] = [
                {
                    'module': module,
                    'step': step,
                    'desc': desc
                },
            ]
        else:
            self.modinfo[mod_eng].append(
                {
                    'module': module,
                    'step': step,
                    'desc': desc
                },
            )

    def generate_index(self):
        mod_re = re.compile(
            r'module  *([a-z_][a-z0-9_]*_assembly(_[0-9]+)?) *\('
        )
        desc_re = re.compile(r'// *desc: *(.*)$')

        module = ""
        desc = ""
        with open(self.sourcefile, "r") as f:
            for line in f.readlines():
                mod_res = mod_re.search(line)
                if mod_res:
                    if module:
                        self.process_module(module, desc)
                    module = mod_res.group(1)
                    desc = ""
                desc_res = desc_re.search(line)
                if desc_res:
                    desc += desc_res.group(1)
            if module:
                self.process_module(module, desc)
        self.write_index()
        self.write_markdown()


def main():
    genidx = GenAssemblyIndex()
    genidx.generate_index()
    sys.exit(0)


if __name__ == "__main__":
    main()


# vim: expandtab tabstop=4 shiftwidth=4 softtabstop=4 nowrap
