#!/usr/bin/env python
# Copyright (C) 2017 Florian Festi
#
#   This program is free software: you can redistribute it and/or modify
#   it under the terms of the GNU General Public License as published by
#   the Free Software Foundation, either version 3 of the License, or
#   (at your option) any later version.
#
#   This program is distributed in the hope that it will be useful,
#   but WITHOUT ANY WARRANTY; without even the implied warranty of
#   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#   GNU General Public License for more details.
#
#   You should have received a copy of the GNU General Public License
#   along with this program.  If not, see <http://www.gnu.org/licenses/>.

import sys
import os.path

try:
    import boxes.generators
except ImportError:
    sys.path.append(os.path.dirname(__file__) + "/../..")
    import boxes.generators

class Boxes2rst:
    def __init__(self):
        self.boxes = {b.__name__ : b() for b in boxes.generators.getAllBoxGenerators().values() if b.webinterface}
        self.groups = boxes.generators.ui_groups
        self.groups_by_name = boxes.generators.ui_groups_by_name

        for name, box in self.boxes.items():
            self.groups_by_name.get(box.ui_group,
                                    self.groups_by_name["Misc"]).add(box)

    def write(self, path):
        with open(path, "w") as f:
            for name, group in self.groups_by_name.items():
                f.write(name + """
----------------

""")
                for box in group.generators:
                    f.write(box.__class__.__name__)
                    f.write("\n..........................................\n\n")
                    f.write("\n\n.. autoclass:: %s.%s" % (
                        box.__class__.__module__, box.__class__.__name__))
                    f.write("\n\n")
                    if os.path.exists("../../static/samples/"+ box.__class__.__name__+".jpg"):
                        f.write(".. image:: ../../static/samples/" + box.__class__.__name__+".jpg\n\n")

if __name__=="__main__":
    if len(sys.argv) != 2:
        print("Usage: boxes2inksacpe TARGETPATH")
    b = Boxes2rst()
    b.write(sys.argv[1])
