# **************************************************************************
# *                                                                        *
# *  Copyright (C) Golaem S.A. - All Rights Reserved.                      *
# *                                                                        *
# **************************************************************************

import os
from ..Qtpy.Qt import QtCore, QtGui, QtWidgets

# **********************************************************************
#
# MultiSelectListDialog
#
# **********************************************************************
class MultiSelectListDialog(QtWidgets.QDialog):
    # ------------------------------------------------------------------
    def __init__(self, parent=None):
        super(MultiSelectListDialog, self).__init__(parent)

        self.multiSelectListWidget = MultiSelectListWidget(self)
        if(not self.multiSelectListWidget.okCancelButtons is None):
            self.multiSelectListWidget.okCancelButtons.accepted.connect(self.accept)
            self.multiSelectListWidget.okCancelButtons.rejected.connect(self.reject)

        self.setWindowTitle('Select values')
        vertLayout = QtWidgets.QVBoxLayout()
        vertLayout.addWidget(self.multiSelectListWidget)
        self.setLayout(vertLayout)

    def exec_(self):
        self.multiSelectListWidget.updateUI()
        return super(MultiSelectListDialog, self).exec_()

# **********************************************************************
#
# MultiSelectListWidget
#
# **********************************************************************
class MultiSelectListWidget(QtWidgets.QWidget):
    # ------------------------------------------------------------------
    def __init__(self, parent=None):
        super(MultiSelectListWidget, self).__init__(parent)

        # Data Members
        self.availableValues = []
        self.selectedValuesIndices = []
        self.okCancelButtons = None

        # Init
        self.initUI()

    # ------------------------------------------------------------------
    def initUI(self):
        #iconsDir = wrapper.WindowMayaWrapper().getIconsDir()
        self.setWindowTitle('Select values')

        # Main layout
        self.mainLayout = QtWidgets.QVBoxLayout(self)
        self.mainLayout.setContentsMargins(0, 0, 0, 0)
        self.mainLayout.setSpacing(0)
        self.setLayout(self.mainLayout)

        # search label and line...
        valueSearchLabel = QtWidgets.QLabel(self)
        valueSearchLabel.setText('Search: ')
        valueLineEdit = QtWidgets.QLineEdit(self)
        valueLineEdit.textChanged.connect(self.onSearchLineEditChanged)
        #... and layout
        searchLayout = QtWidgets.QHBoxLayout()
        searchWidget = QtWidgets.QWidget(self)
        searchWidget.setLayout(searchLayout)
        searchLayout.addWidget(valueSearchLabel)
        searchLayout.addWidget(valueLineEdit)

        # values list...
        self.valuesListWidget = QtWidgets.QListWidget(self)
        self.valuesListWidget.setSelectionMode(QtWidgets.QAbstractItemView.ExtendedSelection)
        self.valuesListWidget.itemSelectionChanged.connect(self.onValueSelectionChanged)

        #... and layout
        self.mainLayout.addWidget(searchWidget)
        self.mainLayout.addWidget(self.valuesListWidget)

        # OK Cancel buttons...
        self.okCancelButtons = QtWidgets.QDialogButtonBox(self)
        self.okCancelButtons.orientation = QtCore.Qt.Horizontal
        self.okCancelButtons.setStandardButtons(QtWidgets.QDialogButtonBox.Ok|QtWidgets.QDialogButtonBox.Cancel)
        #... and layout
        bottomButtonsLayout = QtWidgets.QHBoxLayout()
        bottomButtonsLayout.addWidget(self.okCancelButtons)
        self.mainLayout.addLayout(bottomButtonsLayout)
        
        self.updateUI()

    # ------------------------------------------------------------------
    def updateUI(self):
        savedSelectedValuesIndices = list(self.selectedValuesIndices)
        #fill available values
        self.valuesListWidget.clear()
        for value in self.availableValues:
            self.valuesListWidget.addItem(value)
        #fill additional values when selection is higher than the max list
        maxValue = -1
        for iCurrentSel in savedSelectedValuesIndices:
            maxValue = max(maxValue, iCurrentSel)
        if (maxValue > -1):
            for iOutOfRangeAsset in range(len(self.availableValues), maxValue + 1):
                self.valuesListWidget.addItem("Value {} out of range".format(iOutOfRangeAsset))
        #set current selection
        for i in savedSelectedValuesIndices:
           self.valuesListWidget.item(i).setSelected(True)
        
    # ------------------------------------------------------------------
    def onValueSelectionChanged(self):
        self.selectedValuesIndices = []
        for selectedItem in self.valuesListWidget.selectedItems():
            self.selectedValuesIndices.append((self.valuesListWidget.row(selectedItem)))

    # ------------------------------------------------------------------
    def onSearchLineEditChanged(self, value=None):
        widgetEdited = self.sender()  # QLineEdit, QTextEdit
        tableWidget = widgetEdited.parent().parent()  # QDialog
        for childIndex in range(len(tableWidget.children())):
            if(type(tableWidget.children()[childIndex]) == type(QtWidgets.QListWidget())):
                self.updateValuesListFromSearch(tableWidget.children()[childIndex], value)

    # ------------------------------------------------------------------
    def updateValuesListFromSearch(self, listWidget, searchValue):
        for iItem in range(listWidget.count()):
            listWidget.item(iItem).setHidden(searchValue is not None and (listWidget.item(iItem).text().lower().find(searchValue.lower()) == -1))

