import sys

from PyQt6.QtCore import QSortFilterProxyModel
from PyQt6.QtWidgets import QWidget, QApplication, QListWidget, QVBoxLayout, QHBoxLayout, QGridLayout

from power_qt.pandas_models.basic_table_models import BasicPandasTableModel
import pandas as pd

from power_qt.tableviews import reTableView


class FilterModel(QWidget):
    _proxy_Base = None
    _dynamic_filters = []

    def __init__(self, parent=None):
        super().__init__(parent)
        self.search_website = QListWidget()
        self.search_priority = QListWidget()
        self.search_name = QListWidget()

        self.createProxies(BasicPandasTableModel(),
                           [(0, self.search_website),
                            (2, self.search_priority),
                            (1, self.search_name)])

    def createProxies(self, model, filtering_widgets):
        """Filtering Widgets as a list of tuples containing (int, widget) pairs"""
        if len(filtering_widgets) > 25:
            raise AttributeError

        self._proxy_Base = model

        self._dynamic_filters.clear()

        for i, (filterColumn, connected_widget) in enumerate(filtering_widgets):
            proxy_name = f'_proxy_{chr(65 + i)}'
            print(proxy_name)

            setattr(self, proxy_name, QSortFilterProxyModel())
            proxy = getattr(self, proxy_name)

            self._dynamic_filters.append((proxy_name, connected_widget))

            proxy.setFilterKeyColumn(filterColumn)  # Search all columns.
            proxy.setSourceModel(getattr(self, '_proxy_Base'))

        self.FinalModel = getattr(self, self._dynamic_filters[-1][0])

    def apply_filter(self):
        for proxy, field in self._dynamic_filters:
            if not field.selectedItems():
                proxy.setFilterFixedString('')  # clearing filter if none is selected
            else:
                _search = '|'.join([item.text() for item in field.selectedItems()])
                proxy.setFilterRegularExpression(_search)  # applying filter


class testFilteringTable(QWidget):

    def __init__(self, parent=None):
        super().__init__(parent)
        self._table = reTableView()

        self.main_layout = QHBoxLayout()
        self._filters_layout = QGridLayout()

        # self.main_layout.addLayout(self._filters_layout)
        self.main_layout.addWidget(self._table)

        self.setLayout(self.main_layout)

        self._filters = []

    def setTable(self, table,):
        self._table.update_table(table)


if __name__ == '__main__':
    df = pd.DataFrame({'num_legs': [2, 4, 8, 0],
                       'num_wings': [2, 0, 0, 0],
                       'num_specimen_seen': [10, 2, 1, 8]},
                      index=['falcon', 'dog', 'spider', 'fish'])

    app_main = QApplication(sys.argv)  # load main window
    win = testFilteringTable()
    win.setWindowTitle('Test Filtering Table')
    win.setTable(df)
    win.show()

    try:
        sys.exit(app_main.exec())
    except SystemExit:
        print('Closing Window...')
