/*
 * 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 com.mobirent.communications.MD5;
import gr.bluevibe.fire.displayables.FireScreen;
import gr.bluevibe.fire.displayables.SplashScreen;
import gr.bluevibe.fire.util.FireIO;

import java.util.Hashtable;
import java.util.Vector;
import javax.microedition.lcdui.Canvas;
import javax.microedition.lcdui.Display;
import javax.microedition.midlet.*;

/**
 * @author Budai Attila
 */
public class Midlet extends MIDlet {

    private FireScreen screen;
    private boolean llj = false;

    public void startApp() {
        FireIO.setup(setTheme("theme.png"), 10, null);
        SplashScreen loading = new SplashScreen();
        loading.setTitle("Futár");
        loading.setLogo(FireIO.getLocalImage("logo"));
        Display.getDisplay(this).setCurrent(loading);

        screen = FireScreen.getScreen(Display.getDisplay(this));
        screen.setFullScreenMode(true);
        screen.setOrientation(FireScreen.NORMAL);
        screen.setOrientationChangeKey(new Integer(Canvas.KEY_STAR));
        screen.setInteractiveBusyMode(false);
        MainPanel mainPanel = null;
        Epitomize.setScreen(screen);
        Epitomize.setMyMidlet(this);

        mainPanel = MainPanel.getInstance();

        screen.setCurrent(mainPanel.getPanel());


    }

    public void pauseApp() {
        notifyPaused();
        return;
    }

    public void login() {

        Epitomize.threadWait();
        new Thread(new Runnable() {

            public void run() {
                Epitomize.isDone = true;
                while (Epitomize.isDone) {
                    HTTPRequest test = new HTTPRequest();
                    test.setBaseURL(Epitomize.URL);
                    test.setDataToSend(Epitomize.sessionStartStr("TransporterSF"));
                    try {
                        try {
                            test.sendRequest();
                        } catch (ExceptionHandler e) {
                            screen.getCurrentPanel().showAlert(e.getMessage(), null);
                            return;
                        }
                       // String re = test.getTextAnswer();
                      /*  if (re.equals("")) {
                            screen.getCurrentPanel().showAlert("Bejelentkezési Hiba!", null);
                            try {
                                Thread.sleep(4000);
                            } catch (InterruptedException ex) {
                                ex.printStackTrace();
                            }
                            screen.getCurrentPanel().setAlertMode(false);
                            return;
                        }*/
                        Epitomize.getDatas(test.getTextAnswer());
                        Vector a1 = new Vector();
                        Vector a2 = new Vector();
                        a1.addElement("un");
                        a2.addElement(Epitomize.userName);
                        MD5 md5 = new MD5(Epitomize.userName + Epitomize.passwd);
                        a1.addElement("pc");
                        a2.addElement(md5.asHex());
                        test.setDataToSend(Epitomize.methodCall("lgn", a1, a2));
                        test.sendRequest();
                        Epitomize.getDatas(test.getTextAnswer());
                        
                        try {
                            Thread.sleep(2000);
                        } catch (InterruptedException ex) {
                            ex.printStackTrace();
                        }
                        screen.getCurrentPanel().setAlertMode(false);
                        llj = true;

                    } catch (ExceptionHandler ex) {
                        try {

                           screen.getCurrentPanel().showAlert(ex.getMessage(), null);
                            Thread.sleep(7000);


                        } catch (InterruptedException ex1) {

                            ex1.printStackTrace();
                        }
                    }
                    Epitomize.isDone = false;
                }
            }
        }).start();
    }

    public void exit() {
        notifyDestroyed();
        return;
    }

    public void destroyApp(boolean unconditional) {
        notifyDestroyed();
        return;
    }

    public boolean isLlj() {
        return llj;
    }

    private Hashtable setTheme(String themeFileName) {
        Hashtable mediaMap = new Hashtable();
        mediaMap.put(FireScreen.THEME_FILE, themeFileName);
        mediaMap.put("box", "box.png");
        mediaMap.put("checkedbox", "checkedbox.png");
        return mediaMap;
    }
}
