from qgis.PyQt.QtWidgets import QDialog, QVBoxLayout, QTabWidget, QTextEdit, QGraphicsView, QGraphicsScene
from qgis.core import QgsProject, QgsMapLayer, QgsVectorLayer
from qgis.gui import QgsMapCanvas, QgsAttributeTableView
from qgis.utils import iface

class CombinedDialog(QDialog):
    def __init__(self, layer, parent=iface.mainWindow()):
        super().__init__(parent)
        self.layer = layer

        # Set the dialog layout
        self.setWindowTitle('Layer Information')
        layout = QVBoxLayout(self)

        # Create a QTabWidget to hold the different panels
        self.tabs = QTabWidget(self)

        # Add Metadata Panel (using QgsVectorLayer methods to get metadata)
        metadata_panel = QTextEdit(self)  # TextEdit for displaying metadata
        metadata_text = self.get_layer_metadata(self.layer)
        metadata_panel.setPlainText(metadata_text)  # Set the metadata text content
        self.tabs.addTab(metadata_panel, 'Metadata')

        # Add Preview Panel (using QgsMapCanvas in a QGraphicsView)
        preview_widget = QGraphicsView(self)
        preview_scene = QGraphicsScene(self)
        preview_widget.setScene(preview_scene)

        # Set up the map canvas for previewing the layer
        map_canvas = QgsMapCanvas()
        map_canvas.setCanvasColor('white')
        map_canvas.setDestinationCrs(self.layer.crs())  # Set the CRS of the map canvas
        map_canvas.setExtent(self.layer.extent())  # Set the extent to match the layer's extent
        map_canvas.setLayers([self.layer])  # Set the layer to be previewed
        map_canvas.refresh()  # Refresh the map canvas to show the layer

        # Add the map canvas to the preview scene
        preview_scene.addWidget(map_canvas)
        self.tabs.addTab(preview_widget, 'Preview')

        # Add Attribute Table Panel (using QgsAttributeTableView)
        attribute_table = QgsAttributeTableView()
        attribute_table.setLayer(self.layer)
        self.tabs.addTab(attribute_table, 'Attribute Table')

        # Add the QTabWidget to the layout
        layout.addWidget(self.tabs)

        # Set dialog size
        self.resize(800, 600)

    def get_layer_metadata(self, layer):
        """Return basic metadata information of the layer"""
        metadata = []

        # Get layer name
        metadata.append(f"Layer Name: {layer.name()}")

        # Get layer type
        metadata.append(f"Layer Type: {layer.type()}")

        # Get CRS (Coordinate Reference System)
        metadata.append(f"CRS: {layer.crs().authid()}")

        # Get feature count
        metadata.append(f"Feature Count: {layer.featureCount()}")

        # Get fields (attribute names)
        fields = ", ".join([field.name() for field in layer.fields()])
        metadata.append(f"Fields: {fields}")

        # Get extent
        metadata.append(f"Extent: {layer.extent().toString()}")

        return "\n".join(metadata)

    def showDialog(self):
        self.exec_()


# Usage:
# layer = iface.activeLayer()  # Get the active layer
layer = QgsVectorLayer(
            "dbname='real3d' host=localhost port=5432 user='postgres' password='postgres' sslmode=disable checkPrimaryKeyUnicity='1' table=\"vector\".\"ghdka\" (geom)",
            'test', 'postgres')
if layer:
    dialog = CombinedDialog(layer)
    dialog.showDialog()
else:
    print("No layer selected")
