001package jmri.jmrit.operations.logixng;
002
003import java.beans.PropertyChangeEvent;
004import java.beans.PropertyChangeListener;
005import java.util.*;
006
007import jmri.*;
008import jmri.jmrit.logixng.*;
009import jmri.jmrit.logixng.actions.AbstractDigitalAction;
010import jmri.jmrit.logixng.util.*;
011import jmri.jmrit.logixng.util.parser.*;
012import jmri.jmrit.operations.automation.Automation;
013import jmri.jmrit.operations.automation.AutomationManager;
014import jmri.util.ThreadingUtil;
015
016/**
017 * This action starts an OperationsPro automation.
018 *
019 * @author Daniel Bergqvist Copyright 2025
020 */
021public class OperationsProStartAutomation extends AbstractDigitalAction
022        implements PropertyChangeListener {
023
024    private final LogixNG_SelectComboBox _selectAutomation =
025            new LogixNG_SelectComboBox(this, new AutomationItem[]{}, null, this);
026
027    private final AutomationManager _automationManager;
028    private final List<AutomationItem> _automationList = new ArrayList<>();
029
030
031    public OperationsProStartAutomation(String sys, String user)
032            throws BadUserNameException, BadSystemNameException {
033        super(sys, user);
034
035        _automationManager = InstanceManager.getDefault(AutomationManager.class);
036        updateList();
037        _automationManager.addPropertyChangeListener(AutomationManager.LISTLENGTH_CHANGED_PROPERTY, this);
038    }
039
040    @Override
041    public Base getDeepCopy(Map<String, String> systemNames, Map<String, String> userNames) throws ParserException {
042        DigitalActionManager manager = InstanceManager.getDefault(DigitalActionManager.class);
043        String sysName = systemNames.get(getSystemName());
044        String userName = userNames.get(getSystemName());
045        if (sysName == null) sysName = manager.getAutoSystemName();
046        OperationsProStartAutomation copy = new OperationsProStartAutomation(sysName, userName);
047        copy.setComment(getComment());
048        _selectAutomation.copy(copy._selectAutomation);
049        return manager.registerAction(copy);
050    }
051
052    public LogixNG_SelectComboBox getSelectAutomations() {
053        return _selectAutomation;
054    }
055
056    /** {@inheritDoc} */
057    @Override
058    public LogixNG_Category getCategory() {
059        return CategoryOperations.OPERATIONS;
060    }
061
062    /** {@inheritDoc} */
063    @Override
064    public void execute() throws JmriException {
065        AutomationItem automation = (AutomationItem) _selectAutomation
066                .evaluateValue(getConditionalNG());
067
068        if (automation != null) {
069            ThreadingUtil.runOnGUIWithJmriException(() -> {
070                automation._automation.run();
071            });
072        }
073    }
074
075    @Override
076    public FemaleSocket getChild(int index) throws IllegalArgumentException, UnsupportedOperationException {
077        throw new UnsupportedOperationException("Not supported.");
078    }
079
080    @Override
081    public int getChildCount() {
082        return 0;
083    }
084
085    @Override
086    public String getShortDescription(Locale locale) {
087        return Bundle.getMessage(locale, "OperationsProStartAutomation_Short");
088    }
089
090    @Override
091    public String getLongDescription(Locale locale) {
092        String automation = _selectAutomation.getDescription(locale);
093
094        return Bundle.getMessage(locale, "OperationsProStartAutomation_Long", automation);
095    }
096
097    /** {@inheritDoc} */
098    @Override
099    public void setup() {
100        // Do nothing
101    }
102
103    /** {@inheritDoc} */
104    @Override
105    public void registerListenersForThisClass() {
106        _selectAutomation.registerListeners();
107    }
108
109    /** {@inheritDoc} */
110    @Override
111    public void unregisterListenersForThisClass() {
112        _selectAutomation.unregisterListeners();
113    }
114
115    /** {@inheritDoc} */
116    @Override
117    public void propertyChange(PropertyChangeEvent evt) {
118        if (AutomationManager.LISTLENGTH_CHANGED_PROPERTY.equals(evt.getPropertyName())) {
119            updateList();
120        }
121    }
122
123    private void updateList() {
124        _automationList.clear();
125        for (Automation automation : _automationManager.getAutomationsByNameList()) {
126            _automationList.add(new AutomationItem(automation));
127        }
128        AutomationItem[] automations = _automationList.toArray(AutomationItem[]::new);
129        _selectAutomation.setValues(automations);
130    }
131
132    /** {@inheritDoc} */
133    @Override
134    public void disposeMe() {
135    }
136
137
138    public static class AutomationItem implements LogixNG_SelectComboBox.Item {
139
140        private final Automation _automation;
141
142        public AutomationItem(Automation automation) {
143            this._automation = automation;
144        }
145
146        @Override
147        public String getKey() {
148            return _automation.getId();
149        }
150
151        @Override
152        public String toString() {
153            return _automation.getName();
154        }
155    }
156
157//    private final static org.slf4j.Logger log = org.slf4j.LoggerFactory.getLogger(ActionSensor.class);
158}