# This file is part of bcad.
#
# bcad 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 2 of the License, or
# (at your option) any later version.
#
# bcad 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 bcad; if not, see <https://www.gnu.org/licenses/>.

import logging
import sys

from bcad.binterpreter.main_window import MainWindow
from bcad.binterpreter.singleton import Singleton

log = logging.getLogger(__name__)


def check_callable(_callable):
    if not callable(_callable):
        raise AssertionError("The function supplied is not callable")


def init_display(background_gradient_color1=None, background_gradient_color2=None, display_triedron=False, periodic_callback=None, period=None):

    win = MainWindow(False, Singleton.pipe, Singleton.img)
    logging.info("Init driver")
    win.canva.init_driver()
    display = win.canva._display

    def start_display():
        win.mainloop()

    if display_triedron:
        display.display_triedron()

    if background_gradient_color1 and background_gradient_color2:
        display.set_bg_gradient_color(background_gradient_color1, background_gradient_color2)

    return display, start_display


if __name__ == '__main__':
    display, start_display = init_display()
    from OCC.Core.BRepPrimAPI import BRepPrimAPI_MakeSphere, BRepPrimAPI_MakeBox

    def sphere(event=None):
        display.DisplayShape(BRepPrimAPI_MakeSphere(100).Shape(), update=True)

    def cube(event=None):
        display.DisplayShape(BRepPrimAPI_MakeBox(1, 1, 1).Shape(), update=True)

    def quit(event=None):
        sys.exit()

    start_display()
