import logging
from copy import deepcopy

from joystick_diagrams.input.device import Device_

_logger = logging.getLogger("__name__")


class Profile_:  # noqa: N801
    def __init__(self, profile_name: str):
        self.name: str = profile_name
        self.devices: dict[str, Device_] = {}

    def __repr__(self) -> str:
        return f"(Profile Object: {self.name})"

    def add_device(self, guid: str, name: str) -> Device_:
        guid = Device_.validate_guid(guid)

        if self.get_device(guid) is None:
            self.devices.update({guid: Device_(guid, name)})

        else:
            _logger.warning(f"Device {guid} already exists and will not be re-added")

        return self.get_device(guid)  # type: ignore

    def get_devices(self) -> dict[str, Device_] | None:
        return self.devices

    def get_device(self, guid: str) -> Device_ | None:
        return self.devices.get(guid)

    def merge_profiles(self, profile: "Profile_"):
        """Merge Profiles

        Merges the current OBJ with supplied Profile

        OBJ << PROFILE


        """
        src_profile = deepcopy(self)

        for guid, device in profile.devices.items():
            _logger.debug(f"Handling {guid=} and {device=}")
            if guid not in src_profile.devices:
                # If the device is not in the current profile, deepcopy the entire device
                _logger.debug(f"Device {guid=} not found so adding whole device")
                src_profile.devices[guid] = deepcopy(device)
            else:
                # If the device exists in the current profile, merge inputs
                existing_device = src_profile.devices[guid]
                _logger.debug(f"Existing device is {existing_device=}")
                for input_type, inputs in device.inputs.items():
                    _logger.debug(f"Processing {input_type} and {inputs=}")
                    for input_key, input_ in inputs.items():
                        _logger.debug(f"Processing {input_key} and {input_=}")
                        if input_key not in existing_device.inputs[input_type]:
                            _logger.debug(
                                f"Input key not found, so adding whole key {input_key=}"
                            )
                            # If the input is not in the existing device, deepcopy the input
                            existing_device.inputs[input_type][input_key] = deepcopy(
                                input_
                            )
                        else:
                            # If the input exists, merge modifiers
                            _logger.debug(f"Input key exists {input_key=}")
                            existing_input = existing_device.inputs[input_type][
                                input_key
                            ]
                            for modifier in input_.modifiers:
                                existing_modifier = (
                                    existing_input._check_existing_modifier(
                                        modifier.modifiers
                                    )
                                )
                                if existing_modifier is None:
                                    existing_input.modifiers.append(deepcopy(modifier))
                                # Don't merge upstream modifier command into current

        return src_profile


if __name__ == "__main__":
    pass
