// Copyright 2001-2004, 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.GUIDemo;

import danger.app.Application;
import danger.text.span.SpanUtils;
import danger.text.span.SpannableString;
import danger.ui.Button;
import danger.ui.DialogWindow;
import danger.ui.LinkTextBox;
import danger.ui.LogTextBox;
import danger.ui.MultiFolderView;
import danger.ui.MutableTextBox;
import danger.ui.PhoneNumberTextField;
import danger.ui.ScreenWindow;
import danger.ui.ScrollingTextBox;
import danger.ui.SelectTextBox;
import danger.ui.Shortcut;
import danger.ui.ToolTipWindow;
import danger.ui.photopicker.PhotoPicker;
import danger.util.Folder;
import danger.util.TextBuffer;

/**
 * This class encapsulates the version specific code necessary for maintaining
 * the 2.0 and 2.3 versions of the application.
 * 
 * These are utility routines, that perform the version specific tasks. The
 * routines are marked static so that they can be called without needing
 * instantiation.
 * 
 * This file is for the 2.3 variant of the code.
 * 
 * These are the actual routines to create GUI widgets under 2.3.
 */
public class Version implements Resources {

	/**
	 * The PhotoPicker interface is different on 2.0 and 2.3. This is for the
	 * 2.3 approach.
	 */
	public static PhotoPicker showPhotoPicker() {
		PhotoPicker picker = PhotoPicker.createPicker();
		// picker.show();
		return (picker);
	}

	/**
	 * Return the short style Date string for the passed in Date. For this 2.3
	 * variant, it is superior to use the DateFormat function to provide
	 * internationalization of the returned string. It will use the current
	 * locale to format properly.
	 */
	public static void showLogTextBox() {
		LRDialogWindow dw;
		String[] viewStrings = Application.getCurrentApp().getStringArray(
				ID_LOGTEXTBOX_STRINGS);

		// Create the view, and stuff it with some log style text.
		LogTextBox lb = new LogTextBox(viewStrings[1]);
		for (int i = 0; i < 15; i++) {
			lb.append(viewStrings[2]);
		}
		lb.setSize(200, 40);
		// lb.setLinksFocusable(true);
		lb.show();

		dw = new LRDialogWindow(viewStrings[0], DialogWindow.APP_DIALOG_STYLE,
				false);
		dw.addBottomFrameButton(Application.getCurrentApp().getString(ID_DONE),
				0, 0, null, Shortcut.BACK_BUTTON);

		dw.addChild(lb);
		dw.setFocusedChild(lb);
		dw.show();
	}

	/**
	 * The display dialog for AnimatedBitMapView, only for 2.3 version.
	 * 
	 * It is notable that animations do not work in the simulator. To see the
	 * animation for this requires a 2.3 device.
	 * 
	 * This creates a dialog using the resource file definitions.
	 */
	public static void showAnimatedBitmapView(ScreenWindow window) {
		Application.getCurrentApp().showDialog(ID_ANIMATED_BITMAP_VIEW, window);
	}

	/**
	 * PhoneNumberTextField was deprecated because it originally was intended to
	 * be internal only. This 2.3 version is publicly available and ready for
	 * use.
	 * 
	 * This creates the sample dialog with the single field for editing only
	 * PhoneNumbers.
	 */
	public static void showPhoneNumberTextField() {
		LRDialogWindow dw;
		String[] viewStrings = Application.getCurrentApp().getStringArray(
				ID_PHONENUMBERTEXTFIELD_STRINGS);

		dw = new LRDialogWindow(viewStrings[0], DialogWindow.APP_DIALOG_STYLE,
				false);
		dw.addBottomFrameButton(Application.getCurrentApp().getString(ID_DONE),
				0, 0, null, Shortcut.BACK_BUTTON);

		// Create the Phone Number field, and set it up.
		PhoneNumberTextField numberField = new PhoneNumberTextField();
		numberField.setTitle(viewStrings[1]);
		numberField.show();
		numberField.setSize(150, 40);

		dw.addChild(numberField);
		dw.setFocusedChild(numberField);
		dw.show();
	}

