/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.mobirent.gui;

import com.mobirent.abstracttags.Epitomize;
import com.mobirent.abstracttags.Listable;
import com.mobirent.communications.HTTPRequest;
import com.mobirent.datacollector.Customer;
import com.mobirent.datacollector.Order;
import com.mobirent.messageprocessor.MessageToObject;
import com.mobirent.messageprocessor.OrderList;
import com.mobirent.messageprocessor.XmlMessage;
import gr.bluevibe.fire.components.Component;
import gr.bluevibe.fire.components.ListBox;
import gr.bluevibe.fire.components.ListElement;
import gr.bluevibe.fire.components.Panel;
import gr.bluevibe.fire.displayables.FireScreen;
import gr.bluevibe.fire.util.FireIO;
import java.util.Vector;
import javax.microedition.lcdui.Canvas;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.Font;

/**
 *
 * @author  Budai Attila
 */
public class AnswerPanel extends Epitomize {


    private Panel panel;
    private Listable list;
    private Command ok = new Command("Kiválaszt", Command.OK, 0);
    private Command cancel = new Command("Mégse", Command.BACK, 0);
    private static AnswerPanel INSTANCE = null;
    private ListElement listSelected;
    private Object data;
    private ListBox lBox = new ListBox();
    private HTTPRequest ht;
    private String labelem = "";
    private Panel listPanel;
    private String startChar = "";

    public void setData(Object data) {
        this.data = data;
    }

    private AnswerPanel() {
        panel = getAnswerPanel();
    }

    public ListBox getLBox() {
        return lBox;
    }

    private Panel getAnswerPanel() {
        lBox.setPointerPos(-1);
        listPanel = new Panel();
        listPanel.setId("");
   
        listPanel.addCommand(ok);
        listPanel.addCommand(cancel);
        listPanel.setCommandListener(this);
        return listPanel;

    }

    private void listFeltolt(Listable list) {
        if (list == null) {
        }

        for (int i = 0; i < list.getList().size(); i++) {
            lBox.add(new ListElement(list.getList().elementAt(i).toString(), "L", false));
           
        }

        lBox.setBullet(FireIO.getLocalImage("box"));
        lBox.setSelectedBullet(FireIO.getLocalImage("checkedbox"));
        lBox.setCommandListener(this);
        panel.setVerticalOffset(-1);
        panel.validate();
        lBox.setPointerPos(-1);
        lBox.validate();

        System.gc();
    }

    public Object feldolgoz(String answer, String selected) throws ExceptionHandler {

        if (answer == null) {
            throw new ExceptionHandler("1The Answer Is Null!");

        }

        String ret = "";


        int i = 0;
        for (i = 0; i < answer.length(); i++) {
            if (answer.charAt(i) == '>') {
                break;
            }
        }
        ret = answer.substring(i + 1, answer.length());
        XmlString2Data xmlStr2Data = new XmlString2Data(ret);
        XmlMessage xmlMsg = null;
        xmlMsg = new XmlMessage(xmlStr2Data.getXml());
        MessageToObject msg2Obj = new MessageToObject(xmlMsg);

        if (selected.equals("Ok")) {
            msg2Obj.generateObject("Ok");
        }

        if (selected.equals("GetOrderList") || selected.equals("Visszavivendőek")) {
            return list = new OrderList((Vector) msg2Obj.generateObject("GOL"));
        }
        if (selected.equals("selectedCustomer")) {
            return msg2Obj.generateObject("selectedCustomer");
        }
        return list;

    }

    public String connecting() throws ExceptionHandler {
        if (done == false) {
            ht.sendRequest();
            setIsDone();
        } else {
            throw new ExceptionHandler("Kapcsolódási Időtúllépés!");
        }

        return ht.getTextAnswer();
    }

    public void panelSetup() {
        try {

            ht = new HTTPRequest();

            lBox.deleteAllElement();
            
            lBox.setPointerPos(-1);
            listPanel.removeAll();
            listPanel.setId(labelem);
            String s;
            if (labelem.equals("GetOrderList")) {
                s = "Rendelések";
            } else {
                s = labelem;
            }

            listPanel.add(createRow(s, Font.STYLE_BOLD));
            listPanel.add(lBox);
            if (labelem.equals("Visszavivendőek")) {

                
                Vector a1 = new Vector();
                Vector a2 = new Vector();
                a1.addElement("ori");
                a2.addElement((String) data);
                cancel = new Command("Kilépés", Command.BACK, 0);
                listPanel.addCommand(cancel);
                ht.setDataToSend(Epitomize.methodCall("rdd", a1, a2));
                Epitomize.orderID = "";
            } else if (labelem.equals("GetOrderList")) {
                ht.setDataToSend(methodCall("gosl"));
            }
            listFeltolt((Listable) feldolgoz(connecting(), labelem));
            screen.setCurrent(this.getPanel());
            System.gc();

        } catch (ExceptionHandler ex) {
            screen.getCurrentPanel().showAlert(ex.getMessage(), null);
        }

    }

