package net.informaticalibera.automobile;


import static com.codename1.ui.CN.*;
import com.codename1.ui.Form;
import com.codename1.ui.Dialog;
import com.codename1.ui.Label;
import com.codename1.ui.plaf.UIManager;
import com.codename1.ui.util.Resources;
import com.codename1.io.Log;
import com.codename1.ui.Toolbar;
import com.codename1.ui.layouts.BoxLayout;
import com.codename1.ui.Button;
import com.codename1.ui.FontImage;
import com.codename1.ui.TextField;
import com.codename1.ui.layouts.FlowLayout;

/**
 * This file was generated by <a href="https://www.codenameone.com/">Codename One</a> for the purpose 
 * of building native mobile applications using Java.
 */
public class PrimaApp {

    private Form current;
    private Resources theme;

    public void init(Object context) {
        // use two network threads instead of one
        updateNetworkThreadCount(2);

        theme = UIManager.initFirstTheme("/theme");

        // Enable Toolbar on all Forms by default
        Toolbar.setGlobalToolbar(true);

        // Pro only feature
        Log.bindCrashProtection(true);

        addNetworkErrorListener(err -> {
            // prevent the event from propagating
            err.consume();
            if(err.getError() != null) {
                Log.e(err.getError());
            }
            Log.sendLogAsync();
            Dialog.show("Connection Error", "There was a networking error in the connection to " + err.getConnectionRequest().getUrl(), "OK", null);
        });        
    }
    
    public void start() {
        if(current != null){
            current.show();
            return;
        }
        Form hi = new Form("Automobile", BoxLayout.yLast());
        hi.add(new Label("Inserisci i dati richiesti:"));
        TextField velocita = new TextField("", "Velocità in km/h");
        TextField tempo = new TextField("", "Tempo in minuti");
        Button calcola = new Button("Calcola lo spazio in metri");
        Label risultato = new Label("");
        hi.addAll(velocita, tempo, FlowLayout.encloseCenter(calcola), risultato);
        hi.add(FlowLayout.encloseCenter(new Label(FontImage.createMaterial(FontImage.MATERIAL_DIRECTIONS_CAR, "Label", 50))));
        
        hi.show();
        
        calcola.addActionListener(l -> {
            float kmh = Float.valueOf(velocita.getText());
            int min = Integer.valueOf(tempo.getText());
            float ms = Convert.kmhToMs(kmh);
            int sec = Convert.sec(min);
            MRU automobile = new MRU(sec, (int)ms);
            int spazioPercorso = automobile.spazio();
            risultato.setText("Spazio percorso: " + spazioPercorso + " metri");
            hi.revalidate();
            
            Log.p("Tempo: " + sec + " secondi");
            Log.p("Velocità: " + ms + " metri al secondo");
            Log.p("Spazio percorso: " + spazioPercorso + " metri");
        });
    }

    public void stop() {
        current = getCurrentForm();
        if(current instanceof Dialog) {
            ((Dialog)current).dispose();
            current = getCurrentForm();
        }
    }
    
    public void destroy() {
    }

}
