/**
 * OrderPanel.java
 * @date Jan 9, 2011
 * @author ricky barrette
 * @author Twenty Codes, LLC
 */
package com.TwentyCodes.java.OrderProcessor.UI;

import java.awt.Color;
import java.util.ArrayList;

import javax.swing.JScrollPane;
import javax.swing.JTextPane;
import javax.swing.text.BadLocationException;
import javax.swing.text.Style;
import javax.swing.text.StyleConstants;
import javax.swing.text.StyleContext;
import javax.swing.text.StyledDocument;

import com.TwentyCodes.java.OrderProcessor.Order;
import com.TwentyCodes.java.OrderProcessor.ProgressListener;
import com.TwentyCodes.java.OrderProcessor.Status;
import com.TwentyCodes.java.OrderProcessor.TextStyle;

/**
 * a pane to display orders in color code.
 * @author ricky barrette
 */
public class OrderPane extends JScrollPane {

	private static final long serialVersionUID = -8762146025822173375L;
	private JTextPane mOutput;
	private ProgressListener mListener;

	/**
	 * Creates a new OrderPanel
	 * @author ricky barrette
	 */
	public OrderPane() {
		mOutput = new JTextPane();
		mOutput.setEditable(false);
		setViewportView(mOutput);
	}

	/**
	 * displays orders
	 * 
	 * @param list
	 *            of orders to display
	 * @author ricky barrette
	 */
	public void displayOrders(ArrayList<Order> list) {
		mOutput.setText(null);
		float possible = 0;
		float actual = 0;
		int total = 0;
		for (int i = 0; i < list.size(); i++) {
			if(mListener != null)
				mListener.onProgressUpdate(i, list.size());
			switch (list.get(i).getFinancialStatus()) {
				case CANCELLED:
					updateTextPane(list.get(i).toString(), TextStyle.RED);
					break;
				case CANCELLED_BY_GOOGLE:
					updateTextPane(list.get(i).toString(), TextStyle.RED);
					break;
				default:
					updateTextPane(list.get(i).toString(), TextStyle.REGULAR);
			}

			possible = possible + list.get(i).getOrderAmount();
			actual = actual + list.get(i).getAmountCharged();
			if (list.get(i).getFulfillmentStatus() == Status.DELIVERED)
				total++;
		}

		updateTextPane("Possible sales: $" + possible, TextStyle.BOLD);
		updateTextPane("Actual sales: $" + actual, TextStyle.BOLD);
		updateTextPane("Possible sold:" + list.size(), TextStyle.BOLD);
		updateTextPane("Total sold: " + total, TextStyle.BOLD);
	}
	
	public String getText(){
		return mOutput.getText();
	}

	/**
	 * adds the following string to the textpane
	 * 
	 * @param string
	 * @author ricky barrette
	 */
	private void updateTextPane(String string, TextStyle style) {
		String[] initString = { "\n" + string };
		String[] initStyles = { style.toString() };

		StyledDocument doc = mOutput.getStyledDocument();
		addStylesToDocument(doc);

		// Load the text pane with styled text.
		try {
			for (int i = 0; i < initString.length; i++) {
				doc.insertString(doc.getLength(), initString[i],
						doc.getStyle(initStyles[i]));
			}
		} catch (BadLocationException ble) {
			System.err.println("Couldn't insert initial text into text pane.");
		}
		mOutput.setDocument(doc);

	}

	/**
	 * adds supported styles to the document
	 * 
	 * @param doc
	 * @author ricky barrette
	 */
	protected void addStylesToDocument(StyledDocument doc) {
		// Initialize some styles.
		Style def = StyleContext.getDefaultStyleContext().getStyle(StyleContext.DEFAULT_STYLE);

		Style regular = doc.addStyle("REGULAR", def);
		StyleConstants.setFontFamily(def, "Arial");
		StyleConstants.setFontSize(def, 14);

		Style s = doc.addStyle("ITALIC", regular);
		StyleConstants.setItalic(s, true);

		s = doc.addStyle("BOLD", regular);
		StyleConstants.setBold(s, true);

		s = doc.addStyle("RED", regular);
		StyleConstants.setForeground(s, Color.RED);

		s = doc.addStyle("GREEN", regular);
		StyleConstants.setForeground(s, Color.GREEN);
	}

	/**
	 * sets a listener for db progress updates
	 * 
	 * @param listener
	 * @author ricky barrette
	 */
	public void setOnProgressListerner(ProgressListener listener) {
		mListener = listener;
	}
}