from logging import Logger
from typing import (
    List,
    Optional,
)

from mw_helper import (
    convert_to_dict,
    ensure_list,
)

from production.box._OrderChangerProductionBox import OrderChangerProductionBox
from production.model import (
    Item,
    ProductionOrder,
)


class ComboFilterBox(OrderChangerProductionBox):
    def __init__(
        self,
        name,               # type: object
        sons,               # type: object
        items_to_keep,      # type: List
        inherit_comments,   # type: bool
        logger=None,        # type: Optional[Logger]
    ):
        # type: (...) -> None

        super(ComboFilterBox, self).__init__(name=name, sons=sons, logger=logger)
        self.items_to_keep = convert_to_dict(iterable=ensure_list(value=items_to_keep))
        self.inherit_comments = inherit_comments

    def change_order(
        self,
        order,  # type:  ProductionOrder
    ):
        # type: (...) -> ProductionOrder

        filtered_items = []
        for item in order.items:
            self.handle_item(
                item=item,
                filtered_items=filtered_items,
                levels_to_subtract=0,
            )

        order.items = filtered_items
        return order

    def handle_item(
        self,
        item,                   # type: Item
        filtered_items,         # type: List
        levels_to_subtract,     # type: int
        parent_qty=None,        # type: Optional[float]
    ):
        # type: (...) -> None

        if parent_qty is not None:
            item.qty *= parent_qty

        if item.item_type in ("PRODUCT", "CANADD"):
            if item.original_level is None:
                item.original_level = item.level
            item.level = int(item.level) - levels_to_subtract
            filtered_items.append(item)

        elif item.item_type in ("COMBO", "OPTION"):
            if item.item_type == "OPTION":
                filtered_items.append(item)
            else:
                if item.part_code in self.items_to_keep:
                    son_filtered_items = []
                    for son in item.items:
                        son.seat = item.seat
                        son.manual_course = item.manual_course
                        self.handle_item(
                            item=son,
                            filtered_items=son_filtered_items,
                            levels_to_subtract=levels_to_subtract,
                            parent_qty=item.qty,
                        )

                    item.items = son_filtered_items
                    if son_filtered_items:
                        filtered_items.append(item)
                else:
                    for son in item.items:
                        son.seat = item.seat
                        son.manual_course = item.manual_course
                        if self.inherit_comments:
                            son.repeat_comment_from_father(father=item)
                        self.handle_item(
                            item=son,
                            filtered_items=filtered_items,
                            levels_to_subtract=levels_to_subtract + 1,
                            parent_qty=item.qty,
                        )
