/*
 * 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.communications.HTTPRequest;
import gr.bluevibe.fire.components.Component;
import gr.bluevibe.fire.components.Panel;
import gr.bluevibe.fire.components.Row;
import gr.bluevibe.fire.displayables.FireScreen;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.Font;

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

    private static MainPanel Instance;
    private Command exit = new Command("Kilépés", Command.BACK, 0);
    private Panel panel;

    private MainPanel() {
        panel = getMainPanel();
    }

    private Panel getMainPanel() {

        Panel main = new Panel();
        main.setId("Főmenü");
        main.setLabel("");
        main.addCommand(exit);

        main.setCommandListener(this);

        main.add(createRow("Menü", Font.STYLE_BOLD));
        main.add(new Row());

        main.add(new Row());
        Row e3 = new Row();
        e3.setLabel("Rendelések lekérdezése", FireScreen.defaultLabelFont, new Integer(screen.getWidth() / 1), FireScreen.LEFT);
        e3.setEditable(false);
        e3.setIs(false);
        main.add(e3);
        main.add(getChoose3("Választ", FireScreen.RIGHT));
        main.add(new Row());

        main.add(getChoose1("Kiszállítás megtörtént", FireScreen.RIGHT));

        main.add(new Row());
        main.add(getChoose2("Beállítások", FireScreen.RIGHT));
        return main;

    }

    public Panel getPanel() {
        recordStoreProcesser(recordStoreReader("Adatok"));
        getRecordStoreDatas();
        System.out.println("URL" + URL + ";");
        if (URL.equals("") || userName.equals("")) {         
            SetupPanel s = SetupPanel.getInstance();
            return s.getPanel();
        }
        if (!myMidlet.isLlj() && Epitomize.sessionId.equals("") && Epitomize.orderID.equals("")) {
            System.out.println("login");
            myMidlet.login();
        }
        return panel;
    }

    public static MainPanel getInstance() {
        if (Instance == null) {
            Instance = new MainPanel();
        }
        return Instance;
    }

    public void commandAction(Command cmd, Component c) {
        System.gc();
        if (cmd == choose1) {
            if (orderID.equals("")) {
                screen.getCurrentPanel().showAlert("Nincs Regisztrálandó rendelés!", null);
                try {
                    Thread.sleep(2000);
                    return;
                } catch (InterruptedException ex) {
                    ex.printStackTrace();
                }
            }
            Epitomize.threadWait();
            new Thread(new Runnable() {
                public void run() {
                    AnswerPanel gap = AnswerPanel.getInstance();
                    gap.setLabelem("Visszavivendőek");
                    gap.setData(Epitomize.orderID);
                    gap.panelSetup();
                    done = false;
                }
            }).start();
            return;
        }
        if (cmd == choose3) {
            Epitomize.threadWait();
            new Thread(new Runnable() {
                public void run() {
                    AnswerPanel gap = AnswerPanel.getInstance();
                    gap.setLabelem("GetOrderList");
                    gap.panelSetup();
                    done = false;
                }
            }).start();
            return;
        }
        if (cmd == exit) {
            threadWait();
            new Thread(new Runnable() {
                public void run() {
                    HTTPRequest ht = new HTTPRequest();
                    ht.setDataToSend(sessionStop());
                    try {
                        ht.sendRequest();
                    } catch (ExceptionHandler ex) {
                        //screen.getCurrentPanel().showAlert(ex.getMessage(), null);
                    }
                    sessionId = "";
                    orderID = "";
                    OId = "";
                    Epitomize.putSessionId2RecordStore();
                    myMidlet.exit();
                }
            }).start();
            return;
        }
        if (cmd == choose2) {
            screen.getCurrentPanel().setAlertMode(false);
            screen.setCurrent(SetupPanel.getInstance().getPanel());
            return;
        }
    }
}
