import os
import sys

g_versionStr = '1.0.3'

try:
    from PySide2.QtWidgets import *
    from PySide2.QtCore    import *
    from PySide2.QtGui     import *
except Exception as e:
    # TODO: pyside6?
    from PySide.QtCore     import *
    from PySide.QtGui      import *


import nrfile
import nrtools
import nrdump
import nrimp

import nr3dsimp


# UV-attribute idx
#  U-component idx
#  V-component idx
class TexturingAttribIndex(QWidget):
    def __init__(self, parent=None):
        super(TexturingAttribIndex, self).__init__(parent)
        mainLayout = QVBoxLayout(self)

        horLayout = QHBoxLayout()
        # UV-Attribute index
        gridLayout = QGridLayout()
        gridLayout.addWidget(QLabel("UV-Attribute Index"), 0, 0)
        self.uvAttrIndexSpinBox = QSpinBox()
        self.uvAttrIndexSpinBox.setRange(0, 64)
        self.uvAttrIndexSpinBox.setValue(1)
        gridLayout.addWidget(self.uvAttrIndexSpinBox, 0, 1)

        # U-component index
        gridLayout.addWidget(QLabel("U/V-Component Index"), 1, 0)
        self.uCompIndexSpinBox = QSpinBox()
        self.uCompIndexSpinBox.setRange(0, 3)
        self.uCompIndexSpinBox.setValue(0)
        gridLayout.addWidget(self.uCompIndexSpinBox, 1, 1)
        # V-component index
        self.vCompIndexSpinBox = QSpinBox()
        self.vCompIndexSpinBox.setRange(0, 3)
        self.vCompIndexSpinBox.setValue(1)
        gridLayout.addWidget(self.vCompIndexSpinBox, 1, 2)

        horLayout.addLayout(gridLayout)
        horLayout.addStretch()

        mainLayout.addLayout(horLayout)
        mainLayout.addStretch()


# U-attribute idx/U-component idx
# V-attribute idx/V-component idx
class TexturingAttribCompIndex(QWidget):
    def __init__(self, parent=None):
        super(TexturingAttribCompIndex, self).__init__(parent)
        mainLayout = QVBoxLayout(self)

        horLayout = QHBoxLayout()

        gridLayout = QGridLayout()
        # U-attribute index
        gridLayout.addWidget(QLabel("U-Attribute/Component Index"), 0, 0)
        self.uAttrIndexSpinBox = QSpinBox()
        self.uAttrIndexSpinBox.setRange(0, 64)
        self.uAttrIndexSpinBox.setValue(1)
        gridLayout.addWidget(self.uAttrIndexSpinBox, 0, 1)
        # U-component index
        self.uCompIndexSpinBox = QSpinBox()
        self.uCompIndexSpinBox.setRange(0, 3)
        self.uCompIndexSpinBox.setValue(0)
        gridLayout.addWidget(self.uCompIndexSpinBox, 0, 2)

        # V-attribute index
        gridLayout.addWidget(QLabel("V-Attribute/Component Index"), 1, 0)
        self.vAttrIndexSpinBox = QSpinBox()
        self.vAttrIndexSpinBox.setRange(0, 64)
        self.vAttrIndexSpinBox.setValue(1)
        gridLayout.addWidget(self.vAttrIndexSpinBox, 1, 1)
        # V-component index
        self.vCompIndexSpinBox = QSpinBox()
        self.vCompIndexSpinBox.setRange(0, 3)
        self.vCompIndexSpinBox.setValue(1)
        gridLayout.addWidget(self.vCompIndexSpinBox, 1, 2)

        horLayout.addLayout(gridLayout)
        horLayout.addStretch()

        mainLayout.addLayout(horLayout)
        mainLayout.addStretch()


