//Copyright 2001-2005, Danger, Inc.  All Rights Reserved.
//This file is subject to the Danger, Inc. Sample Code License,
//which is provided in the file SAMPLE_CODE_LICENSE.
//Copies are also available from http://developer.danger.com/

package com.example.SettingsDemo;

import danger.app.Application;
import danger.app.Event;
import danger.app.EventType;
import danger.ui.ScreenWindow;
import danger.ui.Control;
import danger.ui.RadioGroup;
import danger.ui.RingTonePicker;
import danger.ui.EditText;
import danger.util.DEBUG;

/**
 * Implements the SettingsScreen class. To override the ScreenWindow used for
 * settings. This sub-class is specified in the resource definition, so that
 * when the window is created from resources, it creates this SettingsScreen
 * object, not the standard ScreenWindow object.
 * 
 * This is sub-classed so it can handle user events, specifically the Cancel
 * button and the Back/Done button.
 */
public class SettingsScreen extends ScreenWindow implements Resources {

    /**
     * Becomes a common reference to the currently running application. static,
     * because there is only ever one.
     */
    private static SettingsDemo sApplication;

    /**
     * Instance references to the run time created window widgets, so we can
     * save their state when Done is clicked.
     */
    private Control mCheckBox1;
    private Control mCheckBox2;
    private Control mCheckBox3;
    private Control mCheckBox4;

    private RadioGroup mRadioGroup;
    private RingTonePicker mRingTonePicker;
    private EditText mEditText;

    // ------------------------------------------------------------------------
    /**
     * When the object is created from resources, this routine is called after
     * the window and all its objects have been constructed.
     * 
     * The primary thing to do here is to save references to the objects needed
     * to complete the task, specifically the SettingsDemo Application object,
     * and each of the control widgets that save state that we need to grab upon
     * Done.
     */
    public void onDecoded() {
        // Save a reference to the current Application, "SettingsDemo". This
        // makes it possible to have a single coerced GetCurrentApp call here.
        // Coercion is generally undesirable and can lead to bugs.
        sApplication = (SettingsDemo) Application.getCurrentApp();

        // After onDecoded, the whole window has been created from its resource
        // default settings. These settings will need to be passed over to the
        // Settings object to be saved to the database when the user clicks
        // Done.
        // Therefore, find and save a reference to each object of interest.
        mCheckBox1 = getControlWithID(ID_CHECKBOX_1);
        mCheckBox2 = getControlWithID(ID_CHECKBOX_2);
        mCheckBox3 = getControlWithID(ID_CHECKBOX_3);
        mCheckBox4 = getControlWithID(ID_CHECKBOX_4);

        // These three need coercion because the getDescendant returns a View.
        mRadioGroup = (RadioGroup) getDescendantWithID(ID_RADIOBUTTON_GROUP);
        mRingTonePicker = (RingTonePicker) getDescendantWithID(ID_RINGTONE_PICKER);
        mEditText = (EditText) getDescendantWithID(ID_EDITTEXT);

        // Call the super class, as views can be nested.
        super.onDecoded();
    }

    /**
     * This receives events for the SettingsScreen window. Specifically the
     * Done/Cancel buttons.
     */
    public boolean eventWidgetUp(int inWhichWidget, Event inEvent) {
        switch (inWhichWidget) {
        // This demonstrates how to use a control from hiptop2 in a way that is
        // still compatible with original hiptop devices. This handles the
        // DEVICE_BUTTON_CANCEL event which is defined in the Hiptop2Events.java
        // file. On original hiptop devices, this event is never sent. On
        // hiptop2 devices, the event will be received and handled here.
        case Hiptop2Events.DEVICE_BUTTON_CANCEL:
            // Upon cancel, there is nothing to save. Hide window to allow
            // the MainWindow to resume.
            hide();

            // But, the dialog widgets might mismatch Settings,
            // so restore them to keep them in sync.
            sApplication.refreshWindows();

            DEBUG.p("SettingsScreen: DEVICE_BUTTON_CANCEL");
            return true;

        case EventType.DEVICE_BUTTON_BACK:
            // This is the OK/Done/Back button, save the current states of
            // the UI widgets into the Settings object, and thus write them
            // to the DB if needed.
            saveAllUISettings();

            // Hide self, as the Done state, and the MainWindow will come
            // back up.
            hide();

            // The user clicked Done, so refresh all of the window items to
            // make them match the new settings state.
            sApplication.refreshWindows();

            DEBUG.p("SettingsScreen: DEVICE_BUTTON_BACK");
            return true;

        case EventType.DEVICE_BUTTON_JUMP:
            // Always make sure to call super.eventWidgetUp for this control
            // so the system can execute the correct behavior (returning to
            // the Jump screen with phone selected).
            break;
        }

        // For events not handled here, send them to the superclass.
        return super.eventWidgetUp(inWhichWidget, inEvent);
    }

    /**
     * Get the current state of the window widgets who need to be saved. Send
     * their states to the Settings object where they belong. This routine is
     * called when the SettingScreen states are to replace the ones always saved
     * in Settings.
     */
    public void saveAllUISettings() {
        Settings theSettings = Settings.getInstance();

        theSettings.setCheckBox1(mCheckBox1.getValue());
        theSettings.setCheckBox2(mCheckBox2.getValue());
        theSettings.setCheckBox3(mCheckBox3.getValue());
        theSettings.setCheckBox4(mCheckBox4.getValue());

        theSettings.setRadioButton(mRadioGroup.getValue());
        theSettings.setRingTone(mRingTonePicker.getRingTone());
        theSettings.setEditText(mEditText.toString());
    }

    /**
     * Change the state of the window widgets to match the values saved in the
     * Settings object. If the Settings screen is currently showing, this will
     * update the widgets live.  That can happen when the DATASTORE_RESTORED.
     */
    public void restoreAllUISettings() {
        Settings theSettings = Settings.getInstance();

        mCheckBox1.setValue(theSettings.getCheckBox1());
        mCheckBox2.setValue(theSettings.getCheckBox2());
        mCheckBox3.setValue(theSettings.getCheckBox3());
        mCheckBox4.setValue(theSettings.getCheckBox4());

        mRadioGroup.setValue(theSettings.getRadioButton());

        mRingTonePicker.setRingTone(theSettings.getRingTone());
        mEditText.setText(theSettings.getEditText());
    }
}
