"""
***************************************************************************
    GdalAlgorithmDialog.py
    ---------------------
    Date                 : May 2015
    Copyright            : (C) 2015 by Victor Olaya
    Email                : volayaf at gmail dot com
***************************************************************************
*                                                                         *
*   This program 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.                                   *
*                                                                         *
***************************************************************************
"""

__author__ = 'Victor Olaya'
__date__ = 'May 2015'
__copyright__ = '(C) 2015, Victor Olaya'

import json
import os

from PyQt5.QtWidgets import QFileDialog

from qgis.PyQt.QtCore import QCoreApplication
from qgis.PyQt.QtWidgets import (QWidget,
                                 QVBoxLayout,
                                 QPushButton,
                                 QLabel,
                                 QPlainTextEdit,
                                 QLineEdit,
                                 QComboBox,
                                 QCheckBox,
                                 QTreeView,
                                 QTreeWidget,
                                 QTreeWidgetItem,
                                 QSizePolicy,
                                 QDialogButtonBox)

from qgis.core import (
    QgsProcessingException,
    QgsProcessingFeedback,
    QgsProcessingParameterDefinition
)
from qgis.gui import (QgsMessageBar,
                      QgsProjectionSelectionWidget,
                      QgsProcessingAlgorithmDialogBase,
                      QgsProcessingLayerOutputDestinationWidget)

from processing.gui.AlgorithmDialog import AlgorithmDialog
from processing.gui.AlgorithmDialogBase import AlgorithmDialogBase
from processing.gui.ParametersPanel import ParametersPanel
from processing.gui.MultipleInputPanel import MultipleInputPanel
from processing.gui.NumberInputPanel import NumberInputPanel
from processing.gui.wrappers import WidgetWrapper
from processing.tools.dataobjects import createContext
from processing.tools.Widget.TreeWidget import Tree
from processing.tools.FileListPrintUtils import printStr


class GdalAlgorithmDialog(AlgorithmDialog):

    def __init__(self, alg, parent=None):
        super().__init__(alg, parent=parent)
        self.mainWidget().parametersHaveChanged()

    def getParametersPanel(self, alg, parent):
        return GdalParametersPanel(parent, alg)