class TexturingAttribName(QWidget):
    def __init__(self, parent=None):
        super(TexturingAttribName, self).__init__(parent)
        mainLayout = QVBoxLayout(self)

        horLayout = QHBoxLayout()
        horLayout.addWidget(QLabel("Attribute name"))
        self.attribName  = QLineEdit("TEXCOORD")
        horLayout.addWidget(self.attribName)
        horLayout.addStretch()
        mainLayout.addLayout(horLayout)

        horLayout = QHBoxLayout()

        gridLayout = QGridLayout()
        # U-coord index
        gridLayout.addWidget(QLabel("U/V-Component Index"), 0, 0)
        self.uCompIndexSpinBox = QSpinBox()
        self.uCompIndexSpinBox.setRange(0, 3)
        self.uCompIndexSpinBox.setValue(0)
        gridLayout.addWidget(self.uCompIndexSpinBox, 0, 1)

        # V-coord index
        #gridLayout.addWidget(QLabel("V-Component Index"), 1, 0)
        self.vCompIndexSpinBox = QSpinBox()
        self.vCompIndexSpinBox.setRange(0, 3)
        self.vCompIndexSpinBox.setValue(1)
        gridLayout.addWidget(self.vCompIndexSpinBox, 0, 2)
        horLayout.addLayout(gridLayout)

        horLayout.addStretch()
        mainLayout.addLayout(horLayout)

        mainLayout.addStretch()


        # UV-Index
        horLayout = QHBoxLayout()
        horLayout.addWidget(QLabel("Assign Texture From Stage"))
        self.uvIndexSpinBox = QSpinBox()
        self.uvIndexSpinBox.setRange(0, 64)
        self.uvIndexSpinBox.setValue(0)
        horLayout.addWidget(self.uvIndexSpinBox)
        horLayout.addStretch()
        mainLayout.addLayout(horLayout)

        mainLayout.addStretch()


class TexturingAuto(QWidget):
    def __init__(self, parent=None):
        super(TexturingAuto, self).__init__(parent)


class GeometryPostVsMatrix(QWidget):
    def __init__(self, parent=None):
        super(GeometryPostVsMatrix, self).__init__(parent)
        self.mainLayout = QVBoxLayout(self)

        horLayout = QHBoxLayout()
        #horLayout.addWidget(QLabel("Matrix"))
        self.matrixLineEdit = QLineEdit("[[1, 0, 0, 0], [0, 1, 0, 0], [0, 0, 1, 0],[0, 0, 0, 1]]")
        horLayout.addWidget(self.matrixLineEdit)
        self.mainLayout.addLayout(horLayout)

        horLayout = QHBoxLayout()
        self.transposeCheckBox = QCheckBox("Transpose")
        horLayout.addWidget(self.transposeCheckBox)
        horLayout.addStretch()
        self.mainLayout.addLayout(horLayout)

    def getProjectonMatrix(self):
        matrix = nrtools.stringToMat44(self.matrixLineEdit.text())
        if self.transposeCheckBox.isChecked():
            matrix = nrtools.transposeMat(matrix)
        return matrix


