/**
 * 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.math.BigDecimal;
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.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) {
		ArrayList<TextStyle> styles = new ArrayList<TextStyle>();
		mOutput.setText(null);
		BigDecimal possible = new BigDecimal(0);
		BigDecimal actual = new BigDecimal(0);
		BigDecimal possibleNew = new BigDecimal(0);
		int total = 0;
		int newOrders = 0;
		
		for (int i = 0; i < list.size(); i++) {
			if(mListener != null)
				mListener.onProgressUpdate("Processing Order: "+ (i+1) +" of "+ list.size(), false, (i+1), list.size());
			
			try {
				switch (list.get(i).getFulfillmentStatus()) {
					case WILL_NOT_DELIVER:
						styles.add(TextStyle.RED);
						break;
					case NEW:
						styles.add(TextStyle.BLUE);
						newOrders++;
						possibleNew = possibleNew.add(new BigDecimal(list.get(i).getOrderAmount()));
						break;
					default:
						styles.add(TextStyle.REGULAR);
						total++;
						actual = actual.add(new BigDecimal((list.get(i).getAmountCharged())));
				}
			} catch (Exception e) {
				//This is just in case I missed a order status again
				styles.add(TextStyle.ITALIC);
				e.printStackTrace();
			}
			possible = possible.add(new BigDecimal((list.get(i).getOrderAmount())));	
		}

		updateTextPane(list, styles);
		updateTextPane("\nPossible sold: " + list.size() 
				+"\nPossible sales: $" + possible.setScale(2, BigDecimal.ROUND_DOWN)
				+ ( (newOrders > 0) ? "\n\nTotal new sales: "+ newOrders + "\nPossible new sales: $"+ possibleNew.setScale(2, BigDecimal.ROUND_DOWN) : "") 
				+ "\n\nTotal sold: " + total
				+ "\nActual sales: $" + actual.setScale(2, BigDecimal.ROUND_DOWN) , TextStyle.BOLD);
	}
	
	/**
	 * addeds the orders to the text pane 
	 * @param list of orders
	 * @param styles for the orders
	 * @author ricky barrette
	 */
	private void updateTextPane(ArrayList<Order> list, ArrayList<TextStyle> styles) {
		StyledDocument doc = mOutput.getStyledDocument();
		addStylesToDocument(doc);
		// Load the text pane with styled text.
		try {
			for (int i = 0; i < list.size(); i++) {
				doc.insertString(doc.getLength(), "\n"+list.get(i).toString(),
						doc.getStyle(styles.get(i).toString()));
			}
		} catch (BadLocationException ble) {
			System.err.println("Couldn't insert initial text into text pane.");
		}
		mOutput.setDocument(doc);
	}

	/**
	 * @return the text contained by this order pane
	 * @author ricky barrette
	 */
	public String getText(){
		return mOutput.getText();
	}

	/**
	 * adds the following string to the order pane
	 * @param string
	 * @author ricky barrette
	 */
	private void updateTextPane(String string, TextStyle style) {
		StyledDocument doc = mOutput.getStyledDocument();
		addStylesToDocument(doc);

		// Load the text pane with styled text.
		try {
				doc.insertString(doc.getLength(), string, doc.getStyle(style.toString()));
		} 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);
		StyleConstants.setBackground(s, Color.YELLOW);

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

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

		s = doc.addStyle("BLUE", regular);
		StyleConstants.setForeground(s, Color.BLUE);
		StyleConstants.setBold(s, true);
	}

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

	/**
	 * sets text of the order pane
	 * @param message
	 * @author ricky barrette
	 */
	public void setText(String message) {
		mOutput.setText(message);
	}
}