from parallels.core.application.asp_net import AspNetModel
from parallels.core.application.drupal import DrupalModel
from parallels.core.application.joomla import JoomlaModel
from parallels.core.application.magento import MagentoModel
from parallels.core.application.prestashop import PrestashopModel
from parallels.core.application.wordpress import WordpressModel


def get_application_models(filter_name=None, filter_database_type=None):
    """Retrieve configs of supported applications by given filter

    :type filter_name: list[str] | None
    :type filter_database_type: list[str] | None
    :rtype: list[parallels.core.application.base.ApplicationModel]
    """
    application_models = [
        DrupalModel(), JoomlaModel(), MagentoModel(), PrestashopModel(), WordpressModel(), AspNetModel()
    ]
    if filter_name is not None and len(filter_name) == 0:
        # filter by name is empty, so nothing could be retrieved
        return []
    if filter_database_type is not None and len(filter_database_type) == 0:
        # filter by database type is empty, so nothing could be retrieved
        return []
    result = []
    for model in application_models:
        if filter_name is not None:
            if model.name.lower() not in [x.lower() for x in filter_name]:
                continue
        if filter_database_type is not None:
            assert isinstance(filter_database_type, list)
            is_supported = False
            for database_type in filter_database_type:
                if model.is_database_type_supported(database_type):
                    is_supported = True
            if not is_supported:
                continue
        result.append(model)
    return result


def get_application_model(name):
    models = get_application_models(filter_name=[name])
    if len(models) == 0:
        return None
    return models.pop()
