package com.swfarm.biz.chain.dto;

import java.io.Serializable;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Iterator;
import java.util.List;

import org.apache.commons.lang.StringUtils;

import com.swfarm.biz.chain.bo.CustomerOrder;
import com.swfarm.biz.chain.bo.CustomerOrderItem;

public class CustomerOrderDTO implements Serializable {

    private String id;

    private String customerOrderId;

    private String customerOrderNo;

    private Timestamp orderTime;

    private Timestamp shippedTime;

    private Timestamp paidTime;

    private Timestamp allocateTime;

    private Timestamp deliverTime;

    private Timestamp modifiedTime;

    private String customerOrderProcessStep;

    private String exceptionalProcessStep;

    private String accountNumber;

    private String customerName;

    private String buyerFullname;

    private String buyerPhoneNumber;

    private String buyerEmail;

    private String buyerAddress1;

    private String buyerAddress2;

    private String buyerCity;

    private String buyerState;

    private String buyerZip;

    private String buyerCountry;

    private String warehouseCode;

    private List customerOrderItemDTOs = new ArrayList();

    private String saleChannel;

    private String saleChannelOrderId;

    private String saleRecordNo;

    private String shippingForwarderMethod;

    private String shippingOrderNo;

    private String sfmOrderNo;

    private Double totalPrice;

    private Double postageFee;

    private String stockLocation;

    private String buyerCheckoutMessage;

    private String shippingServiceSelected;

    private String packingMaterialArticleNumber;

    private String payPalEmailAddress;

    private String payPalTransactionId;

    private Double totalAvgPurchasePrice;

    private Double totalWeight;

    private Double testingShippingCost;

    private String customerOrderProcessStepName;

    private String allocationProductVoucherNos;

    public CustomerOrderDTO() {
    }

