/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package source;

import javax.microedition.lcdui.Canvas;

import javax.microedition.lcdui.Graphics;

/**
 *
 * @author Radoslaf
 */
public class ShowInfo extends Canvas implements Runnable{

    private Navigator navi;
    private DataHandler handler;
    private boolean brun = true;

    // Konstruktor, start vlakna
    public ShowInfo(Navigator navi, DataHandler handler) {
        this.navi = navi;
        this.handler = handler;
        Thread th = new Thread(this);
        th.start();
    }


    protected synchronized void paint(Graphics g){
        this.setFullScreenMode(true);
        g.setColor(255, 255, 255);
        g.fillRect(0, 0, this.getWidth() , this.getHeight());
        g.setColor(0, 0, 0);
        // Generování popisků
        g.setFont(navi.getPopis());
        g.drawString("Zeměpisná šířka", 5, 5, Graphics.TOP|Graphics.LEFT);
        g.drawString("Zeměpisná délka", 5, 50, Graphics.TOP|Graphics.LEFT);
        g.drawString("Nadmořská výška", 5, 95, Graphics.TOP|Graphics.LEFT);
        g.drawString("Rychlost", 5, 140, Graphics.TOP|Graphics.LEFT);
        g.drawString("Přesnost [H x V]", 5, 185, Graphics.TOP|Graphics.LEFT);
        g.drawString("Datum", 5, 230, Graphics.TOP|Graphics.LEFT);
        g.drawString("Čas", 5, 275, Graphics.TOP|Graphics.LEFT);
        
        // Generování dat
        g.setFont(navi.getGPSdata());
        String delka = new String(this.handler.getLongitude()[0] + "\u00B0 " + this.handler.getLongitude()[1] + "' " + this.handler.getLongitude()[2] + "." + this.handler.getLongitude()[3] + "\"" + this.handler.getWE());
        String sirka = new String(this.handler.getLatitude()[0] + "\u00B0 " + this.handler.getLatitude()[1] + "' " + this.handler.getLatitude()[2] + "." + this.handler.getLatitude()[3] + "\"" + this.handler.getNS());
        String nadvyska = new String(this.handler.getElevation() + " m");
        String rychlost = new String(this.handler.getSpeed() + " km/h");
        // Cislo 6 je zvoleno protoze neni pouzita dalsi metoda presnosti
        // vice http://www.geoframeworks.com/Articles/WritingApps2_4.aspx
        String presnost = new String((int)this.handler.getHDOP()*6 + " x " + (int)this.handler.getVDOP()*6 + " m");
        String datum = new String(this.handler.getDatum()[0] + "." + this.handler.getDatum()[1]+ "."+ this.handler.getDatum()[2]);
        String cas = new String(this.handler.getTime()[0] + ":" + this.handler.getTime()[1] + ":" + this.handler.getTime()[2]);

        g.drawString(sirka, this.getWidth()-5, 25, Graphics.TOP|Graphics.RIGHT);
        g.drawString(delka, this.getWidth()-5, 70, Graphics.TOP|Graphics.RIGHT);
        g.drawString(nadvyska, this.getWidth()-5, 115, Graphics.TOP|Graphics.RIGHT);
        g.drawString(rychlost, this.getWidth()-5, 160, Graphics.TOP|Graphics.RIGHT);
        g.drawString(presnost, this.getWidth()-5, 205, Graphics.TOP|Graphics.RIGHT);
        g.drawString(datum, this.getWidth()-5, 250, Graphics.TOP|Graphics.RIGHT);
        g.drawString(cas, this.getWidth()-5, 295, Graphics.TOP|Graphics.RIGHT);

    }

    //Zmáčknutí klávesy
    protected  void keyPressed(int keyCode) {

        // Ukončení zobrazování Kompasu
        if (keyCode == KEY_NUM5) {
            brun = false;
            navi.switchDisplayable(null, navi.getNavi());
            // Smazání instance
            navi.geninfo = null;

        }

        // Vyvolání formuláře uložení
        if (keyCode == KEY_NUM2) {
            brun = false;
            navi.switchDisplayable(null, navi.getSaveForm());
            // Smazání instance
            navi.geninfo = null;
        }
    }

    public void run() {
        while (brun != false) {
            // Premaluje displej
            repaint();
            try {
                // Displej se překreslí 4x za sekundu
                Thread.sleep(250);
            } catch (InterruptedException ex) {
                ex.printStackTrace();
            }
        }
    }

}
