import java.io.*;
import marimba.channel.*;
import marimba.gui.*;
import marimba.io.*;

public class disasters extends ApplicationPlayerFrame {


    public void start() {
        super.start();
        readState();
    }


    public void stop() {
        saveState();
        super.stop();
    }

    public void saveState() {
        File f = new File(context.getDataDirectory(),"state.dat");
        Widget wList[] = util.getPresentation().widgets;
        String date = util.getWidget("date").getText() + "\n";
        try{
	        FastOutputStream fos = new FastOutputStream(f);
	        fos.writeBytes(date);
            for(int i = 0;i < wList.length;i++) {
                if(wList[i] instanceof CommandButtonWidget) {
                    fos.writeBoolean(((CommandButtonWidget) wList[i]).getBooleanValue());
                }
            }
	        fos.close();
	    }
    	catch(IOException e) {
	        e.printStackTrace();
	    }

    }

    public void readState() {
        File f = new File(context.getDataDirectory(),"state.dat");
        Widget wList[] = util.getPresentation().widgets;
        String date = util.getWidget("date").getText();
        try{
	        FastInputStream fis = new FastInputStream(f);
	        if (date.equals(fis.readLine())) {
                for(int i = 0;i < wList.length;i++) {
                    if(wList[i] instanceof CommandButtonWidget) {
                        ((CommandButtonWidget) wList[i]).setValue(fis.readBoolean());
                        if (((CommandButtonWidget) wList[i]).getBooleanValue()) {
                            ((CommandButtonWidget) wList[i]).action();
                        }
                    }
                }
            }
	        fis.close();
	    }
    	catch(IOException e) {
	        e.printStackTrace();
	    }

    }

}