	/**
	 * For this version 2.3, the ToolTipWindow is fully supported, so demo the
	 * dialog version.
	 */
	public static void showToolTipWindow() {
		LRDialogWindow dw;
		String[] viewStrings = Application.getCurrentApp().getStringArray(
				ID_TOOLTIPWINDOW_STRINGS);

		dw = new LRDialogWindow(viewStrings[0], DialogWindow.APP_DIALOG_STYLE,
				false);
		Button done = dw.addBottomFrameButton(Application.getCurrentApp()
				.getString(ID_DONE), 0, 0, null, Shortcut.BACK_BUTTON);
		dw.setDefaultFrameButton(done);
		dw.show();

		// Create the window, it will display automatically.
		ToolTipWindow.showToolTip(viewStrings[1], 20, 20, 0, dw);
	}

	/**
	 * For this version 2.3, the LinkTextBox is fully supported, so demo the
	 * dialog version of this Control.
	 * 
	 * Note that the click through will not work in the simulator, because the
	 * Browser application is not installed.
	 */
	public static void showLinkTextBox() {
		LRDialogWindow dw;
		String[] viewStrings = Application.getCurrentApp().getStringArray(
				ID_LINKTEXTBOX_STRINGS);

		dw = new LRDialogWindow(viewStrings[0], DialogWindow.APP_DIALOG_STYLE,
				false);
		dw.addBottomFrameButton(Application.getCurrentApp().getString(ID_DONE),
				0, 0, null, Shortcut.BACK_BUTTON);
		
		// Create the LinkTextBox view using the title string.
		LinkTextBox linkBox = new LinkTextBox(viewStrings[1]);
		
		// The string text to be added to the view must be of a specific
		// Spannable type, in order to work properly. Regular Strings
		// will not work. This is the viewable string, which does not
		// include the http header.
		SpannableString url = new SpannableString(viewStrings[2]);

		// Now attach the URL to the string span, which enables the click
		// through to the browser. This is the actual URL, which requires
		// the http:// header.
		SpanUtils.addURL(url, 0, url.length(), viewStrings[3]);

		// A TextBuffer is spannable as well, and allows for longer sets of
		// text, like multiple URLs.  
		TextBuffer text = new TextBuffer();
		text.insert(url);

		// Append the next set of strings.
		url = new SpannableString(viewStrings[4]);
		SpanUtils.addURL(url, 0, url.length(), viewStrings[5]);
		text.insert(url);
		url = new SpannableString(viewStrings[6]);
		SpanUtils.addURL(url, 0, url.length(), viewStrings[7]);
		text.insert(url);
		
		// Set the text with associated spans to the view. 
		linkBox.setText(text);
		linkBox.setSize(150, 40);
		linkBox.show();

		dw.addChild(linkBox);
		dw.setFocusedChild(linkBox);
		dw.show();
	}

	/**
	 * For this version 2.3, the MutableTextbox is fully supported, so demo the
	 * dialog version of this Control.
	 */
	public static void showMutableTextBox() {
		LRDialogWindow dw;
		String[] viewStrings = Application.getCurrentApp().getStringArray(
				ID_MUTABLETEXTBOX_STRINGS);

		dw = new LRDialogWindow(viewStrings[0], DialogWindow.APP_DIALOG_STYLE,
				false);
		dw.addBottomFrameButton(Application.getCurrentApp().getString(ID_DONE),
				0, 0, null, Shortcut.BACK_BUTTON);

		// Create the Phone Number field, and set it up.
		MutableTextBox mutableText = new MutableTextBox(viewStrings[1]);
		mutableText.show();
		mutableText.setSize(150, 40);
		mutableText.setText(viewStrings[2]);

		dw.addChild(mutableText);
		dw.setFocusedChild(mutableText);
		dw.show();
	}

