"""
Inscoper API Example Script: Prototype Acquisition
==================================================
This script demonstrates how to set up device states and display a snapped image in Napari.

Configuration Prerequisites:
---------------------------
1. Environment Variable: Ensure 'InscoperConfigsPath' is defined in your environment
   and points to your Inscoper configurations folder.
2. Configuration Name: This script assumes a hardware configuration named "camera_and_microscope".
   Change it below if your configuration is named differently.
3. Device/Channel Names: Customize device, channel parameter, objective, and filter-cube slot settings below.
"""
import os
from typing import Dict
from dataclasses import dataclass
import numpy as np
import napari
import inscoper_api



@dataclass
class SubDevice:
    name: str = None # a descriptive string for the sub-device
    tag: int = None # a unique identifier within the device for this sub-device
    inscoper_type: int = None # a "purpose" type for the sub-device (what it does, e.g., CUBE, OBJECTIVE, SHUTTER...)

@dataclass
class Device:
    name: str # the name given to the device during the configuration step
    hardware_id: int # an identifier for this device model
    sub_device_map: Dict[int, SubDevice] # the collection of chosen sub-devices for this particular device instance

# Macro to all Inscoper configurations
CONFIG_PATH = os.environ['InscoperConfigsPath']

my_bridge = inscoper_api.Bridge()

config = my_bridge.readConfigFile(os.sep.join([CONFIG_PATH, "camera_and_microscope"]))
my_bridge.loadConfigFile(os.sep.join([CONFIG_PATH, "camera_and_microscope"]))
my_bridge.initDevices()

# Dictionary to store the mapping of hardware IDs to our Device objects.
device_map = {}

# Step 1: Iterate over the "Device List" from the configuration. 
# This tells us what specific devices have been added to this system's configuration.
for device in config.getDeviceList():
    driver_config = device.getDriverConfig()
    
    # In this example, we only keep devices controlled by the Inscoper Device Controller.
    if isinstance(driver_config, inscoper_api.InscoperBoxDriverConfig):
        device_name = device.getId() # e.g., "My Microscope" or "My Stage"
        device_id = driver_config.getHardwareId() # The internal hardware identifier

        sub_device_map = {}
        
        # Iterate over all sub-devices configured for this device.
        for sub_device_config in device.getSubDeviceConfigList():
            sub_device_tag = sub_device_config.getTag() # The unique tag will allow us to connect config and description
            sub_device_id = sub_device_config.getId() # Descriptive name of the sub-device
            
            # Create our SubDevice instance and map it by its tag.
            sub_device_map[sub_device_tag] = SubDevice(name=sub_device_id, tag=sub_device_tag)
            
        # Store the complete Device object (with its sub-devices) in our global map.
        device_map[device_id] = Device(name=device_name, hardware_id=device_id, sub_device_map=sub_device_map)

# At this point, device_map contains our devices, but we are missing the "purpose" of each sub-device.
print(device_map)

# Step 2: Iterate over the "Device Description List".
# This information acts as the "class definitions" (descriptions.xml) for all possible hardware supported by Inscoper,
# providing metadata like the generic type (purpose) of the sub-devices.
for device_desc in my_bridge.getDeviceDescriptionList():

    driver_desc = device_desc.getDriverDescription()
    
    # Again, we only keep devices controlled by the Inscoper Device Controller.
    if isinstance(driver_desc, inscoper_api.InscoperBoxDriverDescription):
        device_id = driver_desc.getHardwareId()
        
        # If this hardware_id matches one of the devices we found in our configuration...
        if device_id in device_map:
            current_device = device_map[device_id]
            sub_device_map = current_device.sub_device_map
            
            # Look at all possible sub-devices described for this hardware model.
            for sub_device_desc in device_desc.getSubDeviceDescriptionList():
                sub_device_tag = sub_device_desc.getTag()
                sub_device_type = sub_device_desc.getType() # The general purpose of the sub-device (e.g., CUBE, SHUTTER, etc.)
                
                # If this configured device actually uses this specific sub-device...
                if sub_device_tag in sub_device_map:
                    current_sub_device = sub_device_map[sub_device_tag]
                    
                    # Store the human-readable string of the sub-device type 
                    current_sub_device.inscoper_type = inscoper_api.Utils.subDeviceTypeToString(sub_device_type)

# --8<-- [start:main_logic]

# 1. Create the Napari viewer
viewer = napari.Viewer()

objective_slot = 0
cube_slot = 0
lamp_intensity_percent = 30

# To move the stage we need to interact with the X_AXIS, Y_AXIS, and MOVE_XY sub-devices.
x_axis_sub_device_id    = None
y_axis_sub_device_id    = None
move_xy_sub_device_id   = None
objective_turret_sub_device_id = None
cube_turret_sub_device_id = None
light_power_sub_device_id = None
shutter_tl_sub_device_id = None
for device_id, device in device_map.items():
    for sub_device_tag, sub_device in device.sub_device_map.items():
        if sub_device.inscoper_type == "X_AXIS":
            x_axis_sub_device_id    = inscoper_api.SubDeviceId(device.name, sub_device.name)
        elif sub_device.inscoper_type == "Y_AXIS":
            y_axis_sub_device_id    = inscoper_api.SubDeviceId(device.name, sub_device.name)
        elif sub_device.inscoper_type == "MOVE_AXIS":
            move_xy_sub_device_id   = inscoper_api.SubDeviceId(device.name, sub_device.name)
        elif sub_device.inscoper_type == "SHUTTER" and "TL" in sub_device.name:
            shutter_tl_sub_device_id = inscoper_api.SubDeviceId(device.name, sub_device.name)
        elif sub_device.inscoper_type == "OBJECTIVE":
            objective_turret_sub_device_id = inscoper_api.SubDeviceId(device.name, sub_device.name)
        elif sub_device.inscoper_type == "CUBE":
            cube_turret_sub_device_id = inscoper_api.SubDeviceId(device.name, sub_device.name)
        elif ("Intensity" in sub_device.name or "Power" in sub_device.name) and "TL" in sub_device.name:
            light_power_sub_device_id = inscoper_api.SubDeviceId(device.name, sub_device.name)

my_bridge.setValue(objective_turret_sub_device_id, objective_slot)
my_bridge.setValue(cube_turret_sub_device_id, cube_slot)
my_bridge.setValue(light_power_sub_device_id, lamp_intensity_percent)
my_bridge.setValue(shutter_tl_sub_device_id, 1)

my_camera = my_bridge.getCamera("MyCamera")
image = my_camera.snapImage(0)

image_data = image.getImageData()

image_width = image.getWidth()
image_height = image.getHeight()
pixel_size_bytes = image.getPixelSize()
bit_depth = image.getBitDepth()
bytes_per_value = bit_depth // 8

values_per_pixel = pixel_size_bytes // bytes_per_value
img = np.reshape(image_data, (image_height, image_width, values_per_pixel))

viewer.add_image(img[:, :, 0], name="Microscope Snap")

my_bridge.setValue(shutter_tl_sub_device_id, 0)
my_bridge.setValue(light_power_sub_device_id, 0)

napari.run()

# --8<-- [end:main_logic]

my_bridge.close()

