from production.model import Item
from typing import Any

from ._AllowedForbiddenFilter import AllowedForbiddenFilter
from ._OrderChangerProductionBox import OrderChangerProductionBox


def item_properties_retriever(item):
    # type: (Item) -> Any
    ret = []
    for p in item.properties:
        ret.append(p)

    return ret


class ItemPropertiesFilterBox(OrderChangerProductionBox):
    def __init__(self, name, sons, allowed_properties, forbidden_properties, allow_combos, logger=None):
        super(ItemPropertiesFilterBox, self).__init__(name, sons, logger)
        self.allowed_forbidden_filter = AllowedForbiddenFilter(
            allowed_values=allowed_properties,
            forbidden_values=forbidden_properties,
            keep_son_parts=allow_combos
        )

    def change_order(self, order):
        combo_without_sons = []
        for item in order.items:
            if item.is_combo() and not item.has_some_product_child():
                combo_without_sons.append(item.get_line_id())

        filtered_order = self.allowed_forbidden_filter.filter_items(order, item_properties_retriever)

        filtered_combos_order = []
        for item in filtered_order.items:
            if not item.is_combo():
                filtered_combos_order.append(item)
                continue

            if item.has_some_product_child() or item.get_line_id() in combo_without_sons:
                filtered_combos_order.append(item)

        filtered_order.items = filtered_combos_order
        return filtered_order
