001package jmri.jmrix.lenz.messageformatters;
002
003import jmri.jmrix.Message;
004import jmri.jmrix.lenz.XNetConstants;
005import jmri.jmrix.lenz.XNetReply;
006import jmri.jmrix.lenz.XPressNetMessageFormatter;
007
008/**
009 * Format replies for XPressNet Command Station reply for Loco Function Status.
010 *
011 * @author Paul Bender Copyright (C) 2025
012 */
013public class XNetLocoFunctionMomentaryStatusReplyFormatter implements XPressNetMessageFormatter {
014    private static final String RS_TYPE = "rsType";
015    private static final String FUNCTION_MOMENTARY = "FunctionMomentary";
016    private static final String FUNCTION_CONTINUOUS = "FunctionContinuous";
017
018    @Override
019    public boolean handlesMessage(Message m) {
020        return m instanceof XNetReply &&
021                m.getElement(0) == XNetConstants.LOCO_INFO_RESPONSE &&
022                m.getElement(1) == XNetConstants.LOCO_FUNCTION_STATUS;
023    }
024
025    @Override
026    public String formatMessage(Message m) {
027        if(!handlesMessage(m)) { 
028            throw new IllegalArgumentException("Message is not supported");
029        }
030        return Bundle.getMessage("XNetReplyLocoLabel") + " " + 
031               Bundle.getMessage(RS_TYPE) + " " + // "Locomotive", key in NBBundle, shared with Operations
032               Bundle.getMessage("XNetReplyFStatusLabel") + " " +
033               parseFunctionMomentaryStatus(m.getElement(2), m.getElement(3));
034    }
035
036    /**
037     * Parse the Momentary status of functions.
038     *
039     * @param element3 contains the data byte including F0,F1,F2,F3,F4
040     * @param element4 contains F12,F11,F10,F9,F8,F7,F6,F5
041     * @return readable version of message
042     */
043    protected String parseFunctionMomentaryStatus(int element3, int element4) {
044        String text = "";
045        if ((element3 & 0x10) != 0) {
046            text += "F0 " + Bundle.getMessage(FUNCTION_MOMENTARY) + "; ";
047        } else {
048            text += "F0 " + Bundle.getMessage(FUNCTION_CONTINUOUS) + "; ";
049        }
050        if ((element3 & 0x01) != 0) {
051            text += "F1 " + Bundle.getMessage(FUNCTION_MOMENTARY) + "; ";
052        } else {
053            text += "F1 " + Bundle.getMessage(FUNCTION_CONTINUOUS) + "; ";
054        }
055        if ((element3 & 0x02) != 0) {
056            text += "F2 " + Bundle.getMessage(FUNCTION_MOMENTARY) + "; ";
057        } else {
058            text += "F2 " + Bundle.getMessage(FUNCTION_CONTINUOUS) + "; ";
059        }
060        if ((element3 & 0x04) != 0) {
061            text += "F3 " + Bundle.getMessage(FUNCTION_MOMENTARY) + "; ";
062        } else {
063            text += "F3 " + Bundle.getMessage(FUNCTION_CONTINUOUS) + "; ";
064        }
065        if ((element3 & 0x08) != 0) {
066            text += "F4 " + Bundle.getMessage(FUNCTION_MOMENTARY) + "; ";
067        } else {
068            text += "F4 " + Bundle.getMessage(FUNCTION_CONTINUOUS) + "; ";
069        }
070        if ((element4 & 0x01) != 0) {
071            text += "F5 " + Bundle.getMessage(FUNCTION_MOMENTARY) + "; ";
072        } else {
073            text += "F5 " + Bundle.getMessage(FUNCTION_CONTINUOUS) + "; ";
074        }
075        if ((element4 & 0x02) != 0) {
076            text += "F6 " + Bundle.getMessage(FUNCTION_MOMENTARY) + "; ";
077        } else {
078            text += "F6 " + Bundle.getMessage(FUNCTION_CONTINUOUS) + "; ";
079        }
080        if ((element4 & 0x04) != 0) {
081            text += "F7 " + Bundle.getMessage(FUNCTION_MOMENTARY) + "; ";
082        } else {
083            text += "F7 " + Bundle.getMessage(FUNCTION_CONTINUOUS) + "; ";
084        }
085        if ((element4 & 0x08) != 0) {
086            text += "F8 " + Bundle.getMessage(FUNCTION_MOMENTARY) + "; ";
087        } else {
088            text += "F8 " + Bundle.getMessage(FUNCTION_CONTINUOUS) + "; ";
089        }
090        if ((element4 & 0x10) != 0) {
091            text += "F9 " + Bundle.getMessage(FUNCTION_MOMENTARY) + "; ";
092        } else {
093            text += "F9 " + Bundle.getMessage(FUNCTION_CONTINUOUS) + "; ";
094        }
095        if ((element4 & 0x20) != 0) {
096            text += "F10 " + Bundle.getMessage(FUNCTION_MOMENTARY) + "; ";
097        } else {
098            text += "F10 " + Bundle.getMessage(FUNCTION_CONTINUOUS) + "; ";
099        }
100        if ((element4 & 0x40) != 0) {
101            text += "F11 " + Bundle.getMessage(FUNCTION_MOMENTARY) + "; ";
102        } else {
103            text += "F11 " + Bundle.getMessage(FUNCTION_CONTINUOUS) + "; ";
104        }
105        if ((element4 & 0x80) != 0) {
106            text += "F12 " + Bundle.getMessage(FUNCTION_MOMENTARY) + "; ";
107        } else {
108            text += "F12 " + Bundle.getMessage(FUNCTION_CONTINUOUS) + "; ";
109        }
110        return (text);
111    }
112
113
114}