package com.mobirent.messageprocessor;

import com.mobirent.abstracttags.Epitomize;


import com.mobirent.datacollector.Customer;
import com.mobirent.datacollector.Order;
import com.mobirent.gui.ExceptionHandler;

import java.util.Vector;

/**
 *
 * @author Budai Attila
 */
public class MessageToObject {

    private XmlMessage xmlMsg;
    private String typeNodeName = "";

    /**
     * 
     * @param xmlMsg a feldolgozandó üzenet.
     */
    public MessageToObject(XmlMessage xmlMsg) {
        this.xmlMsg = xmlMsg;
        System.out.println("msg2Obj : " + xmlMsg);
    }

    /**
     * Az üzenetnek megfelelően visszatér a megfelelő objektummal, amennyiben nem találunk megfelelőt,
     * akkor dobunk egy kivételt.
     * @return
     * @throws com.mobut.gui.MyException
     */
    public Object generateObject(String type) throws ExceptionHandler {
        Vector vector = new Vector();

        XmlNode node = null;// = new XmlNode();
        try {
            node = keres(xmlMsg.baseNode, "<message>");
        } catch (Exception ex) {
            System.out.println("hell");
        }
        if (node != null) {
            throw new ExceptionHandler(1 + node.getValue());
        }
        if (type.equals("Ok")) {
            node = keres(xmlMsg.baseNode, "<message>");
            if (node != null) {
                throw new ExceptionHandler("1Sikertelen művelet");
            } else {
                throw new ExceptionHandler("1Sikeres művelet");
            }
        }
        if (type.equals("selectedCustomer")) {
            node = keres(xmlMsg.baseNode, "<result>");
            if (!node.hasMoreNodes()) {
                throw new ExceptionHandler("1Nem érkezett adat.");
            }
            return new Customer(node.getNextNode());

        }
        if (type.equals("GOL")) {
            node = keres(xmlMsg.baseNode, "<result>");
            if (!node.hasMoreNodes()) {
                throw new ExceptionHandler("1Üres a lista.");
            }
            for (; node.hasMoreNodes();) {
                Order ord = new Order(node.getNextNode());
                vector.addElement(ord);
            }
            return vector;



        }


        node = xmlMsg.baseNode;



        if (type.equals("getDatas")) {

            node = keres(xmlMsg.baseNode, "<response>");
            Vector nodes = new Vector();
            nodes = node.getSubNodes();
            for (int i = 0; i < nodes.size(); i++) {
                XmlNode tmpNode = (XmlNode) nodes.elementAt(i);
                if (tmpNode.getNodeName().equals("<sid>")) {
                    Epitomize.setSessionId(tmpNode.getValue());
                } else if (tmpNode.getNodeName().equals("<oid>")) {
                    Epitomize.setOId(tmpNode.getValue());
                } else if (tmpNode.getNodeName().equals("<result>")) {
                    if ((tmpNode.getValue()) != null) {
                        throw new ExceptionHandler(tmpNode.getValue());

                    }

                }

            }

            System.out.println("oid: " + Epitomize.getOId());
            System.out.println("sid : " + Epitomize.getSessionId());
            return null;
        }
        throw new ExceptionHandler("1Ismeretlen Típusú input." + node.toString());
    }

    public static XmlNode keres(XmlNode node, String nodeName) {
        XmlNode nodeom = null;
        if (node.getNodeName().equals(nodeName)) {
            System.out.println("megtaláltam, és visszaadom : " + node.toString());
            return nodeom = node;
        }
        while (node.hasMoreNodes()) {
            nodeom = keres(node.getNextNode(), nodeName);
        }
        return nodeom;

    }
}
