from launch import LaunchDescription
from launch_ros.actions import Node
from launch.actions import DeclareLaunchArgument, OpaqueFunction
from launch.substitutions import LaunchConfiguration

import os
import json
from ament_index_python.packages import get_package_share_directory


share_dir = get_package_share_directory('vision_detect')
config_dir = os.path.join(share_dir, 'configs/launch_configs/crossboard_topic_pca.json')
with open(config_dir, "r") as f:
    configs = json.load(f)

def generate_launch_description():
    args_detect = [
        DeclareLaunchArgument('output_boxes', default_value=configs['output_boxes']),
        DeclareLaunchArgument('output_masks', default_value=configs['output_masks']),
        DeclareLaunchArgument('left_hand', default_value=configs['left_hand']),
        DeclareLaunchArgument('right_hand', default_value=configs['right_hand']),
        DeclareLaunchArgument('head', default_value=configs['head']),


        DeclareLaunchArgument('get_camera_mode', default_value=configs['get_camera_mode']),
        DeclareLaunchArgument('detect_mode', default_value=configs['detect_mode']),
        DeclareLaunchArgument('calculate_mode', default_value=configs['calculate_mode']),

        DeclareLaunchArgument("Topic_configs", default_value=json.dumps(configs['Topic_configs'])),
        DeclareLaunchArgument("Crossboard_configs", default_value=json.dumps(configs['Crossboard_configs'])),
        DeclareLaunchArgument("PCA_configs", default_value=json.dumps(configs['PCA_configs'])),
    ]

    def create_detect_node(context):
        output_boxes = LaunchConfiguration('output_boxes').perform(context)
        output_masks = LaunchConfiguration('output_masks').perform(context)
        left_hand = LaunchConfiguration('left_hand').perform(context)
        right_head = LaunchConfiguration('right_head').perform(context)
        head = LaunchConfiguration('head').perform(context)

        get_camera_mode = LaunchConfiguration('get_camera_mode').perform(context)
        detect_mode = LaunchConfiguration('detect_mode').perform(context)
        calculate_mode = LaunchConfiguration('calculate_mode').perform(context)

        Topic_configs = LaunchConfiguration('Topic_configs').perform(context)
        Crossboard_configs = LaunchConfiguration('Crossboard_configs').perform(context)
        PCA_configs = LaunchConfiguration('PCA_configs').perform(context)

        return [
            Node(
                package='vision_detect',
                executable='detect_node',
                name=configs['node_name'],
                parameters=[{
                    'output_boxes': output_boxes.lower() == 'true',
                    'output_masks': output_masks.lower() == 'true',
                    'left_hand': left_hand,
                    'right_head': right_head,
                    'head': head,

                    'get_camera_mode': get_camera_mode,
                    'detect_mode': detect_mode,
                    'calculate_mode': calculate_mode,

                    'Topic_configs': Topic_configs,
                    'Crossboard_configs': Crossboard_configs,
                    'PCA_configs': PCA_configs,
                }]
            )
        ]

    return LaunchDescription(args_detect + [
        OpaqueFunction(function=create_detect_node),
    ])