class GeometryPostVsManual(QWidget):
    def __init__(self, parent=None):
        super(GeometryPostVsManual, self).__init__(parent)
        self.mainLayout = QHBoxLayout(self)

        gridLayout = QGridLayout()
        # FOV
        gridLayout.addWidget(QLabel("FOV_Y"), 0, 0)
        self.fovSpinBox = QSpinBox()
        self.fovSpinBox.setRange(1, 360)
        self.fovSpinBox.setValue(45)
        #self.fovSpinBox.setFixedWidth(70)
        gridLayout.addWidget(self.fovSpinBox, 0, 1)

        # Space
        gridLayout.setRowStretch(1, 1)

        # Screen width
        gridLayout.addWidget(QLabel("Screen width"), 2, 0)
        self.screenWidth = QSpinBox()
        self.screenWidth.setRange(1, 10000)
        self.screenWidth.setValue(1024)
        gridLayout.addWidget(self.screenWidth, 2, 1)

        # Screen height
        gridLayout.addWidget(QLabel("Screen height"), 3, 0)
        self.screenHeight = QSpinBox()
        self.screenHeight.setRange(1, 10000)
        self.screenHeight.setValue(768)
        gridLayout.addWidget(self.screenHeight, 3, 1)

        # Space
        gridLayout.setRowStretch(4, 1)

        # Near distance
        gridLayout.addWidget(QLabel("Near"), 5, 0)
        self.nearDist = QDoubleSpinBox()
        self.nearDist.setRange(0.001, 10000)
        self.nearDist.setValue(0.01)
        self.nearDist.setSingleStep(0.01)
        gridLayout.addWidget(self.nearDist, 5, 1)

        # Far distance
        gridLayout.addWidget(QLabel("Far"), 6, 0)
        self.farDist = QSpinBox()
        self.farDist.setRange(1, 1000000)
        self.farDist.setValue(1000)
        gridLayout.addWidget(self.farDist, 6, 1)

        self.mainLayout.addLayout(gridLayout)
        self.mainLayout.addStretch()

    def getProjectonMatrix(self):
        fov      = float(self.fovSpinBox.value())
        width    = float(self.screenWidth.value())
        height   = float(self.screenHeight.value())
        nearDist = float(self.nearDist.value())
        farDist  = float(self.farDist.value())
        return nrtools.perspMatFovLH(fov, width, height, nearDist, farDist)


# 0
class GeometryPreVsAuto(QWidget):
    def __init__(self, parent=None):
        super(GeometryPreVsAuto, self).__init__(parent)


# 1
# Position-attribute idx
#  X/Y/Z-component idx
class GeometryPreVsAttrib(QWidget):
    def __init__(self, parent=None):
        super(GeometryPreVsAttrib, self).__init__(parent)
        mainLayout = QVBoxLayout(self)

        horLayout = QHBoxLayout()
        # UV-Attribute index
        gridLayout = QGridLayout()
        gridLayout.addWidget(QLabel("Position-Attribute Index"), 0, 0)
        self.posAttrIndexSpinBox = QSpinBox()
        self.posAttrIndexSpinBox.setRange(0, 64)
        self.posAttrIndexSpinBox.setValue(0)
        gridLayout.addWidget(self.posAttrIndexSpinBox, 0, 1)

        # X-component index
        gridLayout.addWidget(QLabel("X/Y/Z-Component Index"), 1, 0)
        self.xCompIndexSpinBox = QSpinBox()
        self.xCompIndexSpinBox.setRange(0, 3)
        self.xCompIndexSpinBox.setValue(0)
        gridLayout.addWidget(self.xCompIndexSpinBox, 1, 1)
        # Y-component index
        self.yCompIndexSpinBox = QSpinBox()
        self.yCompIndexSpinBox.setRange(0, 3)
        self.yCompIndexSpinBox.setValue(1)
        gridLayout.addWidget(self.yCompIndexSpinBox, 1, 2)
        # Z-component index
        self.zCompIndexSpinBox = QSpinBox()
        self.zCompIndexSpinBox.setRange(0, 3)
        self.zCompIndexSpinBox.setValue(2)
        gridLayout.addWidget(self.zCompIndexSpinBox, 1, 3)

        horLayout.addLayout(gridLayout)
        horLayout.addStretch()

        mainLayout.addLayout(horLayout)
        mainLayout.addStretch()


