// Copyright 2001-2003, 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.danger.chessclock;

import danger.app.Event;
import danger.app.Listener;
import danger.app.SettingsDB;
import danger.app.SettingsDBException;
import danger.audio.RingToneObject;
import danger.ui.Container;
import danger.ui.Control;
import danger.ui.DialogWindow;
import danger.ui.RingTonePicker;

/**
 * manager for the preferences (aka settings) for the ChessClock.
 */
public class ClockPrefs
	extends Listener
{
	/** non-null; the application object */
	private ChessClock mApp;

	/** non-null; datastore for all the prefs */
	private SettingsDB mSettings;

	// the prefs
	public int mInitialHours1;
	public int mInitialMinutes1;
	public int mInitialSeconds1;
	public int mInitialHours2;
	public int mInitialMinutes2;
	public int mInitialSeconds2;
	public boolean mGoNegative;
	public RingToneObject mExpirationTone;
	public boolean mJumpBackSwitch;



	// ------------------------------------------------------------------------
	// constructors

	/**
	 * Construct an instance.
	 */
	public ClockPrefs(ChessClock app)
	{
		mApp = app;

		boolean	already = SettingsDB.findDB("prefs");
		mSettings = new SettingsDB("prefs", true);
		if (already) {
			readSettingsDB();
		}
		else {
			// defaults, used when there's no preexisting settings db
			mInitialHours1 = 0;
			mInitialMinutes1 = 15;
			mInitialSeconds1 = 0;
			mInitialHours2 = 0;
			mInitialMinutes2 = 15;
			mInitialSeconds2 = 0;
			mGoNegative = true;
			mExpirationTone = null;
			mJumpBackSwitch = false;
		}
	}



	// ------------------------------------------------------------------------
	// public instance methods

	// standard UI method
	public boolean receiveEvent(Event e)
	{
		switch (e.type) {
			case Events.CONFIGURE_DONE: {
				grabSettings((Container) e.argument);
				return true;
			}
			case Event.EVENT_DATASTORE_RESTORED:
				readSettingsDB();
				return true;
		}

		return super.receiveEvent(e);
	}

	/**
	 * Display and handle the prefs dialog.
	 */
	public void show()
	{
		DialogWindow dlog = mApp.getDialog(Resources.CONFIGURE_DIALOG, this);
		putSettings(dlog);
		dlog.show();
	}

	/**
	 * Update the settings database.
	 */
	public void updateSettingsDB()
	{
		mSettings.clearAll();

		mSettings.setIntValue("initial-hours-1", mInitialHours1);
		mSettings.setIntValue("initial-minutes-1", mInitialMinutes1);
		mSettings.setIntValue("initial-seconds-1", mInitialSeconds1);

		mSettings.setIntValue("initial-hours-2", mInitialHours2);
		mSettings.setIntValue("initial-minutes-2", mInitialMinutes2);
		mSettings.setIntValue("initial-seconds-2", mInitialSeconds2);

		mSettings.setIntValue("go-negative", mGoNegative ? 1 : 0);

		if (mExpirationTone == null) {
			mSettings.setIntValue("expiration-tone", 0);
		}
		else {
			mSettings.setIntValue("expiration-tone", mExpirationTone.getID());
		}

		mSettings.setIntValue("jump-back-switch", mJumpBackSwitch ? 1 : 0);
	}



	// ------------------------------------------------------------------------
	// private instance methods

	/**
	 * Grab new settings out of a configuration dialog.
	 */
	private void grabSettings(Container dlog)
	{
		mInitialHours1 = 
			((Control) dlog.getDescendantWithID(Resources.EDIT_HOURS1))
			.getValue();

		mInitialMinutes1 = 
			((Control) dlog.getDescendantWithID(Resources.EDIT_MINUTES1))
			.getValue();

		mInitialSeconds1 = 
			((Control) dlog.getDescendantWithID(Resources.EDIT_SECONDS1))
			.getValue();

		mInitialHours2 = 
			((Control) dlog.getDescendantWithID(Resources.EDIT_HOURS2))
			.getValue();

		mInitialMinutes2 = 
			((Control) dlog.getDescendantWithID(Resources.EDIT_MINUTES2))
			.getValue();

		mInitialSeconds2 = 
			((Control) dlog.getDescendantWithID(Resources.EDIT_SECONDS2))
			.getValue();

		mGoNegative = 
			((Control) dlog.getDescendantWithID(Resources.CHECK_NEGATIVE))
			.getValue() == 1;

		mExpirationTone =
			((RingTonePicker) dlog.getDescendantWithID(Resources.RING_TONE))
			.getRingTone();

		// now deal with the settings db
		updateSettingsDB();
	}

	/**
	 * Put the current settings into a configuration dialog.
	 */
	private void putSettings(Container dlog)
	{
		((Control) dlog.getDescendantWithID(Resources.EDIT_HOURS1))
			.setValue(mInitialHours1);

		((Control) dlog.getDescendantWithID(Resources.EDIT_MINUTES1))
			.setValue(mInitialMinutes1);

		((Control) dlog.getDescendantWithID(Resources.EDIT_SECONDS1))
			.setValue(mInitialSeconds1);

		((Control) dlog.getDescendantWithID(Resources.EDIT_HOURS2))
			.setValue(mInitialHours2);

		((Control) dlog.getDescendantWithID(Resources.EDIT_MINUTES2))
			.setValue(mInitialMinutes2);

		((Control) dlog.getDescendantWithID(Resources.EDIT_SECONDS2))
			.setValue(mInitialSeconds2);

		((Control) dlog.getDescendantWithID(Resources.CHECK_NEGATIVE))
			.setValue(mGoNegative ? 1 : 0);

		((RingTonePicker) dlog.getDescendantWithID(Resources.RING_TONE))
			.setRingTone(mExpirationTone);
	}

	/**
	 * Read the settings database, setting the instance variables of this
	 * instance accordingly.
	 */
	private void readSettingsDB()
	{
		try {
			mInitialHours1 = mSettings.getIntValue("initial-hours-1");
			mInitialMinutes1 = mSettings.getIntValue("initial-minutes-1");
			mInitialSeconds1 = mSettings.getIntValue("initial-seconds-1");
			
			mInitialHours2 = mSettings.getIntValue("initial-hours-2");
			mInitialMinutes2 = mSettings.getIntValue("initial-minutes-2");
			mInitialSeconds2 = mSettings.getIntValue("initial-seconds-2");

			mGoNegative = mSettings.getIntValue("go-negative") != 0;

			int expirationID = mSettings.getIntValue("expiration-tone");
			if (expirationID == 0) {
				mExpirationTone = null;
			}
			else {
				mExpirationTone = new RingToneObject(expirationID);
			}

			mJumpBackSwitch = mSettings.getIntValue("jump-back-switch") != 0;
		}
		catch (SettingsDBException ex) {
			ex.printStackTrace();
		}
	}
}
