# -*- coding: utf-8 -*-
"""
/***************************************************************************
 MirGradDTPDialog
                                 A QGIS plugin
 Набор инструментов для работы с материалами документации территориального планирования
 Generated by Plugin Builder: http://g-sherman.github.io/Qgis-Plugin-Builder/
                             -------------------
        begin                : 2024-03-18
        copyright            : (C) 2024 by I.Bukharin/Mirproekt
        email                : iabukharin@gmail.com
 ***************************************************************************/
"""

import os

from qgis.PyQt import uic
from qgis.PyQt import QtWidgets

# This loads your .ui file so that PyQt can populate your plugin with the elements from Qt Designer
FORM_CLASS, _ = uic.loadUiType(os.path.join(
    os.path.dirname(__file__), 'mirgrad_dtp_dialog_import.ui'))


class MirGradDTPDialogImport(QtWidgets.QDialog, FORM_CLASS):

    def __init__(self, parent=None):
        """Constructor."""
        super(MirGradDTPDialogImport, self).__init__(parent)
        # Set up the user interface from Designer through FORM_CLASS.
        # After self.setupUi() you can access any designer object by doing
        # self.<objectname>, and you can use autoconnect slots - see
        # http://qt-project.org/doc/qt-4.8/designer-using-a-ui-file.html
        # #widgets-and-dialogs-with-auto-connect
        self.setupUi(self)
        self.iface = None
        self.exceptLayerCommon = []
        self.exceptAddLayerClass = []
        self.mMapLayerComboBox.layerChanged.connect(self.destLayerChanged)
        self.checkBox.stateChanged.connect(self.relevantLayerStateChanged)
        self.fieldsView.changed.connect(self.fieldChanged)
    
    def fieldChanged(self):
        for fieldName, propertyDestField in self.fieldsView.fieldPropertyMap().items():
            if propertyDestField.propertyType() == 0:
                self.buttonBox.button(QtWidgets.QDialogButtonBox.Ok).setEnabled(False)
                break
            if propertyDestField.propertyType() == 3:
                if propertyDestField.asExpression() == '':
                    break
        else:
            self.buttonBox.button(QtWidgets.QDialogButtonBox.Ok).setEnabled(True)
    
    def destLayerChanged(self):
        if self.mMapLayerComboBox.currentLayer() is None:
             self.fieldsView.setEnabled(False)
             self.buttonBox.button(QtWidgets.QDialogButtonBox.Ok).setEnabled(False)
        else:
            self.fieldsView.setEnabled(True)
            self.fieldsView.setDestinationFields(self.mMapLayerComboBox.currentLayer().fields())
            for fieldName, propertyDestField in self.fieldsView.fieldPropertyMap().items():
                if propertyDestField.propertyType() == 0:
                    self.buttonBox.button(QtWidgets.QDialogButtonBox.Ok).setEnabled(False)
                    break
            else:
                self.buttonBox.button(QtWidgets.QDialogButtonBox.Ok).setEnabled(True)
        return
    
    def relevantLayerStateChanged(self):
        if self.checkBox.isChecked():
            self.mMapLayerComboBox.setExceptedLayerList(self.exceptLayerCommon + self.exceptAddLayerClass)
            self.destLayerChanged()
        else:
            self.mMapLayerComboBox.setExceptedLayerList(self.exceptLayerCommon)
        return