# Generated by sila2.code_generator; sila2.__version__: 0.12.2
from __future__ import annotations

import uuid
from calendar import LocaleTextCalendar
from typing import TYPE_CHECKING

from sila2.framework.data_types.any import SilaAnyType
from sila2.server import MetadataDict

from ..generated.labware import (
    DeleteLabware_Responses,
    LabwareBase,
    MoveLabware_Responses,
    RegisterLabware_Responses,
)

if TYPE_CHECKING:
    from ..server import Server

from ..base import Base

from ..labware.labware_wrapper import LabwareWrapper


class LabwareImpl(LabwareBase):
    def __init__(self, parent_server: Server, base: Base) -> None:
        super().__init__(parent_server=parent_server)
        self.base = base

    def RegisterLabware(
            self, SessionIdentifier: str, LoadName: str, Location: str, *,
            metadata: MetadataDict
    ) -> RegisterLabware_Responses:
        if self.base.get_session_id() != SessionIdentifier:
            raise "Wrong Identifier"

        new_item_identifier = str(uuid.uuid4())

        if Location.isdigit():
            new_labware = self.base.protocol.load_labware(LoadName,
                                                          int(Location),
                                                          new_item_identifier)
        else:
            new_labware = self.base.protocol.load_labware(LoadName, Location,
                                                          new_item_identifier)

        self.base.add_labware(LabwareWrapper(new_labware, Location),
                              new_item_identifier)
        return RegisterLabware_Responses(ItemIdentifier=new_item_identifier)

    def DeleteLabware(
            self, SessionIdentifier: str, Identifier: str, *,
            metadata: MetadataDict
    ) -> DeleteLabware_Responses:
        if self.base.get_session_id() != SessionIdentifier:
            raise "Wrong Identifier"

        if uuid.UUID(Identifier).version == 4:
            labware = self.base.get_labware(Identifier)
        else:
            labware = self.base.get_labware_by_position(Identifier)

        for key, value in self.base.protocol.loaded_labwares.items():
            if value == labware:
                del self.base.protocol.loaded_labwares[key]
        self.base.delete_labware(Identifier)
        return DeleteLabware_Responses()

    def MoveLabware(
            self, SessionIdentifier: str, ItemIdentifier: str, Location: str, *,
            metadata: MetadataDict
    ) -> MoveLabware_Responses:
        if self.base.get_session_id() != SessionIdentifier:
            raise "Wrong Identifier"
        for key, value in self.base.labwares.items():
            if key == ItemIdentifier:
                if Location.isdigit():
                    self.base.protocol.move_labware(labware=value.labware,
                                                    new_location=int(Location),
                                                    use_gripper=True)
                else:
                    self.base.protocol.move_labware(labware=value.labware,
                                                    new_location=Location,
                                                    use_gripper=True)

        return MoveLabware_Responses()