# 2
class GeometryPreVsAttribComp(QWidget):
    def __init__(self, parent=None):
        super(GeometryPreVsAttribComp, self).__init__(parent)
        self.mainLayout = QVBoxLayout(self)

        horLayout = QHBoxLayout()

        gridLayout = QGridLayout()
        # X-attribute index
        gridLayout.addWidget(QLabel("X-Attribute/Component Index"), 0, 0)
        self.xAttrIndexSpinBox = QSpinBox()
        self.xAttrIndexSpinBox.setRange(0, 64)
        self.xAttrIndexSpinBox.setValue(0)
        gridLayout.addWidget(self.xAttrIndexSpinBox, 0, 1)
        # X-component index
        self.xCompIndexSpinBox = QSpinBox()
        self.xCompIndexSpinBox.setRange(0, 3)
        self.xCompIndexSpinBox.setValue(0)
        gridLayout.addWidget(self.xCompIndexSpinBox, 0, 2)


        # Y-attribute index
        gridLayout.addWidget(QLabel("Y-Attribute/Component Index"), 1, 0)
        self.yAttrIndexSpinBox = QSpinBox()
        self.yAttrIndexSpinBox.setRange(0, 64)
        self.yAttrIndexSpinBox.setValue(0)
        gridLayout.addWidget(self.yAttrIndexSpinBox, 1, 1)
        # Y-component index
        self.yCompIndexSpinBox = QSpinBox()
        self.yCompIndexSpinBox.setRange(0, 3)
        self.yCompIndexSpinBox.setValue(1)
        gridLayout.addWidget(self.yCompIndexSpinBox, 1, 2)


        # Z-attribute index
        gridLayout.addWidget(QLabel("Z-Attribute/Component Index"), 2, 0)
        self.zAttrIndexSpinBox = QSpinBox()
        self.zAttrIndexSpinBox.setRange(0, 64)
        self.zAttrIndexSpinBox.setValue(0)
        gridLayout.addWidget(self.zAttrIndexSpinBox, 2, 1)
        # Z-component index
        self.zCompIndexSpinBox = QSpinBox()
        self.zCompIndexSpinBox.setRange(0, 3)
        self.zCompIndexSpinBox.setValue(2)
        gridLayout.addWidget(self.zCompIndexSpinBox, 2, 2)

        horLayout.addLayout(gridLayout)
        horLayout.addStretch()

        self.mainLayout.addLayout(horLayout)
        self.mainLayout.addStretch()


