from typing import Dict
from yaak import inject
import paginate

from Src.BioAnalyzer.CrossCutting.DTOs.GenePrioritization.DataIntegrationDto import DataIntegrationDto
from Src.BioAnalyzer.CrossCutting.Filters.GenePrioritization.FeSingleDataIntegration import FeSingleDataIntegration
from Src.BioAnalyzer.DataAccess.Entities.GenePrioritization.DataIntegration import DataIntegration
from Src.Core.Manager.ManagerBase import ManagerBase


class DataIntegratorManager(ManagerBase):
    """description of class"""

    @inject.Param(repository='DataIntegrationRepositoryBase')
    def __init__(self, repository):
        """

        :param repository: 
        """
        super().__init__(repository)
        self.__page_size = 10

    def add_one(self, data_integration_dto: DataIntegrationDto):
        fe_data_integration = self._repository.get_one(FeSingleDataIntegration(data_type=data_integration_dto.data_type,
                                                                               conditional=data_integration_dto.conditional),
                                                       DataIntegration,
                                                       {'data_type': 1})

        if  fe_data_integration.result:
            self._repository.delete_one(fe_data_integration)

        adj_list = data_integration_dto.adjacency_list

        page = paginate.Page(adj_list, page=0, items_per_page=self.__page_size)

        while True:

            if not page.next_page:
                break

            data_integration_dto.adjacency_list = page.items
            self._repository.add_one(data_integration_dto)

            page = paginate.Page(adj_list, page=page.next_page, items_per_page=self.__page_size)

    def get_one(self, fe_data_integration: FeSingleDataIntegration,
                include_or_exclude_fields: Dict[str, int] = None) -> FeSingleDataIntegration:
        """

        :param fe_data_integration: 
        :param include_or_exclude_fields: 
        :return: 
        """
        return self._repository.get_one(fe_data_integration,
                                        DataIntegration,
                                        include_or_exclude_fields)