from PyQt5.QtWidgets import QTableView, QHeaderView, QAbstractItemView
from PyQt5.QtCore import Qt

STRETCH = QHeaderView.Stretch


class TableView(QTableView):

    def __init__(self, name, model, col_sizes=[], method_selected_row=None,
                 selection_edit=False):
        super(TableView, self).__init__()
        self.setObjectName(name)
        self.verticalHeader().hide()
        self.setGridStyle(Qt.DotLine)
        self.setAlternatingRowColors(True)
        if not selection_edit:
            self.setEditTriggers(QAbstractItemView.NoEditTriggers)
            self.setSelectionBehavior(QAbstractItemView.SelectRows)
            self.setSelectionMode(QAbstractItemView.SingleSelection)
        self.setVerticalScrollMode(QAbstractItemView.ScrollPerItem)
        self.model = model
        self.method_selected_row = method_selected_row
        self.doubleClicked.connect(self.on_selected_row)
        self.setWordWrap(False)

        if model:
            self.setModel(model)

        header = self.horizontalHeader()
        if col_sizes:
            for i, size in enumerate(col_sizes):
                if type(size) == int:
                    header.resizeSection(i, size)
                else:
                    header.setSectionResizeMode(i, STRETCH)

    def on_selected_row(self):
        selected_idx = self.currentIndex()
        if selected_idx:
            data_row = self.model.get_data(selected_idx)
            self.method_selected_row(data_row)

    def rowsInserted(self, index, start, end):
        # Adjust scroll to last row (bottom)
        self.scrollToBottom()

    def removeElement(self, index, ignore_focus=False):
        if not index:
            return
        if index.row() >= 0:
            if self.hasFocus() or ignore_focus:
                item = self.model.get_data(index)
                id_ = self.model.removeId(index.row(), index)
                self.model.deleteRecords([id_])
                self.model.layoutChanged.emit()
                return item

    def delete_item(self, ignore_focus=False):
        item_removed = {}
        selected_idx = self.currentIndex()
        item_removed = self.removeElement(selected_idx, ignore_focus)
        return item_removed

    def moved_selection(self, key):
        selected_idx = self.currentIndex()
        if key == Qt.Key_Down:
            self.selectRow(selected_idx.row() + 1)
        elif key == Qt.Key_Up:
            self.selectRow(selected_idx.row() - 1)
