package source;

import javax.microedition.lcdui.Canvas;
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;

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

    // Variables
    private DataHandler handler = null;
    private Navigator navi = null;
    private Image rose = null;
    public short direction = 0;
    boolean brun = true;
    int len = 0;

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

    protected void paint(Graphics g) {
        // Spusteni instance pro upravu obrázku
        ImageRoseProccesing R = new ImageRoseProccesing(this);
        this.setFullScreenMode(true);
        g.setColor(255, 255, 255);
        g.fillRect(0, 0, this.getWidth(), this.getHeight());
        len = navi.greader.len;
        // Načte aktualní směr 
        this.direction = this.handler.getDirection();
        this.rose = R.retrieveImage();
        g.drawImage(this.rose, 0, 0, 0);
        g.setColor(0, 0, 0);
        g.drawString("Směr severu je " + this.direction + "\u00B0", 5, this.getWidth() + 5, Graphics.TOP | Graphics.LEFT);
        this.rose = null;
        R = null;

        // Pokud zbyde místo vykresli se další udaje
        int toEnd = this.getHeight() - this.getWidth() + 22;
        if (toEnd > 64) {
            g.drawString("" + this.handler.getLatitude()[0] + "\u00B0 " + this.handler.getLatitude()[1] + "' " + this.handler.getLatitude()[2] + "." + this.handler.getLatitude()[3] + "\"" + this.handler.getNS(), 5, this.getWidth() + 48, Graphics.TOP | Graphics.LEFT);
            g.drawString("" + this.handler.getLongitude()[0] + "\u00B0 " + this.handler.getLongitude()[1] + "' " + this.handler.getLongitude()[2] + "." + this.handler.getLongitude()[3] + "\"" + this.handler.getWE(), 5, this.getWidth() + 28, Graphics.TOP | Graphics.LEFT);
        }
    }

    //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());
            navi.kompas = null;
        }

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

    public void run() {
        while (brun != false) {
            // Premaluje displej
            repaint();
            try {
                // Uspí vlákno na 250ms
                Thread.sleep(250);
            } catch (InterruptedException ex) {
                ex.printStackTrace();
            }
        }
    }
}