class PostVsImportGui(QDialog):
    def __init__(self, parent=None):
        super(PostVsImportGui, self).__init__(parent)
 
        self.setWindowTitle("Ninja Ripper 2 World Space Import  ({})".format(g_versionStr))
        self.setWindowFlags(self.windowFlags() & ~Qt.WindowContextHelpButtonHint);

        # User selected files
        self.fileNamesList = []

        # Main Vertical layout
        self.mainLayout = QVBoxLayout(self)

        # File select button and Horizontal layout
        horLayout = QHBoxLayout()
        button = QPushButton("Select Files")
        horLayout.addWidget(button)
        self.mainLayout.addLayout(horLayout)
        button.clicked.connect(self.filesSelectSlot)

        # Geometry reconstruction layout
        self.geometryLayout = QVBoxLayout()
        horLayout = QHBoxLayout()
        horLayout.addWidget(QLabel("Geometry Load Parameters"))
        self.geometryCombo = QComboBox()
        self.geometryCombo.addItem("Manual")
        self.geometryCombo.addItem("Matrix")
        self.geometryCombo.currentIndexChanged.connect(self.geometryComboChangeSlot)
        horLayout.addWidget(self.geometryCombo)
        horLayout.addStretch()
        self.geometryLayout.addLayout(horLayout)
        self.geometry = GeometryPostVsManual() # Default
        self.geometryLayout.addWidget(self.geometry)
        self.mainLayout.addLayout(self.geometryLayout)
        
        self.mainLayout.addStretch()

        # Texturing reconstruction
        self.textureLayoutMain = QVBoxLayout()

        horLayout = QHBoxLayout()
        horLayout.addWidget(QLabel("Texture Load Parameters"))
        self.textureCombo = QComboBox()
        self.textureCombo.addItem("Auto")                      # 0
        self.textureCombo.addItem("Attribute Name")            # 1
        self.textureCombo.addItem("Attribute Index")           # 2
        self.textureCombo.addItem("Attribute/Component Index") # 3
        horLayout.addWidget(self.textureCombo)

        horLayout.addStretch()
        self.textureLayoutMain.addLayout(horLayout)
        self.textureCombo.currentIndexChanged.connect(self.texturingComboChangeSlot)

        # Set default widget
        self.textureLayout = QVBoxLayout()
        self.texturing = TexturingAuto()
        self.textureLayout.addWidget(self.texturing)
        self.textureLayoutMain.addLayout(self.textureLayout)
        self.mainLayout.addLayout(self.textureLayoutMain)

        # Check box
        horLayout = QHBoxLayout()
        self.checkBox = QCheckBox('Get Texcoords From Local Space')
        horLayout.addWidget(self.checkBox)
        self.mainLayout.addLayout(horLayout)
        self.mainLayout.addStretch()

        if nr3dsimp.isMaxPlusAvailable():
            horLayout = QHBoxLayout()
            self.convertToEditPoly = QCheckBox('Convert Mesh to Editable Poly (Slow)')
            horLayout.addWidget(self.convertToEditPoly)
            horLayout.addStretch()
            self.mainLayout.addLayout(horLayout)

        # Import button
        horLayout = QHBoxLayout()
        self.importButton = QPushButton("Import")
        self.importButton.setMinimumHeight(30)
        horLayout.addWidget(self.importButton)
        self.mainLayout.addLayout(horLayout)
        self.importButton.clicked.connect(self.importSlot)

        self.mainLayout.addStretch()


    @Slot()
    def filesSelectSlot(self):
        outDir = nrtools.getNrOutputDir()
        if outDir:
            dialog = QFileDialog(self, "Select files", outDir)
        else:
            dialog = QFileDialog(self, "Select files")
        dialog.setFileMode(QFileDialog.ExistingFiles)
        dialog.setNameFilter("Ninja Ripper 2 files (*.nr)")
        if dialog.exec_():
            self.fileNamesList = dialog.selectedFiles();

    @Slot()
    def geometryComboChangeSlot(self, idx):
        if 0 == idx:
            self.geometry.deleteLater()
            self.geometry = None
            self.geometry = GeometryPostVsManual()
            self.geometryLayout.addWidget(self.geometry)
        if 1 == idx:
            self.geometry.deleteLater()
            self.geometry = None
            self.geometry = GeometryPostVsMatrix()
            self.geometryLayout.addWidget(self.geometry)

    @Slot()
    def texturingComboChangeSlot(self, idx):
        #print("TextureLoadIdx={:d}".format(idx))
        if 0 == idx:
            self.texturing.deleteLater()
            self.texturing = TexturingAuto()
            self.textureLayout.addWidget(self.texturing)
        if 1 == idx:
            self.texturing.deleteLater()
            self.texturing = TexturingAttribName()
            self.textureLayout.addWidget(self.texturing)
        if 2 == idx:
            self.texturing.deleteLater()
            self.texturing = TexturingAttribIndex()
            self.textureLayout.addWidget(self.texturing)
        if 3 == idx:
            self.texturing.deleteLater()
            self.texturing = TexturingAttribCompIndex()
            self.textureLayout.addWidget(self.texturing)


    def getOptions(self):
        options = nrimp.ImportOptions()
        maxImpOptions = nr3dsimp.MaxImportOptions()
        if hasattr(self, 'convertToEditPoly'):
            if self.convertToEditPoly.isChecked():
                maxImpOptions.convertMeshTo = nr3dsimp.kConvertMeshTo.EditablePoly
                print("Convert to Editable Poly")
        setattr(options, 'maxImpOptions', maxImpOptions)

        # Geometry parameters
        options.geometryLoadMode = nrimp.GeometryLoadMode.PostVs
        options.projMat = self.geometry.getProjectonMatrix()

        # Texturing parameters
        texComboIdx = self.textureCombo.currentIndex()
        if 0 == texComboIdx: # Auto
            options.textureLoadMode = nrimp.TextureLoadMode.Auto
        elif 1 == texComboIdx: # Attribute Name
            options.textureLoadMode = nrimp.TextureLoadMode.AttributeName
            options.texAttrName_attribName = self.texturing.attribName.text()
            options.texAttrName_uCompIdx   = self.texturing.uCompIndexSpinBox.value()
            options.texAttrName_vCompIdx   = self.texturing.vCompIndexSpinBox.value()
            options.texAttrName_uvIdx      = self.texturing.uvIndexSpinBox.value()
        elif 2 == texComboIdx: # Attribute Index
            options.textureLoadMode = nrimp.TextureLoadMode.AttributeIndex
            options.texAttrIdx_uAttribIdx  = self.texturing.uvAttrIndexSpinBox.value()
            options.texAttrIdx_vAttribIdx  = self.texturing.uvAttrIndexSpinBox.value()
            options.texAttrIdx_uCompIdx    = self.texturing.uCompIndexSpinBox.value()
            options.texAttrIdx_vCompIdx    = self.texturing.vCompIndexSpinBox.value()
        elif 3 == texComboIdx: # Attribute Index
            options.textureLoadMode = nrimp.TextureLoadMode.AttributeIndex
            options.texAttrIdx_uAttribIdx  = self.texturing.uAttrIndexSpinBox.value()
            options.texAttrIdx_vAttribIdx  = self.texturing.vAttrIndexSpinBox.value()
            options.texAttrIdx_uCompIdx    = self.texturing.uCompIndexSpinBox.value()
            options.texAttrIdx_vCompIdx    = self.texturing.vCompIndexSpinBox.value()

        options.loadExtraUvData = False
        if self.checkBox.isChecked():
            options.loadExtraUvData = True

        return options

    @Slot()
    def importSlot(self):
        if len(self.fileNamesList):
            options = self.getOptions()
            options.dump()

            print("Import started")

            imp = nr3dsimp.MaxImporter(options)

            for fileName in self.fileNamesList:
                imp.importMesh(fileName)

            print("Import finished")
        else:
            print("No nr-files selected")


