/**
 * Copyright (c) 2005 Red Hat, Inc. All rights reserved.
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307
 * USA
 *
 * Component of: Red Hat Application Server
 *
 * Initial Developers: Gregory Lapouchnian
 *                     Patrick Smith
 * --------------------------------------------------------------------------
 * $Id$
 * --------------------------------------------------------------------------
 */
package olstore.client;

import java.awt.Component;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

import javax.swing.JPanel;

/**
 * A panel to display the contents of the shopping cart.
 */
public class ShoppingCartPanel extends JPanel {

    /** The list of items in the shopping cart. */
    private ArrayList items;

    /** A reference to the main client. */
    private OlstoreSwingClient client;

    /**
     * A panel to store and display all the items in the shopping cart.
     *
     * @param client the client application to which this cart belongs
     */
    public ShoppingCartPanel(OlstoreSwingClient client) {
        items = new ArrayList();
        this.client = client;
    }

    /**
     * Add a new item to the shopping cart.
     * @param item the item to be added
     */
    public void addNewItemToCart(OrderItem item) {
        items.add(item);
        add(new OrderPanel(item));
        updatePrice(Double.valueOf(item.getPrice()).doubleValue());
    }

    /**
     * Increase the quantity of the given item in the shopping cart by one.
     * @param item the item whose quantity is to be increased
     */
    public void increaseQuantity(OrderItem item) {
        // remove the corresponding OrderPanel from this component
        Component[] orderPanels = getComponents();

        for (int i = 0; i < orderPanels.length; i++) {
            OrderItem o = ((OrderPanel) orderPanels[i]).getOrderItem();

            if (o.equals(item)) {
                o.setQuantity(o.getQuantity() + 1);

                // repaint this panel
                ((OrderPanel) orderPanels[i]).updateQuantity();

                updatePrice(Double.valueOf(o.getPrice()).doubleValue());

                return;
            }
        }
    }

    /**
     * Add change to the total price displayed in the client.
     * @param change the amount to add to the total (negative number to decrease
     * the total)
     */
    public void updatePrice(double change) {
        client.updatePrice(new BigDecimal(change));
    }

    /**
     * Check whether there are any items in the shopping cart.
     * @return true is there are no items in the shopping cart, false otherwise
     */
    public boolean isEmpty() {
        return items.isEmpty();
    }

    /**
     * Remove all items from the shopping cart.
     */
    public void removeAllItems() {
        items = new ArrayList();
        removeAll();

        client.setPrice(new BigDecimal(0));

        validate();
        repaint();
    }

    /**
     * Remove the given item from the shopping cart.
     * @param item the item to be removed from the shopping cart.
     */
    public void removeFromCart(OrderItem item) {
        // remove the corresponding OrderPanel from this component
        Component[] orderPanels = getComponents();

        for (int i = 0; i < orderPanels.length; i++) {
            OrderItem o = ((OrderPanel) orderPanels[i]).getOrderItem();

            if (o.equals(item)) {
                remove(orderPanels[i]);
                updatePrice(-o.getTotal());
                break;
            }
        }

        // remove the item from the list
        items.remove(item);

        // repaint this panel
        validate();
        repaint();
    }

    /**
     * Get the contents fo the shopping cart.
     * @return the array of all the OrderItems as an array of Objects
     */
    public Object[] getCartContents() {
        return items.toArray();
    }

    /**
     * Get the items in the cart.
     * @return a list of all the items in the cart
     */
    public List getItemsInCart() {
        return items;
    }

    /**
     * Check whether the given order item is already in the cart.
     * @param item the item that we need to check for
     * @return true if the item is already in the shopping cart, false otherwise
     */
    public boolean isItemInCart(OrderItem item) {
        return items.contains(item);
    }
}
