# This is a CamiTK python action
import camitk

# There are four functions to interact with CamiTK
# - init is called when the application starts and the extension is loaded
# - process is called when the action is triggered (for action with GUI, click on "Apply")
# - targetDefined is called when the action targets, ie., its input(s), are modified
# - parameterChanged is called when a CamiTK parameter value is changed in the GUI
# 
# Note that only process is mandatory.
#
# self can be used like in a Python class to save the state.
# you can also use CamiTK property to save the state and have a GUI

def init(self:camitk.Action):
    # Add resource initialization if required (e.g. open a component)
    # You may comment or remove this method if your action does not required
    # any initialization
    return

def process(self:camitk.Action):
    # self.getTargets() contains the list of Component currently set as this action target
    # Example
    logMessage = "Action \"" + self.getName() + "\" called"
    @if@ $parameters$
    logMessage = logMessage + " with parameters\n"

    # Example of parameter value retrieval
    @else@
    logMessage = logMessage + " without any parameters\n"
    @endif@

    @for@ $p$ in $parameters$
    param$index(p)$ = self.getParameterValue("$title(p.name)$")
    @if@ contains($p.enumValues$)        
    enum$index(p)$Values = self.getProperty("$title(p.name)$").getAttribute("enumNames")
    logMessage = logMessage + " - $title(p.name)$: '" + enum$index(p)$Values[param$index(p)$] + "' (value: " + str(param$index(p)$) + ")\n"    
    @else@
    logMessage = logMessage + " - $title(p.name)$: " + str(param$index(p)$) + "\n";
    @endif@
    @endfor@

    # Create a warning log message for this demo (although CAMITK_TRACE_ALT
    # and CAMITK_INFO_ALT are usually recommended for log messages)
    camitk.warning(logMessage)
    
    @if@ $componentClass$
    # Process each targets
    # example of processing
    for selected in self.getTargets():
        # Here do something here input
        camitk.info("Processing component '" + selected.getName() + "'...")
    @endif@

    # call self.refreshApplication() or camitk.refresh() at the end to refresh the viewers
    return True # or False if something went wrong

def targetDefined(self:camitk.Action):
    # You may comment or remove this method if your action does not required
    # to do something when the action target(s) is/are defined
    logMessage = "Current targets:\n"
    for selected in self.getTargets():
        logMessage =  logMessage + "- Component \"" + selected.getName() + "\" is a target\n"
    camitk.warning(logMessage)
    return

def parameterChanged(self:camitk.Action, name:str):
    # You may comment or remove this method if your action does not required
    # to do something when one of the parameter's value has changed
    camitk.warning("Parameter " + name + " changed, new value: " + str(self.getParameterValue(name)))
    return