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

import java.io.IOException;
import java.io.OutputStream;
import javax.microedition.io.Connector;
import javax.microedition.io.file.FileConnection;

/**
 *
 * @author Radoslaf
 */
public class SaveToFile extends Thread {

    private DataHandler handler;
    private Navigator navi;
    private int c = 0;

    public SaveToFile(Navigator navi, DataHandler handler) {
        this.handler = handler;
        this.navi = navi;
        Thread th = new Thread();
        th.start();
    }

    public void run() {
        try {
            this.saveToFile();
        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }

    private synchronized void saveToFile() throws IOException {

        String text = navi.getSaveTextBox().getString();
        String name = "Navigator" + navi.savecount + ".txt";
        // Příklad direcoty file:///C:/data/Images/Image(001).jpg
        String dir1 = "c:/";
        String dir2 = "e:/";
        String path = null;

        // Upozorněni na chybějící text bude zobrazeno jen jednou
        c = 0;

        int sel = navi.getMem_choice().getSelectedIndex();
        if (sel == 0) {
            path = "file:///" + dir1;
        }
        if (sel == 1) {
            path = "file:///" + dir2;
        }


        if (text == null && c != 1) {
            navi.getInfo().setTimeout(-2);
            navi.getInfo().setString("Nebyl nalezen doplňující text.");
            navi.switchDisplayable(null, navi.getInfo());
            c++;
        } // Pokud je text obsažen dojde okamžitě k uložení
        else {
            navi.getInfo().setTimeout(2000);
            navi.getInfo().setString("Data jsou ukládána.");
            navi.switchDisplayable(null, navi.getInfo());

            // Ukládání dat
            try {
                //String url = path + name;
                byte data[] = this.toText(text).getBytes("UTF-8");
                FileConnection newDir = (FileConnection) Connector.open(path+"Navigator",Connector.READ_WRITE);
                if(!newDir.exists()) {
                    newDir.mkdir();
                }
                newDir.close();
                String url = path+"Navigator/" + name;
                FileConnection fconn = (FileConnection) Connector.open(url, Connector.READ_WRITE);
                if (!fconn.exists()) {
                    fconn.create();
                }
                OutputStream ops = fconn.openOutputStream();
                ops.write(data);
                ops.close();
                fconn.close();
                this.navi.savecount++;
                this.navi.getLoadData().setString("");
                this.navi.switchDisplayable(null, this.navi.getNavi());
            } catch (IOException ioe) {
                System.out.println("IOException: " + ioe.getMessage());
            } catch (SecurityException se) {
                System.out.println("Security exception:" + se.getMessage());
            }
        }
    }

    private String toText(String userText) {
        String text = null;
        text = new String(
                "====================" + "\n" +
                "Datum: " + this.handler.getDatum()[0] + "." + this.handler.getDatum()[1] + "." + this.handler.getDatum()[2] + "\n" +
                "Čas: " + this.handler.getTime()[0] + ":" + this.handler.getTime()[1] + ":" + this.handler.getTime()[2] + "\n" +
                "--------------------" + "\n" +
                "Zeměpisná šířka: " + this.handler.getLongitude()[0] + "\u00B0 " + this.handler.getLongitude()[1] + "' " + this.handler.getLongitude()[2] + "." + this.handler.getLongitude()[3] + "\"" + this.handler.getNS() + "\n" +
                "Zeměpisná délka: " + this.handler.getLatitude()[0] + "\u00B0 " + this.handler.getLatitude()[1] + "' " + this.handler.getLatitude()[2] + "." + this.handler.getLatitude()[3] + "\"" + this.handler.getWE() + "\n" +
                "Nadmořská výška: " + this.handler.getElevation() + "\n" +
                "--------------------" + "\n" +
                "Popis: " + userText + "\n" +
                "====================");
        return text;
    }
}
