/*
*/

import javax.microedition.lcdui.*;
import javax.microedition.midlet.MIDlet;

public class AlarmConfigurer extends MIDlet implements CommandListener {

    private final static Command CMD_OK =
	    new Command("Save", Command.OK, 1);

    private final static Command CMD_BACK =
	    new Command("Back", Command.BACK, 1);

    private Display display;

    private List mainList;
    private List multipleList;
    private Form AutoOffForm;
    private Form NightModeForm;
    private Form VolumeHourForm;
    
    private TextField HourOffField;
    private TextField HourBegField;
    private TextField HourEndField;
    private TextField VolumeHourField;
    
    private AlarmConfig alarmCfg;
    private boolean firstTime;

    public AlarmConfigurer() {
        display = Display.getDisplay(this);

        String[] stringArray = {
            "До сигнала", 
            "После сигнала", 
            "При автовключении", 
            "Каждый час",
            "В режиме без звука"
        };

        // the string elements will have no images
        Image[] imageArray = null;

        HourOffField = new TextField("в час:","",2,TextField.NUMERIC);
        HourBegField = new TextField("в час:","",2,TextField.NUMERIC);
        HourEndField = new TextField("в час:","",2,TextField.NUMERIC);
        VolumeHourField = new TextField("ед.:","",1,TextField.NUMERIC);

        alarmCfg = new AlarmConfig();
        alarmCfg.loadConfig();
        
        multipleList = new List("Говорить время", Choice.MULTIPLE, stringArray, 
                                imageArray);
        multipleList.addCommand(CMD_BACK);
        multipleList.addCommand(CMD_OK);
        multipleList.setCommandListener(this);
        
        NightModeForm = new Form("Ночной режим");
        NightModeForm.append("Включить ночной режим вечером");
        NightModeForm.append(HourBegField);
        NightModeForm.append("Выключить ночной режим утром");
        NightModeForm.append(HourEndField);
        NightModeForm.append("Ночной режим влияет только на произношение каждый час!");
        NightModeForm.addCommand(CMD_BACK);
        NightModeForm.addCommand(CMD_OK);
        NightModeForm.setCommandListener(this);
        
        VolumeHourForm = new Form("Громкость hour.mmf");
        VolumeHourForm.append("Воспроизводить hour.mmf с громкостью");
        VolumeHourForm.append(VolumeHourField);
        VolumeHourForm.append("0 - с громкостью звонка");
        VolumeHourForm.append("6 - с громкостью клавиатуры");
        VolumeHourForm.addCommand(CMD_BACK);
        VolumeHourForm.addCommand(CMD_OK);
        VolumeHourForm.setCommandListener(this);

        AutoOffForm = new Form("Автовыключение");
        AutoOffForm.append("Выключить телефон");
        AutoOffForm.append(HourOffField);
        AutoOffForm.append("Больше 23 - функция отключена");
        AutoOffForm.addCommand(CMD_BACK);
        AutoOffForm.addCommand(CMD_OK);
        AutoOffForm.setCommandListener(this);
        
        firstTime = true;
    }

    protected void startApp() {
        if (firstTime) {
            // these are the images and strings for the choices.
            Image[] imageArray = null;
    
            try {
                // load the duke image to place in the image array
                Image icon = Image.createImage("/img/Icon.png");
    
    
                // these are the images and strings for the choices.
                imageArray = new Image[] {
                    icon, 
                    icon, 
                    icon, 
                    icon
                };
            } catch (java.io.IOException err) {
                // ignore the image loading failure the application can recover.
            }
    
            String[] stringArray = {
                "Говорить время", 
                "Ночной режим", 
                "Громкость",
                "Автовыключение"
            };
            mainList = new List("Alarm configurer", Choice.IMPLICIT, stringArray, 
                                imageArray);
            mainList.addCommand(CMD_BACK);
            mainList.setCommandListener(this);
            display.setCurrent(mainList);
            firstTime = false;
        }
    }

    protected void destroyApp(boolean unconditional) {
    }

    protected void pauseApp() {
    }

    public void commandAction(Command c, Displayable d) {
        
        if (d.equals(mainList)) {
            // in the main list
            if (c == List.SELECT_COMMAND) {
                if (d.equals(mainList)) {
                    switch (((List)d).getSelectedIndex()) {
                        case 0:
                            multipleList.setSelectedFlags(alarmCfg.speakFlags);
                            display.setCurrent(multipleList);
                            break;
                        case 1:
                            HourBegField.setString(alarmCfg.hourBegNightMode);
                            HourEndField.setString(alarmCfg.hourEndNightMode);
                            display.setCurrent(NightModeForm);
                            break;
                        case 2:
                            VolumeHourField.setString(alarmCfg.volumeHourMmf);
                            display.setCurrent(VolumeHourForm);
                            break;
                        case 3:
                            HourOffField.setString(alarmCfg.hourAutoOff);
                            display.setCurrent(AutoOffForm);
                            break;
                    }
                }
            } else if (c == CMD_BACK) {
               destroyApp(false);
               notifyDestroyed();
            }
            
        } else if (d.equals(multipleList)) {
            if (c == CMD_BACK) {
                display.setCurrent(mainList);
            } else if (c == CMD_OK) {
                multipleList.getSelectedFlags(alarmCfg.speakFlags);
                alarmCfg.saveConfig();
                display.setCurrent(mainList);
            }

        } else if (d.equals(NightModeForm)) {
            if (c == CMD_BACK) {
                display.setCurrent(mainList);
            } else if (c == CMD_OK) {
                alarmCfg.hourBegNightMode = HourBegField.getString();
                alarmCfg.hourEndNightMode = HourEndField.getString();
                alarmCfg.saveConfig();
                display.setCurrent(mainList);
            }

        } else if (d.equals(VolumeHourForm)) {
            if (c == CMD_BACK) {
                display.setCurrent(mainList);
            } else if (c == CMD_OK) {
                alarmCfg.volumeHourMmf = VolumeHourField.getString();
                alarmCfg.saveConfig();
                display.setCurrent(mainList);
            }

        } else if (d.equals(AutoOffForm)) {
            if (c == CMD_BACK) {
                display.setCurrent(mainList);
            } else if (c == CMD_OK) {
                alarmCfg.hourAutoOff = HourOffField.getString();
                alarmCfg.saveConfig();
                display.setCurrent(mainList);
            }
        }
    }


}

