# processors/process_scoil.py
# -*- coding: utf-8 -*-
import sympy
import traceback
import re
from .processor_utils import get_sympy_representation, sympy_expr_to_scl, get_target_scl_name, format_variable_name
from .symbol_manager import SymbolManager

SCL_SUFFIX = "_sympy_processed"

def process_scoil(instruction, network_id, sympy_map, symbol_manager: SymbolManager, data):
    """Genera SCL para Set Coil (SCoil), simplificando la condición."""
    instr_uid = instruction["instruction_uid"]
    instr_type_original = instruction.get("type", "SCoil")
    if instr_type_original.endswith(SCL_SUFFIX) or "_error" in instr_type_original:
        return False

    # Obtener condición de entrada (SymPy expr)
    in_info = instruction["inputs"].get("in")
    sympy_expr_in = get_sympy_representation(in_info, network_id, sympy_map, symbol_manager)

    # Obtener operando (nombre SCL del destino)
    target_scl_name = get_target_scl_name(instruction, "operand", network_id, default_to_temp=False) # SCoil necesita destino

    # Verificar dependencias
    if sympy_expr_in is None: return False
    if target_scl_name is None:
        print(f"Error: SCoil {instr_uid} operando no es variable o falta info.")
        instruction["scl"] = f"// ERROR: SCoil {instr_uid} operando no es variable."
        instruction["type"] = instr_type_original + "_error"
        return True

    # No hacer nada si la condición es FALSE constante
    if sympy_expr_in == sympy.false:
         instruction["scl"] = f"// SCoil {instr_uid} con condición FALSE constante, optimizado."
         instruction["type"] = instr_type_original + SCL_SUFFIX
         return True

    # Generar SCL Core (Set)
    scl_core = f"{target_scl_name} := TRUE;"

    # Aplicar Condición IF si no es TRUE constante
    scl_final = ""
    if sympy_expr_in != sympy.true:
        # Simplificar la condición ANTES de convertirla a SCL
        try:
            #simplified_expr = sympy.simplify_logic(sympy_expr_in, force=True)
            simplified_expr = sympy.logic.boolalg.to_dnf(sympy_expr_in, simplify=True)
        except Exception as e:
            print(f"Error simplifying condition for SCoil {instr_uid}: {e}")
            simplified_expr = sympy_expr_in # Fallback
        condition_scl = sympy_expr_to_scl(simplified_expr, symbol_manager)

        # Evitar IF TRUE THEN...
        if condition_scl == "TRUE":
             scl_final = scl_core
        else:
            indented_core = "\n".join([f"  {line}" for line in scl_core.splitlines()])
            scl_final = f"IF {condition_scl} THEN\n{indented_core}\nEND_IF;"
    else:
        # Condición es TRUE constante
        scl_final = scl_core

    # Actualizar instrucción
    instruction["scl"] = scl_final # SCL final generado
    instruction["type"] = instr_type_original + SCL_SUFFIX
    # SCoil no tiene salida lógica para propagar en sympy_map

    return True

# --- Processor Information Function ---
def get_processor_info():
    """Devuelve la información para la bobina Set (SCoil)."""
    return {'type_name': 'scoil', 'processor_func': process_scoil, 'priority': 3}