package com.ht.svg.action;

import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.events.Event;

import com.ht.data.RealTimeDataManager;

/**
 * the superclass of the send command actions
 *
 * @deprecated 只支持简单的遥控命令
 * @author Jordi SUC
 */
@Deprecated
public class SendCommand extends AbstractActionListener {

    /**
     * the string names
     */
    protected static String tagToWriteName = "tagToWrite", referenceTagName = "referenceTag", invalidValueName = "invalidValue",
            defaultValueName = "defaultValue", returnToInitialValueMethodName = "returnToInitialValueMethod",
            autoName = "auto", mouseUpEventName = "mouseUpEvent",
            valueName = "value", commandToSendName = "commandToSend", usedAttName = "used";

    /**
     * the constructor of the class
     *
     * @param actionElement the dom element defining the action
     */
    public SendCommand(Element actionElement) {

        super(actionElement);

    }

    @Override
    public void performAction(Event event) {


            //the tag to write value to set
            String tagValue = "";

            //getting the current value of the reference tag
            Object obj = RealTimeDataManager.INSTANCE.getVarValue(actionElement.getAttribute(referenceTagName));
            
            if (obj != null) {

                String referenceTagValue = obj.toString();

                if (actionElement.hasChildNodes() && !referenceTagValue.equals("")) {

                    //getting the tag to write value corresponding to the reference tag value
                    Element childElement = null;
                    String value = "";
                    
                    for (Node child = actionElement.getFirstChild(); child != null; child = child.getNextSibling()) {

                        if (child instanceof Element) {

                            childElement = (Element) child;
                            value = childElement.getAttribute(valueName);

                            if (referenceTagValue.equals(value)) {

                                tagValue = childElement.getAttribute(commandToSendName);
                                break;
                            }
                        }
                    }
                }

                if (tagValue.equals("")) {

                    //setting the default value for the tag to write
                    tagValue = actionElement.getAttribute(defaultValueName);
                }

            } else {

                //setting the invalid value for the tag to write
                tagValue = actionElement.getAttribute(invalidValueName);
            }

            if (tagValue != null) {

            }

            //putTagValue(actionElement.getAttribute(tagToWriteName), tagValue);

    }
}