	/**
	 * For this version 2.3, the ScrollingTextBox is fully supported, so demo
	 * the dialog version of this Control.
	 */
	public static void showScrollingTextBox() {
		LRDialogWindow dw;
		String[] viewStrings = Application.getCurrentApp().getStringArray(
				ID_SCROLLINGTEXTBOX_STRINGS);

		dw = new LRDialogWindow(viewStrings[0], DialogWindow.APP_DIALOG_STYLE,
				false);
		dw.addBottomFrameButton(Application.getCurrentApp().getString(ID_DONE),
				0, 0, null, Shortcut.BACK_BUTTON);

		// Create the ScrollingTextBox, and set it up.
		ScrollingTextBox scrollingText = new ScrollingTextBox(viewStrings[1]);
		scrollingText.show();
		scrollingText.setSize(150, 40);

		// Add in much more text for scrolling. The fruit list.
		String[] fruitStrings = Application.getCurrentApp().getStringArray(
				ID_FRUIT_STRINGS);
		StringBuffer bigString = new StringBuffer(viewStrings[2]);
		for (int i = 0; i < fruitStrings.length; i++) {
			bigString.append(fruitStrings[i] + " ");
		}
		scrollingText.setText(bigString);

		dw.addChild(scrollingText);
		dw.setFocusedChild(scrollingText);
		dw.show();
	}

	/**
	 * SelectTextBox class demonstration. 2.3 specific class.
	 */
	public static void showSelectTextBox() {
		LRDialogWindow dw;
		String[] viewStrings = Application.getCurrentApp().getStringArray(
				ID_SELECTTEXTBOX_STRINGS);

		dw = new LRDialogWindow(viewStrings[0], DialogWindow.APP_DIALOG_STYLE,
				false);
		dw.addBottomFrameButton(Application.getCurrentApp().getString(ID_DONE),
				0, 0, null, Shortcut.BACK_BUTTON);

		// Build the select text box, and stuff it with a string from the
		// resource file. The size must be set, because it defaults to a
		// very small 5 character wide box.
		SelectTextBox selectBox = new SelectTextBox(viewStrings[1]);
		selectBox.show();
		selectBox.setSize(150, 40);
		selectBox.setText(viewStrings[2]);

		dw.addChild(selectBox);
		dw.setFocusedChild(selectBox);
		dw.show();
	}

	/**
	 * MultiFolderView for the 2.3 version of the OS. 
	 * 
	 * Under 2.3, there is a drawing bug that erases the last line when the focus
	 * moves out of the MultiFolderView.
	 */
	public static void showMultiFolderView() {
		LRDialogWindow dw;
		String[] viewStrings = Application.getCurrentApp().getStringArray(
				ID_MULTIFOLDERVIEW_STRINGS);

		dw = new LRDialogWindow(viewStrings[0], DialogWindow.APP_DIALOG_STYLE,
				false);
		dw.addBottomFrameButton(Application.getCurrentApp().getString(ID_DONE),
				0, 0, null, Shortcut.BACK_BUTTON);

		// Create the MultiFolderView.
		MultiFolderView folderView = new MultiFolderView();
		folderView.setSize(170, 64);
		folderView.show();

		// Add some dummy folders to the View. The strings are pulled from the
		// resource file to allow for easy localization, even for these
		// programmatically built views.
		Folder f = folderView.createFolder(viewStrings[1]);
		f.addItem(viewStrings[4]);
		f.addItem(viewStrings[5]);
		f.addItem(viewStrings[6]); // multiple items to allow for selecting.
		folderView.createFolder(viewStrings[2]);
		f = folderView.createFolder(viewStrings[3]);
		f.addItem(viewStrings[7]);

		dw.addChild(folderView);
		dw.setFocusedChild(folderView);
		dw.show();
	}
}
