# -*- coding: utf-8 -*-
"""
/***************************************************************************
 NetworkStorePluginDialog
                                 A QGIS plugin
  interacting with the KISTERS network store
 Generated by Plugin Builder: http://g-sherman.github.io/Qgis-Plugin-Builder/
                             -------------------
        begin                : 2022-01-18
        git sha              : $Format:%H$
        copyright            : (C) 2022 by Attila Bibok
        email                : Attila.bibok@kisters.net
 ***************************************************************************/

/***************************************************************************
 *                                                                         *
 *   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.                                   *
 *                                                                         *
 ***************************************************************************/
"""

import os

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

# from PyQt5 import QtWidgets, QtCore


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


class NetworkStorePluginDialog(QtWidgets.QDialog, FORM_CLASS_BASE):
    """Main Dialog to create a blank project"""

    def __init__(self, parent=None):
        """Constructor."""
        super(NetworkStorePluginDialog, 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)
        # set filter for filename
        self.mQgsFileWidget_geopackage.setFilter("Geopackage (*.gpkg)")

    #     # TODO: Create a method that parses _BaseLink and _BaseNode children classes
    #     node_class_list = [
    #         "Junction",
    #         "FlowBoundary",
    #         "LevelBoundary",
    #         "Storage",
    #     ]
    #     link_class_list = [
    #         "Channel",
    #         "Delay",
    #         "FlowControlledStructure",
    #         "Pipe",
    #         "Weir",
    #         "Drain",
    #         "Pump",
    #         "Turbine",
    #         "TopDownRectangularOrifice",
    #         "TopDownSphericalOrifice",
    #     ]

    #     # checkable tree view
    #     self.checkable_tree.setHeaderHidden(True)
    #     self.checkable_tree.setAlternatingRowColors(True)
    #     # create a root item
    #     self.checkable_tree.setHeaderLabels(["Model Library - Water"])
    #     node_item = QtWidgets.QTreeWidgetItem(["Nodes"])
    #     node_item.setFlags(node_item.flags() | QtCore.Qt.ItemIsUserCheckable)
    #     node_item.setCheckState(0, QtCore.Qt.Checked)
    #     link_item = QtWidgets.QTreeWidgetItem(["Links"])
    #     link_item.setFlags(link_item.flags() | QtCore.Qt.ItemIsUserCheckable)
    #     link_item.setCheckState(0, QtCore.Qt.Checked)
    #     self.checkable_tree.addTopLevelItem(node_item)
    #     self.checkable_tree.addTopLevelItem(link_item)

    #     # Populate the node group
    #     for node in node_class_list:
    #         item = QtWidgets.QTreeWidgetItem([node])
    #         item.setFlags(item.flags() | QtCore.Qt.ItemIsUserCheckable)
    #         item.setCheckState(0, QtCore.Qt.Checked)
    #         node_item.addChild(item)

    #     # Populate the link group
    #     for link in link_class_list:
    #         item = QtWidgets.QTreeWidgetItem([link])
    #         item.setFlags(item.flags() | QtCore.Qt.ItemIsUserCheckable)
    #         item.setCheckState(0, QtCore.Qt.Checked)
    #         link_item.addChild(item)

    # def get_checked_items(self):
    #     _checked_items = {}
    #     for i in range(self.checkable_tree.topLevelItemCount()):
    #         top_item = self.checkable_tree.topLevelItem(i)
    #         if top_item.checkState(0) == QtCore.Qt.Checked:
    #             _checked_list = []
    #             for j in range(top_item.childCount()):
    #                 child_item = top_item.child(j)
    #                 if child_item.checkState(0) == QtCore.Qt.Checked:
    #                     _checked_list.append(child_item.text(0))
    #             _checked_items[top_item.text(0)] = _checked_list
    #     return _checked_items

    # TODO: add a call to create an empty dummy network with just metadata(crs) and then call that network
    # TODO: just check if the network_id already exists or not. Local network is not supported for now. Maybe later.
