from production.model import ProdStates
from typing import List, Dict

from ._TagLinesUndoCommandType import TagLinesUndoCommandType
from ._UndoCommand import UndoCommand


class TagLinesUndoCommand(UndoCommand):
    def __init__(self, order_id, view, tag, lines, tag_type, disabled_commands, line_ids_jit_lines):
        # type: (str, str, str, List[str], TagLinesUndoCommandType, List[UndoCommand], Dict) -> None

        super(TagLinesUndoCommand, self).__init__(order_id, view)
        self.tag = tag
        self.lines = lines
        self.type = tag_type
        self.disabled_commands = disabled_commands
        self.line_ids_jit_lines = line_ids_jit_lines

    def undo(self, order):
        for line_id in self.lines:
            if self.type == TagLinesUndoCommandType.add:
                order.remove_tag(line_id, self.tag, self.line_ids_jit_lines.get(line_id))
            else:
                order.toggle_tag(line_id, self.tag, self.line_ids_jit_lines.get(line_id))

            if self.tag in ['served', 'done']:
                order.prod_state = ProdStates.NORMAL
                order.remove_tag(line_id, "printed", self.line_ids_jit_lines.get(line_id))
                order.remove_tag(line_id, "pick-printed", self.line_ids_jit_lines.get(line_id))

            if self.tag == 'done':
                order.remove_tag(line_id, "served", self.line_ids_jit_lines.get(line_id))

        for undo_command in self.disabled_commands:
            undo_command.enable()