    public CustomerOrderDTO(CustomerOrder customerOrder) {
        this.id = customerOrder.getId();
        this.customerOrderId = customerOrder.getId();
        this.customerOrderNo = customerOrder.getCustomerOrderNo();
        this.orderTime = customerOrder.getOrderTime();
        this.shippedTime = customerOrder.getShippedTime();
        this.paidTime = customerOrder.getPaidTime();
        this.allocateTime = customerOrder.getAllocateTime();
        this.deliverTime = customerOrder.getDeliverTime();
        this.modifiedTime = customerOrder.getModifiedTime();
        this.customerOrderProcessStep = customerOrder.getCustomerOrderProcessStep();
        this.exceptionalProcessStep = customerOrder.getExceptionalProcessStep();
        this.accountNumber = customerOrder.getAccountNumber();
        this.customerName = customerOrder.getCustomerName();
        this.buyerFullname = customerOrder.getBuyerFullname();
        this.buyerPhoneNumber = customerOrder.getBuyerPhoneNumber();
        this.buyerEmail = customerOrder.getBuyerEmail();
        this.buyerAddress1 = customerOrder.getBuyerAddress1();
        this.buyerAddress2 = customerOrder.getBuyerAddress2();
        this.buyerCity = customerOrder.getBuyerCity();
        this.buyerState = customerOrder.getBuyerState();
        this.buyerZip = customerOrder.getBuyerZip();
        this.buyerCountry = customerOrder.getBuyerCountry();
        this.warehouseCode = customerOrder.getWarehouseCode();
        for (Iterator iter = customerOrder.getCustomerOrderItems().iterator(); iter.hasNext();) {
            CustomerOrderItem customerOrderItem = (CustomerOrderItem) iter.next();
            CustomerOrderItemDTO customerOrderItemDTO = new CustomerOrderItemDTO(customerOrderItem);
            this.addCustomerOrderItemDTO(customerOrderItemDTO);
        }

        this.saleChannel = customerOrder.getSaleChannel();
        this.saleChannelOrderId = customerOrder.getSaleChannelOrderId();
        this.saleRecordNo = customerOrder.getSaleRecordNo();
        this.shippingForwarderMethod = customerOrder.getShippingForwarderMethod();
        this.shippingOrderNo = customerOrder.getShippingOrderNo();
        this.sfmOrderNo = customerOrder.getSfmOrderNo();
        this.totalPrice = customerOrder.getTotalPrice();
        this.postageFee = customerOrder.getPostageFee();
        this.stockLocation = customerOrder.getStockLocation();
        this.buyerCheckoutMessage = customerOrder.getBuyerCheckoutMessage();
        this.shippingServiceSelected = customerOrder.getShippingServiceSelected();
        this.packingMaterialArticleNumber = customerOrder.getPackingMaterialArticleNumber();
        this.payPalEmailAddress = customerOrder.getPayPalEmailAddress();
        this.payPalTransactionId = customerOrder.getPayPalTransactionId();
        this.totalAvgPurchasePrice = customerOrder.getTotalAvgPurchasePrice();
        this.totalWeight = customerOrder.getTotalWeight();
        this.testingShippingCost = customerOrder.getTestingShippingCost();
        this.customerOrderProcessStepName = customerOrder.getCustomerOrderProcessStepName();
        this.allocationProductVoucherNos = customerOrder.getAllocationProductVoucherNos();
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getCustomerOrderId() {
        return customerOrderId;
    }

    public void setCustomerOrderId(String customerOrderId) {
        this.customerOrderId = customerOrderId;
    }

    public String getCustomerOrderNo() {
        return customerOrderNo;
    }

    public void setCustomerOrderNo(String customerOrderNo) {
        this.customerOrderNo = customerOrderNo;
    }

    public List getCustomerOrderItemDTOs() {
        if (customerOrderItemDTOs == null) {
            customerOrderItemDTOs = new ArrayList();
        }
        Collections.sort(customerOrderItemDTOs, new Comparator() {

            public int compare(Object obj1, Object obj2) {
                CustomerOrderItemDTO coiDTO1 = (CustomerOrderItemDTO) obj1;
                CustomerOrderItemDTO coiDTO2 = (CustomerOrderItemDTO) obj2;
                int result = 0;
                if (StringUtils.isNotEmpty(coiDTO1.getSaleRecordNo())
                        && StringUtils.isNotEmpty(coiDTO2.getSaleRecordNo())) {
                    result = coiDTO1.getSaleRecordNo().compareTo(coiDTO2.getSaleRecordNo());
                }
                else if (StringUtils.isNotEmpty(coiDTO1.getSaleRecordNo())
                        && StringUtils.isEmpty(coiDTO2.getSaleRecordNo())) {
                    result = -1;
                }
                else if (StringUtils.isEmpty(coiDTO1.getSaleRecordNo())
                        && StringUtils.isNotEmpty(coiDTO2.getSaleRecordNo())) {
                    result = 1;
                }
                else {
                    if (StringUtils.isNotEmpty(coiDTO1.getCustomerOrderItemId())
                            && StringUtils.isNotEmpty(coiDTO2.getCustomerOrderItemId())) {
                        result = coiDTO1.getCustomerOrderItemId().compareTo(coiDTO2.getCustomerOrderItemId());
                    }
                    else if (StringUtils.isNotEmpty(coiDTO1.getArticleNumber())
                            && StringUtils.isNotEmpty(coiDTO2.getArticleNumber())) {
                        result = coiDTO1.getArticleNumber().compareTo(coiDTO2.getArticleNumber());
                    }
                    else {
                        result = coiDTO1.toString().compareTo(coiDTO2.toString());
                    }
                }
                return result;
            }

        });
        return customerOrderItemDTOs;
    }

    public void addCustomerOrderItemDTO(CustomerOrderItemDTO coiDTO) {
        if (!this.getCustomerOrderItemDTOs().contains(coiDTO)) {
            this.customerOrderItemDTOs.add(coiDTO);
            coiDTO.setCustomerOrderDTO(this);
        }
    }

    public void setCustomerOrderItemDTOs(List customerOrderItemDTOs) {
        this.customerOrderItemDTOs = customerOrderItemDTOs;
    }

    public Timestamp getOrderTime() {
        return orderTime;
    }

    public void setOrderTime(Timestamp orderTime) {
        this.orderTime = orderTime;
    }

    public String getCustomerOrderProcessStep() {
        return customerOrderProcessStep;
    }

    public void setCustomerOrderProcessStep(String customerOrderProcessStep) {
        this.customerOrderProcessStep = customerOrderProcessStep;
    }

    public String getExceptionalProcessStep() {
        return exceptionalProcessStep;
    }

    public void setExceptionalProcessStep(String exceptionalProcessStep) {
        this.exceptionalProcessStep = exceptionalProcessStep;
    }

    public String getAccountNumber() {
        return accountNumber;
    }

    public void setAccountNumber(String accountNumber) {
        this.accountNumber = accountNumber;
    }

    public String getCustomerName() {
        return customerName;
    }

    public void setCustomerName(String ebayUserId) {
        this.customerName = ebayUserId;
    }

    public String getBuyerFullname() {
        return buyerFullname;
    }

    public void setBuyerFullname(String buyerFullname) {
        this.buyerFullname = buyerFullname;
    }

    public String getBuyerPhoneNumber() {
        return buyerPhoneNumber;
    }

    public void setBuyerPhoneNumber(String buyerPhoneNumber) {
        this.buyerPhoneNumber = buyerPhoneNumber;
    }

    public String getBuyerEmail() {
        return buyerEmail;
    }

    public void setBuyerEmail(String buyerEmail) {
        this.buyerEmail = buyerEmail;
    }

    public String getBuyerAddress1() {
        return buyerAddress1;
    }

    public void setBuyerAddress1(String buyerAddress1) {
        this.buyerAddress1 = buyerAddress1;
    }

    public String getBuyerAddress2() {
        return buyerAddress2;
    }

    public void setBuyerAddress2(String buyerAddress2) {
        this.buyerAddress2 = buyerAddress2;
    }

    public String getBuyerCity() {
        return buyerCity;
    }

    public void setBuyerCity(String buyerCity) {
        this.buyerCity = buyerCity;
    }

    public String getBuyerState() {
        return buyerState;
    }

    public void setBuyerState(String buyerState) {
        this.buyerState = buyerState;
    }

    public String getBuyerZip() {
        return buyerZip;
    }

    public void setBuyerZip(String buyerZip) {
        this.buyerZip = buyerZip;
    }

    public String getBuyerCountry() {
        return buyerCountry;
    }

    public void setBuyerCountry(String buyerCountry) {
        this.buyerCountry = buyerCountry;
    }

    public Timestamp getModifiedTime() {
        return modifiedTime;
    }

    public void setModifiedTime(Timestamp modifiedTime) {
        this.modifiedTime = modifiedTime;
    }

    public String getWarehouseCode() {
        return warehouseCode;
    }

    public void setWarehouseCode(String warehouseCode) {
        this.warehouseCode = warehouseCode;
    }

    public Timestamp getShippedTime() {
        return shippedTime;
    }

    public void setShippedTime(Timestamp shippedTime) {
        this.shippedTime = shippedTime;
    }

    public Timestamp getPaidTime() {
        return paidTime;
    }

    public void setPaidTime(Timestamp paidTime) {
        this.paidTime = paidTime;
    }

    public String getSaleChannel() {
        return saleChannel;
    }

    public void setSaleChannel(String saleChannel) {
        this.saleChannel = saleChannel;
    }

    public String getSaleChannelOrderId() {
        return saleChannelOrderId;
    }

    public void setSaleChannelOrderId(String saleChannelOrderId) {
        this.saleChannelOrderId = saleChannelOrderId;
    }

    public String getSaleRecordNo() {
        return saleRecordNo;
    }

    public void setSaleRecordNo(String saleRecordNo) {
        this.saleRecordNo = saleRecordNo;
    }

    public String getShippingForwarderMethod() {
        return shippingForwarderMethod;
    }

    public void setShippingForwarderMethod(String shippingForwarderMethod) {
        this.shippingForwarderMethod = shippingForwarderMethod;
    }

    public String getShippingOrderNo() {
        return shippingOrderNo;
    }

    public void setShippingOrderNo(String shippingOrderNo) {
        this.shippingOrderNo = shippingOrderNo;
    }

    public String getSfmOrderNo() {
        return sfmOrderNo;
    }

    public void setSfmOrderNo(String sfmOrderNo) {
        this.sfmOrderNo = sfmOrderNo;
    }

    public Double getTotalPrice() {
        return totalPrice;
    }

    public void setTotalPrice(Double totalPrice) {
        this.totalPrice = totalPrice;
    }

    public Double getPostageFee() {
        return postageFee;
    }

    public void setPostageFee(Double postageFee) {
        this.postageFee = postageFee;
    }

    public String getStockLocation() {
        return stockLocation;
    }

    public void setStockLocation(String stockLocation) {
        this.stockLocation = stockLocation;
    }

    public String getBuyerCheckoutMessage() {
        return buyerCheckoutMessage;
    }

    public void setBuyerCheckoutMessage(String buyerCheckoutMessage) {
        this.buyerCheckoutMessage = buyerCheckoutMessage;
    }

    public String getShippingServiceSelected() {
        return shippingServiceSelected;
    }

    public void setShippingServiceSelected(String shippingServiceSelected) {
        this.shippingServiceSelected = shippingServiceSelected;
    }

    public String getPackingMaterialArticleNumber() {
        return packingMaterialArticleNumber;
    }

    public void setPackingMaterialArticleNumber(String packingMaterialArticleNumber) {
        this.packingMaterialArticleNumber = packingMaterialArticleNumber;
    }

    public String getPayPalEmailAddress() {
        return payPalEmailAddress;
    }

    public void setPayPalEmailAddress(String payPalEmailAddress) {
        this.payPalEmailAddress = payPalEmailAddress;
    }

    public String getPayPalTransactionId() {
        return payPalTransactionId;
    }

    public void setPayPalTransactionId(String payPalTransactionId) {
        this.payPalTransactionId = payPalTransactionId;
    }

    public Double getTotalAvgPurchasePrice() {
        return totalAvgPurchasePrice;
    }

    public void setTotalAvgPurchasePrice(Double totalAvgPurchasePrice) {
        this.totalAvgPurchasePrice = totalAvgPurchasePrice;
    }

    public Double getTotalWeight() {
        return totalWeight;
    }

    public void setTotalWeight(Double totalWeight) {
        this.totalWeight = totalWeight;
    }

    public Double getTestingShippingCost() {
        return testingShippingCost;
    }

    public void setTestingShippingCost(Double testingShippingCost) {
        this.testingShippingCost = testingShippingCost;
    }

    public String getCustomerOrderProcessStepName() {
        return customerOrderProcessStepName;
    }

    public void setCustomerOrderProcessStepName(String customerOrderProcessStepName) {
        this.customerOrderProcessStepName = customerOrderProcessStepName;
    }

    public String getAllocationProductVoucherNos() {
        return allocationProductVoucherNos;
    }

    public void setAllocationProductVoucherNos(String allocationProductVoucherNos) {
        this.allocationProductVoucherNos = allocationProductVoucherNos;
    }
}