from PyQt5 import QtCore, QtWidgets, QtGui

from ..structures.structureddataframe import StructuredDataFrame


class PandasModel(QtCore.QAbstractTableModel):
    """
    Class to populate a table view with a pandas dataframe
    """

    def __init__(self, data, parent=None):
        QtCore.QAbstractTableModel.__init__(self, parent)
        self._data = data

    def rowCount(self, parent=None):
        return len(self._data.values)

    def columnCount(self, parent=None):
        return self._data.columns.size

    def data(self, index, role=QtCore.Qt.DisplayRole):
        if index.isValid():
            if role == QtCore.Qt.DisplayRole:
                return str(self._data.values[index.row()][index.column()])
        return None

    def headerData(self, col, orientation, role):
        if orientation == QtCore.Qt.Horizontal and role == QtCore.Qt.DisplayRole:
            return str(self._data.columns[col])
        return None


def viewDataFrame(df: StructuredDataFrame):
    """
    Construct a TableView Dialog to view DataFrameContents)

    Parameters
    ----------
    df : StructuredDataFrame
    """

    # TODO: Implement Copy Function

    dialog = QtWidgets.QDialog(None)
    layout = QtWidgets.QVBoxLayout(dialog)
    layout.setContentsMargins(0, 0, 0, 0)
    view = QtWidgets.QTableView(None)
    view.setModel(PandasModel(df))
    layout.addWidget(view)
    dialog.exec_()
