#!/usr/bin/env python3

import sys
import subprocess
import xml.etree.ElementTree as ET

# Path to the XML file
xml_file = "ibvunit/ibvunit.xml"
dok_file = "dok.txt"

# Function to display help

def show_help():
    print()
    print("Options:")
    print("  --show_active_output    Display only controllers with active outputs")
    print("  --all                   Display full information about controllers")
    print("  --show-id <id>          Display outputs only from the specified controller")
    print("                          (Works only with --all or --show_active_output)")
    print("                          --all --show-id 35")
    print("                          --show_active_output--show-id 35")
    print("  --help                  Display this help message")
    sys.exit(1)

# Function to run system commands and return the output
def run_command(command):
    result = subprocess.run(command, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
    return result.stdout.decode('utf-8')

# Parse the XML file and get Address values from the <Devices> section
def get_addresses_from_xml(xml_file):
    tree = ET.parse(xml_file)
    root = tree.getroot()

    # Namespace must be included for the XML
    namespace = {'ns': 'http://www.insbud.net/ibvunit'}

    # Extract all addresses from the Devices section
    addresses = []
    for device in root.findall('.//ns:Devices/ns:Device', namespace):
        addresses.append(device.get('Address'))

    return addresses

# Read dok.txt and create a mapping of output names to their descriptions
def load_output_descriptions(dok_file):
    descriptions = {}
    current_id = None
    with open(dok_file, 'r') as file:
        for line in file:
            line = line.strip()
            if line.startswith("id."):  # New controller section
                current_id = line.split(".")[1]
            elif current_id is not None and line.startswith("output.do."):
                output_name = f"rs.0.id.{current_id}.{line.split('=')[0].strip()}"
                description = line.split('=')[1].strip()
                descriptions[output_name] = description
    return descriptions

# Main logic
def main():
    show_active_output = False
    show_all = False
    specific_id = None

    # Check arguments
    if len(sys.argv) == 1:
        show_help()

    i = 1
    while i < len(sys.argv):
        arg = sys.argv[i]
        if arg == "--show_active_output":
            show_active_output = True
        elif arg == "--all":
            show_all = True
        elif arg == "--show-id":
            if i + 1 < len(sys.argv):  # Check if id is provided
                specific_id = sys.argv[i + 1]
                i += 1  # Skip to the next argument
            else:
                print("Error: You must provide an identifier after --show-id.")
                show_help()
        elif arg == "--help":
            show_help()
        else:
            print(f"Unknown option: {arg}")
            show_help()
        i += 1  # Move to the next argument

    # Validate argument combinations
    if specific_id is not None and not (show_active_output or show_all):
        print("Error: --show-id works only with --all or --show_active_output.")
        show_help()

    # Get addresses from the XML file
    addresses = get_addresses_from_xml(xml_file)

    # Load output descriptions from dok.txt
    output_descriptions = load_output_descriptions(dok_file)

    # For each address, execute the corresponding command
    for address in addresses:
        # Check if the identifier matches
        if specific_id is not None and specific_id != address:
            continue  # Skip other identifiers

        output = run_command(f'./ibls -a 127.0.0.1 --pretty -p 2001 -c "get(rs.0.id.{address}.out;);"')

        # If --all option is provided, display full output without active output details
        if show_all:
            print(f"Controller id.{address}:")
            print(output)

        # If --show_active_output option is provided, display only active outputs
        if show_active_output:
            active_outputs = [line for line in output.splitlines() if 'do.' in line and '= 1' in line]

            if active_outputs:
                for line in active_outputs:
                    # Extract the full output name, e.g., rs.0.id.39.output.do.2
                    output_name = line.split('=')[0].strip()
                    output_identifier = output_name.split('.')[-1]  # This should give us do.x

                    # Look for the description in the loaded mappings
                    full_output_name = f"rs.0.id.{address}.output.{output_identifier.split('.')[-1]}"
                    description = output_descriptions.get(full_output_name, "Unknown output")

                    # Debug: Print the full output name and the corresponding description key
                    print(f"Checking for output: {full_output_name} (Key: {output_identifier})")

                    # Check if the output is present in the descriptions and get the name
                    output_description_key = f"output.do.{output_identifier.split('.')[-1]}"  # Corrected to just 'output.do.x'
                    print(f"Checking for description with key: {output_description_key}")  # Debug line
                    if output_description_key in output_descriptions:
                        description = output_descriptions[output_description_key]

                    print(f"Controller id.{address} output.{output_identifier.split('.')[-1]} name: {description}")
            else:
                print(f"Controller id.{address} has no active outputs.")

if __name__ == "__main__":
    main()
