from mw_helper import ensure_list

from production.box._OrderChangerProductionBox import OrderChangerProductionBox
from production.model import create_states_from_state_without_voids


class FromOrderStateFilterBox(OrderChangerProductionBox):
    def __init__(self, name, sons, states, logger=None):
        super(FromOrderStateFilterBox, self).__init__(name, sons, logger)

        self.states = []
        for state in ensure_list(states):
            states_from_state_without_voids = create_states_from_state_without_voids(state)
            for new_state in states_from_state_without_voids:
                if new_state not in self.states:
                    self.states.append(new_state)

    def change_order(self, order):
        state_history = [x.state for x in order.state_history]
        for state in self.states:
            if state in state_history:
                return order

        return None
