from typing import (
    Dict,
    List,
    Optional,
    Union,
)

from ._RuptureEventTypeDto import RuptureEventTypeDto


class RuptureEventDto(object):
    def __init__(
        self,                       # type: RuptureEventDto
        id,                         # type: int
        type,                       # type: RuptureEventTypeDto
        user_id,                    # type: Optional[int]
        enabled,                    # type: List[int]
        main_disabled_part_codes,   # type: List[int]
        affected_items,             # type: Dict[int, List[int]]
    ):
        # type: (...) -> None

        self.id = id
        self.type = type
        self.user_id = user_id
        self.enabled = enabled
        self.main_disabled_part_codes = main_disabled_part_codes
        self.affected_items = affected_items

    def to_dict(
        self,   # type: RuptureEventDto
    ):
        # type: (...) -> Dict[str, Union[str, int, List[int], Dict[int, List[int]]]]

        return {
            "id": self.id,
            "type": self.type.name,
            "userId": self.user_id,
            "enabled": self.enabled,
            "affectedItems": self.affected_items,
            "mainDisabledPartCodes": self.main_disabled_part_codes,
        }

    @staticmethod
    def build_from_dict(
        data_dict,  # type: Dict
    ):
        # type: (...) -> RuptureEventDto

        return RuptureEventDto(
            id=data_dict["id"],
            type=RuptureEventTypeDto(data_dict["type"]),
            user_id=data_dict.get("userId"),
            enabled=data_dict["enabled"],
            main_disabled_part_codes=data_dict["mainDisabledPartCodes"],
            affected_items={int(key): value for key, value in data_dict["affectedItems"].items()},
        )
