from PySide6.QtCore import QAbstractListModel, Qt, Slot, QModelIndex
from .AddressDAO import AddressDAO
from ..PyqcrmDataRoles import PyqcrmDataRoles

class AddressModel(QAbstractListModel):

    def __init__(self):
        super().__init__()
        self.__address_data = AddressDAO().getAddressData()

    def rowCount(self, parent = QModelIndex()):
        return len(self.__address_data)

    def data(self, index, role = Qt.DisplayRole):
        row = index.row()
        if role == Qt.DisplayRole:
            data = self.__address_data[row][2]
            return data
        elif role == PyqcrmDataRoles.CITY_ROLE:
            data = self.__address_data[row][3]
            return data
        return None

    def roleNames(self):
        return {
            Qt.DisplayRole: b"display",
            PyqcrmDataRoles.CITY_ROLE: b"city",
        }

    def setData(self):
        pass

    @Slot(bool, str)
    def getAddresses(self, all, zipcode):
        data = AddressDAO().getAddressData(all, zipcode)
        return data