    public void setStartChar(String startChar) {
        this.startChar = startChar;
    }

    public String getStartChar() {
        return startChar;
    }

    public void setLabelem(String labelem) {
        this.labelem = labelem;
    }

    public String getLabelem() {
        return labelem;
    }

    public Panel getPanel() {
        return panel;
    }

    public void setHt(HTTPRequest ht) {
        this.ht = ht;
    }

    public void setHtDataToSend(String data) {
        ht.setDataToSend(data);
    }

    public static AnswerPanel getInstance() {
        if (INSTANCE == null) {
            INSTANCE = new AnswerPanel();
        }
        System.gc();

        return INSTANCE;
    }

    public void commandAction(Command comm, Component c) {
        System.gc();

        if (comm == ok) {
            lBox.keyEvent(Canvas.FIRE);
            listSelected = (ListElement) ((ListBox) lBox).getCheckedElements().elementAt(0);
            if (labelem.equals("Visszavivendőek")) {
                threadWait();
                new Thread(new Runnable() {

                    public void run() {
                        HTTPRequest http = new HTTPRequest();
                        Order tmp = (Order) list.getList().elementAt(lBox.getCheckedElementIndex());
                        Vector a1 = new Vector();
                        Vector a2 = new Vector();
                        a1.addElement("ori");
                        a2.addElement((String) tmp.getOrderID());
                        Epitomize.setOrderID(tmp.getOrderID());
                        http.setDataToSend(methodCall("rro", a1, a2));
                        try {
                            http.sendRequest();
                        } catch (ExceptionHandler ex) {
                            ex.printStackTrace();
                        }
                        try {
                            feldolgoz(http.getTextAnswer(), "Ok");
                        } catch (ExceptionHandler ex) {
                            screen.getCurrentPanel().showAlert(ex.getMessage(), null);
                        }
                        lBox.deleteElement(lBox.getCheckedElementIndex());
                        lBox.validate();
                    }
                }).start();
                return;

            } else if (labelem.equals("GetOrderList")) {
                threadWait();
                new Thread(new Runnable() {

                    public void run() {
                        HTTPRequest http = new HTTPRequest();
                        Order tmp = (Order) list.getList().elementAt(lBox.getCheckedElementIndex());
                        Vector a1 = new Vector();
                        Vector a2 = new Vector();
                        a1.addElement("ori");
                        a2.addElement(tmp.getOrderID());
                        Epitomize.setOrderID(tmp.getOrderID());
                        http.setDataToSend(methodCall("rd", a1, a2));
                        try {
                            http.sendRequest();
                        } catch (ExceptionHandler ex) {
                            ex.printStackTrace();
                        }
                        RePanel rp = RePanel.getINSTANCE();

                        try {
                            rp.setId(tmp.getOrderID());
                            rp.setupRePanel((Customer) AnswerPanel.getInstance().feldolgoz(http.getTextAnswer(), "selectedCustomer"));

                        } catch (ExceptionHandler ex) {
                            ex.printStackTrace();
                        }
                        screen.setCurrent(rp.getPanel());

                    }
                }).start();
                return;
            }

        }

        if (comm == cancel) {

            if (labelem.equals("GetOrderList")) {
                screen.setCurrent(MainPanel.getInstance().getPanel(), FireScreen.RIGHT);
                screen.closePopup();
                screen.setBusyMode(false);
                return;
            }
            if (labelem.equals("Visszavivendőek")) {
                orderID = "";
                sessionId = "";
                OId = "";
                threadWait();
                new Thread(new Runnable() {

                    public void run() {
                        Epitomize.putSessionId2RecordStore();
                        ht.setDataToSend(sessionStop());
                        try {
                            ht.sendRequest();
                        } catch (ExceptionHandler ex) {
                            screen.getCurrentPanel().showAlert(ex.getMessage(), null);
                        }
                        myMidlet.exit();
                    }
                }).start();

                return;
            }
            return;
        }

    }
}
