// 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.audio.Meta;
import danger.audio.Tone;
import danger.ui.ScreenWindow;
import danger.ui.StaticText;
import danger.util.DEBUG;

/**
 * Implements the main window class. It is created from a Screen resource. The
 * layout merely shows the current Settings as StaticText items that are updated
 * to properly reflect those values.
 */
public class MainWindow extends ScreenWindow implements Resources, Commands {

    /**
     * Local reference for the application, to avoid the need for coercion.
     */
    private static SettingsDemo sApplication;

    /**
     * Reference variables for the StaticText items in the window, to allow for
     * updating their strings.
     */
    private StaticText mHighScoreText;

    private StaticText mCheckText1;
    private StaticText mCheckText2;
    private StaticText mCheckText3;
    private StaticText mCheckText4;

    private StaticText mRadioText;
    private StaticText mRingToneText;
    private StaticText mEditText;

    // ------------------------------------------------------------------------
    /**
     * This is called after the window has been created from resources. The
     * reason to override this routine is to allow access to the screen widgets,
     * so they can be updated when the Settings change.
     */
    public void onDecoded() {
        // get coerced reference to current App, it's used to update all window
        // widgets.
        sApplication = (SettingsDemo) Application.getCurrentApp();

        // After the window is created, the StaticText fields have default
        // values that will need updating. They will be updated when Settings is
        // sure to be correct.
        // Therefore, find and save a reference to each object of interest.
        mHighScoreText = (StaticText) getDescendantWithID(ID_HIGHSCORE);

        mCheckText1 = (StaticText) getDescendantWithID(ID_CHECKSTATE_1);
        mCheckText2 = (StaticText) getDescendantWithID(ID_CHECKSTATE_2);
        mCheckText3 = (StaticText) getDescendantWithID(ID_CHECKSTATE_3);
        mCheckText4 = (StaticText) getDescendantWithID(ID_CHECKSTATE_4);

        mRadioText = (StaticText) getDescendantWithID(ID_RADIO_STATE);
        mRingToneText = (StaticText) getDescendantWithID(ID_RINGTONE_STATE);
        mEditText = (StaticText) getDescendantWithID(ID_EDITTEXT_STATE);

        super.onDecoded();
    }

    /**
     * To handle a private shortcut, this routine overrides the Containers'
     * routine, and looks for the Menu-X command. It's not user visible, and in
     * this case is expected to only be useful for debugging purposes. It will
     * reset a used DB back to the first run empty state. The Application does
     * the work.
     */
    public boolean eventShortcut(char shortcut, Event event) {
        if (shortcut == 'X') {
            sApplication.deleteAllRequest();
            DEBUG.p("MainWindow: EVENT_DELETE_ALL_SETTINGS");
            return true;
        } else {
            return false;
        }
    }

    /**
     * Handles events. Called automatically whenever this window receives an
     * event. The only events handled are related to the menu.  All other 
     * events are handled automatically by the window, by calling super.
     */
    public boolean receiveEvent(Event e) {
        switch (e.type) {
        case EVENT_SCORE_1000:
            Settings.getInstance().setHighScore(1000);
            sApplication.refreshWindows();
            DEBUG.p("MainWindow: EVENT_SCORE_1000");
            return true;

        case EVENT_SCORE_2000:
            Settings.getInstance().setHighScore(2000);
            sApplication.refreshWindows();
            DEBUG.p("MainWindow: EVENT_SCORE_2000");
            return true;

        case EVENT_GENERAL_SETTINGS:
            // Upon receiving the General:Settings menu, show the already
            // created window, which will then be accepting all further events.
            sApplication.getScreenWindow().show();
            DEBUG.p("MainWindow: EVENT_GENERAL_SETTINGS");
            return true;
        }

        return super.receiveEvent(e);
    }

    /**
     * Handles device buttons, specifically the Jump, Cancel, and Done 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. The
        // DEVICE_BUTTON_CANCEL event is defined in the Hiptop2Events.java
        // file. On original hiptop devices, this event is never sent. On
        // hiptop2 devices, this event will be received and handled here.
        // In the 2.0 API realm, this could be "Event.DEVICE_BUTTON_CANCEL"
        // instead.
        case Hiptop2Events.DEVICE_BUTTON_CANCEL: {
            sApplication.returnToLauncher();
            return true;
        }

        // For both the Cancel or Back/Done button from the MainWindow, it
        // will jump back to the launcher screen to provide a better
        // experience.
        case EventType.DEVICE_BUTTON_BACK: {
            sApplication.returnToLauncher();
            return true;
        }
        }

        // Always make sure to call super.eventWidgetUp for other events so the
        // system can execute the correct behavior (e.g. returning to the Jump
        // screen with phone selected).
        return super.eventWidgetUp(inWhichWidget, inEvent);
    }

    /**
     * Make all this window's static text items match the current states in the
     * Settings object.
     */
    public void setAllStates() {
        Settings theSettings = Settings.getInstance();

        mHighScoreText.setText(String.valueOf(theSettings.getHighScore()));

        mCheckText1.setText(String.valueOf(theSettings.getCheckBox1()));
        mCheckText2.setText(String.valueOf(theSettings.getCheckBox2()));
        mCheckText3.setText(String.valueOf(theSettings.getCheckBox3()));
        mCheckText4.setText(String.valueOf(theSettings.getCheckBox4()));

        mRadioText.setText(String.valueOf(theSettings.getRadioButton()));

        // To be 1.1 compatible, this uses the MetaID to get the actual name.
        // This is compatible with the 2.0 APIs too.
        // Under the 2.0 API, this could just be getRingTone().getName()
        Tone theTone = Meta.getToneFromID(theSettings.getRingTone());
        if (theTone != null) {
            mRingToneText.setText(theTone.getName());
        } else {
            mRingToneText.setText("null");
        }

        mEditText.setText(theSettings.getEditText());
    }
}
