# -*- coding: utf-8 -*-

__Name__ = 'BoxCreator'
__Comment__ = 'Creates a box with interlocked notches'
__Author__ = 'christi'
__Version__ = '1.3.7'
__Date__ = '2023-01-03'
__License__ = 'LGPL-3.0-or-later'
__Web__ = 'https://forum.freecadweb.org/viewtopic.php?t=31795'
__Wiki__ = ''
__Icon__ = 'BoxCreator.svg'
__Help__ = 'Try it out and play with it. It should be self explanatory'
__Status__ = 'Beta'
__Requires__ = 'FreeCAD >= v0.17'
__Communication__ = 'https://forum.freecadweb.org/viewtopic.php?p=264483'
__Files__ = 'BoxCreator.svg,boxcreator/__init__.py,boxcreator/boxcreator.py,boxcreator/boxcreator.ui'


import os

import FreeCAD as app
import FreeCADGui as gui
from FreeCAD import Vector

from boxcreator import boxcreator


class BoxcreatorDialog():
    """Show a dialog for BoxCreator"""
    def __init__(self):
        self.ui_file = os.path.join(app.getUserMacroDir(True),
                                    'boxcreator/boxcreator.ui')
        self.form = gui.PySideUic.loadUi(self.ui_file)
        self._connect_widgets()
        self.form.show()

    def _connect_widgets(self):
        self.form.pushButtonCreate.pressed.connect(self.createBox)
        self.form.pushButton_CompartX.pressed.connect(self.compartmentX)
        self.form.pushButton_CompartY.pressed.connect(self.compartmentY)
        self.form.pushButton_CompartZ.pressed.connect(self.compartmentZ)

    def createBox(self):
        boxWidth = self.form.doubleSpinBoxWidth.value()
        boxHeight = self.form.doubleSpinBoxHeight.value()
        boxLength = self.form.doubleSpinBoxLength.value()
        notchWidth = self.form.doubleSpinBoxNotchWidth.value()
        materialWidth = self.form.doubleSpinBoxMaterialWidth.value()
        drawSides = [self.form.checkBoxTop.isChecked(),
                     self.form.checkBoxBottom.isChecked(),
                     self.form.checkBoxLeft.isChecked(),
                     self.form.checkBoxRight.isChecked(),
                     self.form.checkBoxFront.isChecked(),
                     self.form.checkBoxBack.isChecked()]
        overhangTop = [self.form.overhangTopLeft.value(),
                       self.form.overhangTopRight.value(),
                       self.form.overhangTopFront.value(),
                       self.form.overhangTopBack.value()]
        overhangBottom = [self.form.overhangBotLeft.value(),
                          self.form.overhangBotRight.value(),
                          self.form.overhangBotFront.value(),
                          self.form.overhangBotBack.value()]
        if (boxWidth == 0) or (boxHeight == 0) or (boxLength == 0):
            app.Console.PrintError('Error! None of the values can be 0!')
            # we bail out without doing anything
            return

        box = boxcreator.create_box(materialWidth, boxWidth, boxHeight, boxLength, notchWidth, drawSides, overhangTop, overhangBottom, app.activeDocument())
        gui.Selection.clearSelection()
        gui.Selection.addSelection(box)
        gui.SendMsgToActiveView('ViewFit')

    def compartmentX(self):
        self.createCompartment(Vector(1,0,0))

    def compartmentY(self):
        self.createCompartment(Vector(0,1,0))

    def compartmentZ(self):
        self.createCompartment(Vector(0,0,1))

    def createCompartment(self, direction):
        box = gui.Selection.getSelection()
        notchWidth = self.form.doubleSpinBoxNotchWidth.value()
        materialWidth = self.form.doubleSpinBoxMaterialWidth.value()
        drawSides = [self.form.checkBoxTop.isChecked(),
                     self.form.checkBoxBottom.isChecked(),
                     self.form.checkBoxLeft.isChecked(),
                     self.form.checkBoxRight.isChecked(),
                     self.form.checkBoxFront.isChecked(),
                     self.form.checkBoxBack.isChecked()]
        offset = self.form.compartmentOffset.value()
        boxsize = Vector(self.form.doubleSpinBoxWidth.value(), self.form.doubleSpinBoxLength.value(), self.form.doubleSpinBoxHeight.value())
        compartment = boxcreator.create_compartment(box, direction, offset, materialWidth, notchWidth, drawSides, boxsize)
        if compartment:
            gui.Selection.clearSelection()
            gui.Selection.addSelection(compartment)


if __name__ == '__main__':
    d = BoxcreatorDialog()
