package com.lcardenas.infoberry;
import net.rim.device.api.system.DeviceInfo;
import net.rim.device.api.system.GPRSInfo;
import net.rim.device.api.system.Memory;
import net.rim.device.api.ui.MenuItem;
import net.rim.device.api.ui.component.Dialog;
import net.rim.device.api.ui.component.LabelField;
import net.rim.device.api.ui.component.Menu;
import net.rim.device.api.ui.component.SeparatorField;
import net.rim.device.api.ui.container.MainScreen;
import net.rim.device.api.ui.container.VerticalFieldManager;
import net.rim.device.api.ui.decor.Background;
import net.rim.device.api.ui.decor.BackgroundFactory;
public class vtnprincipal extends MainScreen  {
    //llamamos a la clase principal
    private InfoBerry padre;
    //variables para el menu
    private MenuItem mnubateria;
    private MenuItem mnuestado;
    private MenuItem mnuacerca;
    public vtnprincipal(InfoBerry padre) {
        super();
        this.padre = padre;
    }
    public void incventana(){
        VerticalFieldManager _ventana = new VerticalFieldManager(VerticalFieldManager.VERTICAL_SCROLL |
                VerticalFieldManager.VERTICAL_SCROLLBAR);
    double tmemoria =((DeviceInfo.getTotalFlashSize()/1024)/1024.00);
    double fmemoria = ((Memory.getFlashFree()/1024)/1024.00);
    Background cyan = BackgroundFactory.createSolidBackground(0x00E0FFFF);
    Background gris = BackgroundFactory.createSolidBackground(0x00DCDCDC );
    //Borramos todos de la pantalla
    this.deleteAll();
    //llamamos al menu
    incMenu();
    //DIBUJAMOS LA VENTANA
    try{
    LabelField title = new LabelField("Info Berry", LabelField.FIELD_HCENTER | LabelField.USE_ALL_HEIGHT );
    setTitle(title);
    _ventana.add(new LabelField("Información del Dispositivo", LabelField.FIELD_HCENTER  |LabelField.RIGHT | LabelField.USE_ALL_HEIGHT | LabelField.NON_FOCUSABLE ));
    _ventana.add(new SeparatorField());
    _ventana.add(new SeparatorField());
    txthorizontal modelo = new txthorizontal("Modelo:", DeviceInfo.getDeviceName());
    modelo.setBackground(gris);
    _ventana.add(modelo);
    txthorizontal pin = new txthorizontal("PIN:" , Integer.toHexString(DeviceInfo.getDeviceId()).toUpperCase());
    pin.setBackground(cyan);
    _ventana.add(pin);
    txthorizontal imeid = new txthorizontal("IMEID:" , GPRSInfo.imeiToString(GPRSInfo.getIMEI()));
    imeid.setBackground(gris);
    _ventana.add(imeid);
    txthorizontal version= new txthorizontal("SO Versión:" ,  DeviceInfo.getSoftwareVersion());
    version.setBackground(cyan);
    _ventana.add(version);
    txthorizontal plataforma= new txthorizontal("SO Plataforma:" ,  DeviceInfo.getPlatformVersion());
    plataforma.setBackground(gris);
    _ventana.add(plataforma);
    txthorizontal numero= new txthorizontal("Numero Telefonico: " ,  "Hay que firmar");
    numero.setBackground(cyan);
    _ventana.add(numero);
    _ventana.add(new SeparatorField());
    _ventana.add(new SeparatorField());
    _ventana.add(new LabelField("Memoria", LabelField.FIELD_HCENTER  | LabelField.USE_ALL_HEIGHT | LabelField.NON_FOCUSABLE));
    _ventana.add(new SeparatorField());
    txthorizontal totalm= new txthorizontal("Memoria app Total:" ,  mmemoria(tmemoria) + " Mb");
    totalm.setBackground(gris);
    _ventana.add(totalm);
    txthorizontal disponiblem= new txthorizontal("Memoria app Disponible:" ,  mmemoria(fmemoria) + " Mb");
    disponiblem.setBackground(cyan);
    _ventana.add(disponiblem);
    ///txthorizontal estadoram = new txthorizontal("Memoria RAM:" ,  mmemoria(prueba) + " Mb");
    //estadoram.setBackground(gris);
    //add(estadoram);
    _ventana.add(new SeparatorField()); 
    _ventana.add(new SeparatorField());
    this.add(_ventana);
    }catch(Exception e){
        Dialog.alert("Excepción en clase vtnprincipal: " + e.toString());
    }
}
//DIBUJAMOS EL MENU
private void incMenu() {
        MenuItem.separator(30);
        mnubateria = new MenuItem("Bateria",40, 10) {
            public void run() {
                bateria();
            }
        };
        mnuestado = new MenuItem("Estado de Red", 50, 10) {
            public void run() {
                estado();
            }
        };
        mnuacerca = new MenuItem("Acerca de..", 60, 10) {
            public void run() {
                acerca();
            }
        };
        MenuItem.separator(70);
};
//
public void makeMenu(Menu menu, int instance) {
    if (!menu.isDisplayed()) {
        menu.deleteAll();
        menu.add(MenuItem.separator(30));
        menu.add(mnubateria);
        menu.add(mnuestado);
        menu.add(mnuacerca);
        menu.add(MenuItem.separator(60));
    }
}
public void bateria(){
    padre.vtnbateria.incventana();
    padre.pushScreen(padre.vtnbateria);
    }
public void estado(){
    padre.vtnestado.incventana();
    padre.pushScreen(padre.vtnestado);
    }
 public void acerca(){
     padre.vtnacerca.incventana();
     padre.pushScreen(padre.vtnacerca);
}
public boolean onClose(){
    Dialog.alert("Hasta Luego");
    System.exit(0);
    return true;
}
public double mmemoria(double x) { 
    if ( x > 0 ) 
         return Math.floor(x * 100) / 100; 
    else 
         return Math.ceil(x * 100) / 100; 
} 
}