from logging import Logger

from typing import (
    List, Optional,
)

from .._OrderChangerProductionBox import OrderChangerProductionBox
from production.model import ProductionOrder


class PrepTimeOrderSplitter(OrderChangerProductionBox):
    def __init__(
        self,                   # type: PrepTimeOrderSplitter
        name,                   # type: str
        sons,                   # type: object
        logger=None,            # type: Optional[Logger]
    ):
        # type: (...) -> None

        super(PrepTimeOrderSplitter, self).__init__(name, sons, logger)

    def change_order(
        self,       # type: PrepTimeOrderSplitter
        order,      # type: ProductionOrder
    ):
        # type: (...) -> List[ProductionOrder]

        orders = []
        split_index = 0

        for item in order.items:
            for temp_order in orders:
                if len(temp_order.items) > 0 and item.prep_time == temp_order.items[0].prep_time:
                    temp_order.items.append(item)
                    break
            else:
                new_order = order.clone([item], split_index)
                orders.append(new_order)
                split_index += 1

        return orders
