/**
 * @author Twenty Codes, LLC
 * @author ricky barrette
 * @date Nov 21, 2010
 */

package com.TwentyCodes.java.OrderProcessor;

import java.io.Serializable;


/**
 * This class will represent a single order
 * @author ricky barrette
 */
public class Order implements Comparable<Order>, Serializable{
	
	private static final long serialVersionUID = 810485062967800884L;
	private String mItemName;
	private long mGoogleOrderNumber;
	private Date mOrderCreationDate;
	private float mOrderAmount;
	private float mAmountCharged;
	private Status mFinancialStatus;
	private Status mFulfillmentStatus;
	private String mCustomerEmail;
	private String mCustomerName;
	
	/**
	 * Creates a new Order from a comma separated line from a *.csv file.
	 * 
	 * Order ID,Merchant Order Number,Order Creation Date,Currency of Transaction,Order Amount,Amount Charged,Financial Status,Fulfillment Status,Link to Order,Total Tax,Total Shipping,Amount Refunded,Amount Charged Back,Chargeback Protection,Shipping Method,Email Marketing,Buyer Email Address,Buyer Name,Buyer Address 1,Buyer Address 2,Buyer City,Buyer State,Buyer Postal Code,Buyer Country,Buyer Phone Number,Tracking Data,Item 1 ID,Item 1 Name,Item 1 Description,Item 1 Price,Item 1 Quantity
	 * 
	 * @param order
	 * @author ricky barrette
	 * @throws InvalidDateFormatException 
	 */
	public Order(String order) throws NumberFormatException, InvalidDateFormatException{
		if(Main.DEBUG)
			System.out.println("parsing in order: "+ order);
		
		String [] lineParts;
		lineParts = order.split(",");
		
		this.mGoogleOrderNumber = Long.parseLong(removeLastChar(lineParts[0].split(" ")[1]));
		this.mOrderCreationDate = new Date(lineParts[2].substring(1, (lineParts[2].length() -1) ));
		this.mOrderAmount  = Float.parseFloat(lineParts[4]);
		this.mAmountCharged = Float.parseFloat(lineParts[5]);
		this.mFinancialStatus = Status.parseStatus(lineParts[6]);
		this.mFulfillmentStatus = Status.parseStatus(lineParts[7]);
		this.mCustomerEmail = removeFistAndLastChar(lineParts[16]);
		this.mCustomerName = removeFistAndLastChar(lineParts[17]);
		this.mItemName = removeFistAndLastChar(lineParts[27]);
		
		if (Main.DEBUG)
			System.out.println(this.toString());
	}
	
	/**
	 * removes the last char from a string
	 * @param s
	 * @return
	 * @author ricky barrette
	 */
	private String removeLastChar(String s) {
		   return s.substring(0, s.length() - 1);
	}
	
	/**
	 * removes the first and last chars from a string
	 * @param s
	 * @return
	 * @author ricky barrette
	 */
	private String removeFistAndLastChar(String s){
		return s.substring(1, s.length()-1);
	}
	
	/**
	 * returns a human readable string that represnets this object
	 * (non-Javadoc)
	 * @see java.lang.Object#toString()
	 * @author ricky barrette
	 */
	@Override
	public String toString(){
		return this.mGoogleOrderNumber +", "+ this.mOrderCreationDate.toString() +", "+ this.mItemName +", "+ this.mOrderAmount +", "+ this.mAmountCharged +", "+ this.mFinancialStatus +", "+ this.mFulfillmentStatus +", "+ this.mCustomerName +", "+ this.mCustomerEmail;
	}
	
	/**
	 * Compares orders by orders creation date
	 * (non-Javadoc)
	 * @see java.lang.Comparable#compareTo(java.lang.Object)
	 * @author ricky barrette
	 */
	@Override
	public int compareTo(Order o) {
		return this.mOrderCreationDate.compareTo(o.getOrderCreationDate());
	}


	/**
	 * @param mItemName the mItemName to set
	 * @author ricky barrette
	 */
	public void setItemName(String mItemName) {
		this.mItemName = mItemName;
	}


	/**
	 * @return the mItemName
	 * @author ricky barrette
	 */
	public String getItemName() {
		return mItemName;
	}


	/**
	 * @param googleOrderNumber the mGoogleOrderNumber to set
	 * @author ricky barrette
	 */
	public void setGoogleOrderNumber(long googleOrderNumber) {
		this.mGoogleOrderNumber = googleOrderNumber;
	}


	/**
	 * @return the mGoogleOrderNumber
	 * @author ricky barrette
	 */
	public long getGoogleOrderNumber() {
		return mGoogleOrderNumber;
	}


	/**
	 * @param orderCreationDate the mOrderCreationDate to set
	 * @author ricky barrette
	 */
	public void setOrderCreationDate(Date orderCreationDate) {
		this.mOrderCreationDate = orderCreationDate;
	}


	/**
	 * @return the mOrderCreationDate
	 * @author ricky barrette
	 */
	public Date getOrderCreationDate() {
		return mOrderCreationDate;
	}


	/**
	 * @param orderAmount the mOrderAmount to set
	 * @author ricky barrette
	 */
	public void setOrderAmount(float orderAmount) {
		this.mOrderAmount = orderAmount;
	}


	/**
	 * @return the mOrderAmount
	 * @author ricky barrette
	 */
	public float getOrderAmount() {
		return mOrderAmount;
	}


	/**
	 * @param amountCharged the mAmountCharged to set
	 * @author ricky barrette
	 */
	public void setAmountCharged(float amountCharged) {
		this.mAmountCharged = amountCharged;
	}


	/**
	 * @return the mAmountCharged
	 * @author ricky barrette
	 */
	public float getAmountCharged() {
		return mAmountCharged;
	}


	/**
	 * @param financialStatus the mFinancialStatus to set
	 * @author ricky barrette
	 */
	public void setFinancialStatus(Status financialStatus) {
		this.mFinancialStatus = financialStatus;
	}


	/**
	 * @return the mFinancialStatus
	 * @author ricky barrette
	 */
	public Status getFinancialStatus() {
		return mFinancialStatus;
	}


	/**
	 * @param fulfillmentStatus the mFulfillmentStatus to set
	 * @author ricky barrette
	 */
	public void setFulfillmentStatus(Status fulfillmentStatus) {
		this.mFulfillmentStatus = fulfillmentStatus;
	}


	/**
	 * @return the mFulfillmentStatus
	 * @author ricky barrette
	 */
	public Status getFulfillmentStatus() {
		return mFulfillmentStatus;
	}


	/**
	 * @param customerEmail the mCustomerEmail to set
	 * @author ricky barrette
	 */
	public void setCustomerEmail(String customerEmail) {
		this.mCustomerEmail = customerEmail;
	}


	/**
	 * @return the mCustomerEmail
	 * @author ricky barrette
	 */
	public String getCustomerEmail() {
		return mCustomerEmail;
	}


	/**
	 * @param customerName the mCustomerName to set
	 * @author ricky barrette
	 */
	public void setCustomerName(String customerName) {
		this.mCustomerName = customerName;
	}


	/**
	 * @return the mCustomerName
	 * @author ricky barrette
	 */
	public String getCustomerName() {
		return mCustomerName;
	}

}