#####################################################################
#####################################################################


class PreVsImportGui(QDialog):
    def __init__(self, parent=None):
        super(PreVsImportGui, self).__init__(parent)

        self.options = nrimp.ImportOptions()
 
        self.setWindowTitle("Ninja Ripper 2 Local Space Import  ({})".format(g_versionStr))
        self.setWindowFlags(self.windowFlags() & ~Qt.WindowContextHelpButtonHint);


        # User selected files
        self.fileNamesList = []

        # Main Vertical layout
        self.mainLayout = QVBoxLayout(self)

        # File select button and Horizontal layout
        horLayout = QHBoxLayout()
        button = QPushButton("Select Files")
        horLayout.addWidget(button)
        self.mainLayout.addLayout(horLayout)
        button.clicked.connect(self.filesSelectSlot)

        # Geometry reconstruction layout
        self.geometryLayout = QVBoxLayout()
        horLayout = QHBoxLayout()
        horLayout.addWidget(QLabel("Geometry Load Parameters"))
        self.geometryCombo = QComboBox()
        self.geometryCombo.addItem("Auto")                      # 0
        self.geometryCombo.addItem("Attribute Index")           # 1
        self.geometryCombo.addItem("Attribute/Component Index") # 2
        self.geometryCombo.currentIndexChanged.connect(self.geometryComboChangeSlot)
        horLayout.addWidget(self.geometryCombo)
        horLayout.addStretch()
        self.geometryLayout.addLayout(horLayout)
        self.geometry = GeometryPreVsAuto() # Default
        self.geometryLayout.addWidget(self.geometry)
        self.mainLayout.addLayout(self.geometryLayout)
        
        self.mainLayout.addStretch()

        # Texturing reconstruction
        self.textureLayoutMain = QVBoxLayout()

        horLayout = QHBoxLayout()
        horLayout.addWidget(QLabel("Texture Load Parameters"))
        self.textureCombo = QComboBox()
        self.textureCombo.addItem("Auto")                      # 0
        self.textureCombo.addItem("Attribute Name")            # 1
        self.textureCombo.addItem("Attribute Index")           # 2
        self.textureCombo.addItem("Attribute/Component Index") # 3
        horLayout.addWidget(self.textureCombo)

        horLayout.addStretch()
        self.textureLayoutMain.addLayout(horLayout)
        self.textureCombo.currentIndexChanged.connect(self.texturingComboChangeSlot)


        # Set default widget
        self.textureLayout = QVBoxLayout()
        self.texturing = TexturingAuto()
        self.textureLayout.addWidget(self.texturing)
        self.textureLayoutMain.addLayout(self.textureLayout)
        self.mainLayout.addLayout(self.textureLayoutMain)

        # Check box
        horLayout = QHBoxLayout()
        self.checkBox = QCheckBox('Get Texcoords From World Space')
        horLayout.addWidget(self.checkBox)
        self.mainLayout.addLayout(horLayout)
        self.mainLayout.addStretch()


        if nr3dsimp.isMaxPlusAvailable():
            horLayout = QHBoxLayout()
            self.convertToEditPoly = QCheckBox('Convert Mesh to Editable Poly (Slow)')
            horLayout.addWidget(self.convertToEditPoly)
            horLayout.addStretch()
            self.mainLayout.addLayout(horLayout)


        # Import button
        horLayout = QHBoxLayout()
        self.importButton = QPushButton("Import")
        self.importButton.setMinimumHeight(30)
        horLayout.addWidget(self.importButton)
        self.mainLayout.addLayout(horLayout)
        self.importButton.clicked.connect(self.importSlot)

        self.mainLayout.addStretch()

    @Slot()
    def filesSelectSlot(self):
        outDir = nrtools.getNrOutputDir()
        if outDir:
            dialog = QFileDialog(self, "Select files", outDir)
        else:
            dialog = QFileDialog(self, "Select files")
        dialog.setFileMode(QFileDialog.ExistingFiles)
        dialog.setNameFilter("Ninja Ripper 2 files (*.nr)")
        if dialog.exec_():
            self.fileNamesList = dialog.selectedFiles();

    @Slot()
    def geometryComboChangeSlot(self, idx):
        if 0 == idx:
            self.geometry.deleteLater()
            self.geometry = None
            self.geometry = GeometryPreVsAuto()
            self.geometryLayout.addWidget(self.geometry)
        if 1 == idx:
            self.geometry.deleteLater()
            self.geometry = None
            self.geometry = GeometryPreVsAttrib()
            self.geometryLayout.addWidget(self.geometry)
        if 2 == idx:
            self.geometry.deleteLater()
            self.geometry = None
            self.geometry = GeometryPreVsAttribComp()
            self.geometryLayout.addWidget(self.geometry)

    @Slot()
    def texturingComboChangeSlot(self, idx):
        #print("TextureLoadIdx={:d}".format(idx))
        if 0 == idx:
            self.texturing.deleteLater()
            self.texturing = TexturingAuto()
            self.textureLayout.addWidget(self.texturing)
        if 1 == idx:
            self.texturing.deleteLater()
            self.texturing = TexturingAttribName()
            self.textureLayout.addWidget(self.texturing)
        if 2 == idx:
            self.texturing.deleteLater()
            self.texturing = TexturingAttribIndex()
            self.textureLayout.addWidget(self.texturing)
        if 3 == idx:
            self.texturing.deleteLater()
            self.texturing = TexturingAttribCompIndex()
            self.textureLayout.addWidget(self.texturing)


    def getOptions(self):
        options = nrimp.ImportOptions()
        maxImpOptions = nr3dsimp.MaxImportOptions()
        if hasattr(self, 'convertToEditPoly'):
            if self.convertToEditPoly.isChecked():
                maxImpOptions.convertMeshTo = nr3dsimp.kConvertMeshTo.EditablePoly
                print("Convert to Editable Poly")
        setattr(options, 'maxImpOptions', maxImpOptions)

        # Geometry parameters
        geomCombo = self.geometryCombo.currentIndex()
        if 0 == geomCombo:
            options.geometryLoadMode = nrimp.GeometryLoadMode.PreVs_Auto
        elif 1 == geomCombo:
            options.geometryLoadMode = nrimp.GeometryLoadMode.PreVs_Manual
            options.prevs_xAttrIdx = self.geometry.posAttrIndexSpinBox.value()
            options.prevs_yAttrIdx = self.geometry.posAttrIndexSpinBox.value()
            options.prevs_zAttrIdx = self.geometry.posAttrIndexSpinBox.value()
            options.prevs_xCompIdx = self.geometry.xCompIndexSpinBox.value()
            options.prevs_yCompIdx = self.geometry.yCompIndexSpinBox.value()
            options.prevs_zCompIdx = self.geometry.zCompIndexSpinBox.value()
        elif 2 == geomCombo:
            options.geometryLoadMode = nrimp.GeometryLoadMode.PreVs_Manual
            options.prevs_xAttrIdx = self.geometry.xAttrIndexSpinBox.value()
            options.prevs_yAttrIdx = self.geometry.yAttrIndexSpinBox.value()
            options.prevs_zAttrIdx = self.geometry.zAttrIndexSpinBox.value()
            options.prevs_xCompIdx = self.geometry.xCompIndexSpinBox.value()
            options.prevs_yCompIdx = self.geometry.yCompIndexSpinBox.value()
            options.prevs_zCompIdx = self.geometry.zCompIndexSpinBox.value()

        # Texturing parameters
        texComboIdx = self.textureCombo.currentIndex()
        if 0 == texComboIdx: # Auto
            options.textureLoadMode = nrimp.TextureLoadMode.Auto
        elif 1 == texComboIdx: # Attribute Name
            options.textureLoadMode = nrimp.TextureLoadMode.AttributeName
            options.texAttrName_attribName = self.texturing.attribName.text()
            options.texAttrName_uCompIdx   = self.texturing.uCompIndexSpinBox.value()
            options.texAttrName_vCompIdx   = self.texturing.vCompIndexSpinBox.value()
            options.texAttrName_uvIdx      = self.texturing.uvIndexSpinBox.value()
        elif 2 == texComboIdx: # Attribute Index
            options.textureLoadMode = nrimp.TextureLoadMode.AttributeIndex
            options.texAttrIdx_uAttribIdx  = self.texturing.uvAttrIndexSpinBox.value()
            options.texAttrIdx_vAttribIdx  = self.texturing.uvAttrIndexSpinBox.value()
            options.texAttrIdx_uCompIdx    = self.texturing.uCompIndexSpinBox.value()
            options.texAttrIdx_vCompIdx    = self.texturing.vCompIndexSpinBox.value()
        elif 3 == texComboIdx: # Attribute Index
            options.textureLoadMode = nrimp.TextureLoadMode.AttributeIndex
            options.texAttrIdx_uAttribIdx  = self.texturing.uAttrIndexSpinBox.value()
            options.texAttrIdx_vAttribIdx  = self.texturing.vAttrIndexSpinBox.value()
            options.texAttrIdx_uCompIdx    = self.texturing.uCompIndexSpinBox.value()
            options.texAttrIdx_vCompIdx    = self.texturing.vCompIndexSpinBox.value()

        options.loadExtraUvData = False
        if self.checkBox.isChecked():
            options.loadExtraUvData = True

        return options

    @Slot()
    def importSlot(self):
        if len(self.fileNamesList):
            options = self.getOptions()
            options.dump()

            print("Import started")

            imp = nr3dsimp.MaxImporter(options)

            for fileName in self.fileNamesList:
                imp.importMesh(fileName)

            print("Import finished")
        else:
            print("No nr-files selected")