class GdalParametersPanel(ParametersPanel):

    def __init__(self, parent, alg):
        super().__init__(parent, alg)

        self.dialog = parent
        algname = alg.name()
        w = QWidget()
        layout = QVBoxLayout()
        layout.setMargin(0)
        layout.setSpacing(6)

        gdalHidden = True
        loop = True

        if algname == "postgistogeoserver":
            # 资源目录
            label = QLabel()
            label.setText(self.tr("资源目录树（必选）"))
            layout.addWidget(label)
            tree = Tree("zyml")
            self.curTree = tree.initTreeWidget()
            layout.addWidget(self.curTree)
            self.curTree.itemSelectionChanged.connect(self.parametersHaveChanged)
            gdalHidden = True
        elif algname == "importvectorintopostgisdatabaseavailableconnections" or algname == "importrasterintopostgisdatabase" or algname == "gdbtopostgis":
            # 行政区划
            label = QLabel()
            label.setText(self.tr("行政区划（必选）"))
            layout.addWidget(label)
            tree = Tree("xzqh")
            self.curTree = tree.initTreeWidget()
            layout.addWidget(self.curTree)
            self.curTree.itemSelectionChanged.connect(self.parametersHaveChanged)
            # 上传附件
            pushBtn = QPushButton()
            pushBtn.setText("选择附件")
            pushBtn.clicked.connect(self.uploadChange)
            layout.addWidget(pushBtn)
            self.filelist = QPlainTextEdit()
            self.filelist.setReadOnly(True)
            layout.addWidget(self.filelist)
            gdalHidden = True
        elif algname == "postgisupdate":
            label = QLabel()
            label.setText(self.tr("预分析结果"))
            layout.addWidget(label)
            self.vectorupdate = QPlainTextEdit()
            self.vectorupdate.setReadOnly(True)
            layout.addWidget(self.vectorupdate)
            gdalHidden = True
            loop = False
        elif algname == "postgisrestore" or algname == "spotfileupload" or algname == "spotfiledownload" or algname == "spotfiledelete" or algname == "licensemake":
            gdalHidden = True
            loop = False

        label = QLabel()
        label.setText(self.tr("控制台"))
        label.setHidden(gdalHidden)
        layout.addWidget(label)
        self.text = QPlainTextEdit()
        self.text.setReadOnly(True)
        self.text.setHidden(gdalHidden)
        layout.addWidget(self.text)

        w.setLayout(layout)
        self.addExtraWidget(w)

        self.connectParameterSignals(loop=loop)
        self.parametersHaveChanged()

    def uploadChange(self):
        file_paths, _ = QFileDialog.getOpenFileNames(self, '选择文件', '', 'All Files (*)')
        fileliststr = ",".join(file_paths)
        self.filelist.setPlainText(fileliststr)
        printStr(fileliststr)

    def connectParameterSignals(self, loop):
        for wrapper in list(self.wrappers.values()):
            wrapper.widgetValueHasChanged.connect(self.parametersHaveChanged)
            # TODO - remove when all wrappers correctly emit widgetValueHasChanged!
            # For compatibility with 3.x API, we need to check whether the wrapper is
            # the deprecated WidgetWrapper class. If not, it's the newer
            # QgsAbstractProcessingParameterWidgetWrapper class
            # TODO QGIS 4.0 - remove
            if issubclass(wrapper.__class__, WidgetWrapper):
                w = wrapper.widget
            else:
                w = wrapper.wrappedWidget()
            if loop == True:
                self.connectWidgetChangedSignals(w)
                for c in w.findChildren(QWidget):
                    self.connectWidgetChangedSignals(c)

    def connectWidgetChangedSignals(self, w):
        if isinstance(w, QLineEdit):
            w.textChanged.connect(self.parametersHaveChanged)
        elif isinstance(w, QComboBox):
            w.currentIndexChanged.connect(self.parametersHaveChanged)
        elif isinstance(w, QgsProjectionSelectionWidget):
            w.crsChanged.connect(self.parametersHaveChanged)
        elif isinstance(w, QCheckBox):
            w.stateChanged.connect(self.parametersHaveChanged)
        elif isinstance(w, MultipleInputPanel):
            w.selectionChanged.connect(self.parametersHaveChanged)
        elif isinstance(w, NumberInputPanel):
            w.hasChanged.connect(self.parametersHaveChanged)
        elif isinstance(w, QgsProcessingLayerOutputDestinationWidget):
            w.destinationChanged.connect(self.parametersHaveChanged)
        elif isinstance(w, QTreeWidget):
            w.itemSelectionChanged.connect(self.parametersHaveChanged)

    def parametersHaveChanged(self):
        context = createContext()
        feedback = QgsProcessingFeedback()
        try:
            # messy as all heck, but we don't want to call the dialog's implementation of
            # createProcessingParameters as we want to catch the exceptions raised by the
            # parameter panel instead...
            parameters = {} if self.dialog.mainWidget() is None else self.dialog.mainWidget().createProcessingParameters()
            for output in self.algorithm().destinationParameterDefinitions():
                if not output.name() in parameters or parameters[output.name()] is None:
                    if not output.flags() & QgsProcessingParameterDefinition.FlagOptional:
                        parameters[output.name()] = self.tr("[temporary file]")
            for p in self.algorithm().parameterDefinitions():
                if p.flags() & QgsProcessingParameterDefinition.FlagHidden:
                    continue
                if p.flags() & QgsProcessingParameterDefinition.FlagOptional and p.name() not in parameters:
                    continue
                if p.name() not in parameters or not p.checkValueIsAcceptable(parameters[p.name()]):
                    # not ready yet
                    self.text.setPlainText('')
                    return
            try:
                b = self.contains_keys(self, ["curTree"])
                if b == True:
                    # 获取选中的项
                    selectedItems = self.curTree.selectedItems()
                    if len(selectedItems) == 0:
                        self.algorithm().setSelectedValue("")
                    else:
                        for selectedItem in selectedItems:
                            v = selectedItem.text(1)
                            self.algorithm().setSelectedValue(v)
                commands = self.algorithm().getConsoleCommands(parameters, context, feedback, executing=False)
                b = self.contains_keys(self, ["vectorupdate"])
                if b == True:
                    if len(commands) == 0:
                        self.vectorupdate.setPlainText("更新数据包通过预分析，可进行数据更新！")
                    else:
                        self.vectorupdate.setPlainText("\n".join(commands))
                commands = [c for c in commands if c not in ['cmd.exe', '/C ']]
                self.text.setPlainText(" ".join(commands))
            except QgsProcessingException as e:
                self.text.setPlainText(str(e))
        except AlgorithmDialogBase.InvalidParameterValue as e:
            self.text.setPlainText(self.tr("Invalid value for parameter '{0}'").format(e.parameter.description()))
        except AlgorithmDialogBase.InvalidOutputExtension as e:
            self.text.setPlainText(e.message)

    def contains_keys(self, obj, keys):
        if isinstance(obj, dict):
            return all(key in obj.keys() for key in keys)
        elif hasattr(type(obj), '__dict__'):
            return all(key in obj.__dict__ for key in keys)
        else:
            raise ValueError("Invalid object type")
