/*

*/

import javax.microedition.rms.RecordStore;
import javax.microedition.rms.RecordStoreException;

public class AlarmConfig {

    public boolean[] speakFlags;
    public String hourAutoOff;
    public String volumeHourMmf;
    public String hourBegNightMode;
    public String hourEndNightMode;

    private static RecordStore myStore;
    
    public AlarmConfig() {
        speakFlags = new boolean[5];
    }

    public void loadConfig() {
        byte[] data = new byte[9];
        
        try {
            myStore = RecordStore.openRecordStore("cfg", true);

            data = myStore.getRecord(1);

            for (int i = 0; i < 5; i++) {
                if (data[i] == 0) {
                    speakFlags[i] = false;
                } else {
                    speakFlags[i] = true;
                }
            }

            hourBegNightMode = Integer.toString(data[5] & 0xFF);
            hourEndNightMode = Integer.toString(data[6] & 0xFF);
            volumeHourMmf = Integer.toString(data[7] & 0xFF);
            hourAutoOff = Integer.toString(data[8] & 0xFF);

        } catch (RecordStoreException rse) {
            speakFlags[0] = true;
            speakFlags[1] = false;
            speakFlags[2] = true;
            speakFlags[3] = true;
            speakFlags[4] = false;

            hourAutoOff = "99";
            volumeHourMmf = "6";
            hourBegNightMode = "23";
            hourEndNightMode = "7";

            this.saveConfig();
        }

        try {
            myStore.closeRecordStore();
        } catch (RecordStoreException frse) {
            /* Silently fail */
        }
    
    }

    public void saveConfig() {
        byte[] data = new byte[9];
        int byte2save;
        
        for (int i = 0; i < 5; i++) {
            if (speakFlags[i]) {
                data[i] = 1;
            } else {
                data[i] = 0;
            }
        }

        byte2save = Integer.parseInt(hourBegNightMode) & 0xFF;
        data[5] = (byte) byte2save;

        byte2save = Integer.parseInt(hourEndNightMode) & 0xFF;
        data[6] = (byte) byte2save;

        byte2save = Integer.parseInt(volumeHourMmf) & 0x0F;
        data[7] = (byte) byte2save;

        byte2save = Integer.parseInt(hourAutoOff) & 0xFF;
        data[8] = (byte) byte2save;

        try {
            myStore = RecordStore.openRecordStore("cfg", true);
            if (myStore.getNumRecords() == 0) {
                myStore.addRecord(data, 0, 9);
            } else {
                myStore.setRecord(1, data, 0, 9);
            }
        } catch (RecordStoreException rse) {
        }

        try {
            myStore.closeRecordStore();
        } catch (RecordStoreException frse) {
            /* Silently fail */
        }

    }

}
