
import os
import sys

try:
    from PySide2.QtWidgets import *
    from PySide2.QtCore    import *
    from PySide2.QtGui     import *
except Exception as e:
    try:
        from PySide.QtCore import *
        from PySide.QtGui  import *
    except Exception as e:
        print('PySide/PySide2: Not found')
        exit()


########################################################################
# 3dsMax ver  pymxs MaxPlus   Comment                        Tested
########################################################################
# 2022        +     -                                        +
# 2021        +     -         qtmax module
# 2020        +     +                                        +
# 2019        +     +
# 2018        +     +                                        +
# 2017        +     +                                        +
# 2016.3      -     +         MaxPlus.AttachQWidgetToMax()
# 2016        -     +                                        +
# 2015        -     +                                        +
# 2014.1      -     +


#GetQMaxMainWindow
#GetQMaxWindow
#AttachQWidgetToMax

getMaxMainWindow = None
attachQWidgetToMax = None

g_use3dsMax = True

try:
    #[2022-2021]
    from qtmax import GetQMaxMainWindow as getMaxMainWindow
    print("qtmax.GetQMaxMainWindow found")
except Exception as e:
    try:
        #[2020-2018]
        from MaxPlus import GetQMaxMainWindow as getMaxMainWindow
        print("MaxPlus.GetQMaxMainWindow found")
    except Exception as e:
        try:
            #[2017]
            from MaxPlus import GetQMaxWindow as getMaxMainWindow
            print("MaxPlus.GetQMaxWindow found")
        except Exception as e:
            try:
                #[2016.1]
                from MaxPlus import AttachQWidgetToMax as attachQWidgetToMax
                print("MaxPlus.AttachQWidgetToMax found")
            except Exception as e:
                print("NR2 addon: Window parenting API not supported or 3ds Max API not found")


import nr3dsgui as gui


class _GCProtector(object):
	widgets = []


def mainImport(preVs):
    dlg = None
    if g_use3dsMax:
        # 3ds Max

        app = QApplication.instance()
        if not app:
            app = QApplication([])

        if getMaxMainWindow:
            if preVs:
                dlg = gui.PreVsImportGui(getMaxMainWindow())
            else:
                dlg = gui.PostVsImportGui(getMaxMainWindow())
        else:
            print("Can't get 3ds Max main window for using as parent")
            if preVs:
                dlg = gui.PreVsImportGui()
            else:
                dlg = gui.PostVsImportGui()
            if attachQWidgetToMax:
                print("Attach to parent window")
                attachQWidgetToMax(dlg)

        _GCProtector.widgets.append(dlg)
        dlg.show()
    else:
        # Gui tests from pure Python command line
        app = QApplication(sys.argv)
        if preVs:
            dlg = gui.PreVsImportGui()
        else:
            dlg = gui.PostVsImportGui()
        dlg.show()
        sys.exit(app.exec_())
