import uuid
from dataclasses import dataclass
from typing import Literal

from opentrons.protocol_api.protocol_context import ModuleTypes
from opentrons.protocol_engine import ModuleModel
from opentrons.types import DeckSlotName
from opentrons_shared_data.module.types import ModuleType

from ..data import StaticItemsDict, DynamicItemsDict


@dataclass
class ModuleData:
    module_name: str
    """Module Type used for loading the module"""
    location: str
    """Location on the OT deck the module is loaded on"""
    child: uuid.UUID | None = None
    """Item identifier of child labware if applicable"""

    def update(self, instance: ModuleTypes, static_items: StaticItemsDict):
        self.child = static_items.get_generic_id_by_reference(instance.labware)

    @staticmethod
    def from_instance(instance: ModuleTypes, static_items: StaticItemsDict):
        data = ModuleData(module_name=instance.model, location=instance.parent, )
        data.update(instance, static_items)
        return data


@dataclass
class ModuleWrapper:
    module_instance: ModuleTypes | None
    """The instance used for usage in the Opentrons API"""
    data: ModuleData
    """Data needed to instantiate the instance"""

    def __eq__(self, other):
        if isinstance(other, ModuleWrapper):
            return self.module_instance == other.module_instance
        if isinstance(other, ModuleTypes):
            return self.module_instance == other
        return False

    @staticmethod
    def from_instance(instance: ModuleTypes, static_items: StaticItemsDict):
        data = ModuleData.from_instance(instance, static_items)
        data.update(instance, static_items)
        return ModuleWrapper(module_instance=instance,
                             data=data)
