import socket

from bclearer_core.infrastructure.session.common.session_identity_vectors import (
    OperatingSystemObjectIdentityVector,
    OperatingSystemObjectIdentityVectorPlaces,
)
from bclearer_core.infrastructure.session.common.session_objects import (
    SessionObjects,
)
from bclearer_core.infrastructure.session.operating_system.helpers.bie_operating_system_type_getter import (
    get_bie_operating_system_type,
)
from bclearer_core.infrastructure.session.operating_system.helpers.machine_sid_getter import (
    get_machine_sid,
)
from bclearer_orchestration_services.identification_services.b_identity_ecosystem.bie_id_creation_module.identity_vectors import (
    BieIdentityVectorBase,
)


class OperatingSystemObjects(
    SessionObjects
):
    _instance = None

    def __new__(cls):
        if not cls._instance:
            cls._instance = super(
                OperatingSystemObjects,
                cls,
            ).__new__(cls)

        return cls._instance

    def __init__(self):
        if getattr(
            self,
            "_is_initialized",
            False,
        ):
            return

        self.computer_name = (
            socket.gethostname()
        )

        self.machine_sid = (
            get_machine_sid()
        )

        self.bie_operating_system_type = (
            get_bie_operating_system_type()
        )

        identity_vector = (
            self._create_vector()
        )

        super().__init__(
            identity_vector=identity_vector,
        )

        self._is_initialized = True

    def _create_vector(
        self,
    ) -> BieIdentityVectorBase:
        vector_places = (
            self._create_vector_places()
        )

        operating_system_identity_vector = OperatingSystemObjectIdentityVector(
            bie_type=self.bie_operating_system_type,
            bie_hr_name=self.computer_name,
            places=vector_places,
        )

        return operating_system_identity_vector

    def _create_vector_places(
        self,
    ) -> OperatingSystemObjectIdentityVectorPlaces:
        operating_system_identity_vector_places = OperatingSystemObjectIdentityVectorPlaces(
            operating_system_type_bie_id=self.bie_operating_system_type.item_bie_identity,
            machine_sid=self.machine_sid,
        )

        return (
            operating_system_identity_vector_places
        )
