/**
 * 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.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Image;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.DecimalFormat;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

/**
 * A custom JPanel class used for displaying items
 * that are in the shopping cart.
 */
public class OrderPanel extends JPanel {

    /** The total price for the item and the quantity in the cart. */
    private JLabel totalPrice;

    /** The quantity of the item in the cart. */
    private JTextField quantity;

    /** The item represented by this panel. */
    private OrderItem orderItem;

    /** The formatter used for displaying prices. */
    private DecimalFormat formatter = new DecimalFormat("#.00");

    /**
     * Creates a new OrderPanel given an OrderItem,
     * and builds the panel to display the item information.
     * @param orderItem The order item to add to the panel.
     */
    public OrderPanel(OrderItem orderItem) {
        this.orderItem = orderItem;

        buildPanel();
    }

    /**
     * Creates the panel to display the item by creating
     * components for each piece of information (picture, price, etc...)
     * and putting them in a GridBagLayout.
     */
    public void buildPanel() {

        GridBagLayout gridbag = new GridBagLayout();
        GridBagConstraints c = new GridBagConstraints();
        setMaximumSize(new Dimension(Integer.MAX_VALUE, 40));

        setLayout(gridbag);
        JButton button;
        setLayout(new GridBagLayout());
        c.fill = GridBagConstraints.HORIZONTAL;

        ImageIcon icon = new ImageIcon(orderItem.getImage().getScaledInstance(
                50, -1, Image.SCALE_SMOOTH));
        JLabel image = new JLabel(icon);
        c.ipadx = 5;
        c.gridx = 0;
        c.gridy = 0;
        c.gridheight = 2;
        add(image, c);

        JLabel name = new JLabel(orderItem.getTitle());
        c.weightx = 1.0;
        c.gridheight = 1;
        c.gridwidth = 4;
        c.gridx = 1;
        c.gridy = 0;
        add(name, c);

        quantity = new JTextField();
        quantity.setText(String.valueOf(orderItem.getQuantity()));
        quantity.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent arg0) {
                try {
                    int newQuantity = Integer.valueOf(quantity.getText())
                            .intValue();

                    double oldTotal = orderItem.getTotal();
                    orderItem.setQuantity(newQuantity);
                    double newTotal = orderItem.getTotal();

                    // update the labels that display the price
                    ((ShoppingCartPanel) getParent()).updatePrice(newTotal
                            - oldTotal);
                    setTotalPrice(orderItem.getTotal());
                } catch (Exception e) {
                }
            }
        });
        c.weightx = 0;
        c.gridwidth = 1;
        c.ipadx = 40;
        c.gridx = 3;
        c.gridy = 1;
        add(quantity, c);

        JLabel x = new JLabel("x");
        c.gridx = 2;
        c.gridy = 1;
        c.ipadx = 0;
        add(x, c);

        totalPrice = new JLabel();
        c.gridx = 4;
        c.gridy = 1;
        c.ipadx = 10;
        add(totalPrice, c);
        // set the initial price
        setTotalPrice(orderItem.getTotal());

        JLabel price = new JLabel(orderItem.getPrice());
        c.weightx = 1.0;
        c.ipadx = 0;
        c.gridx = 1;
        c.gridy = 1;
        add(price, c);

        JButton remove = new JButton("X");
        remove.setBackground(OlstoreSwingClient.buttonColor);
        remove.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {
                ((ShoppingCartPanel) getParent()).removeFromCart(orderItem);
            }
        });
        c.weightx = 0;
        c.ipadx = 0;
        c.gridx = 5;
        c.gridheight = 2;
        c.gridy = 0;
        add(remove, c);

        setBackground(OlstoreSwingClient.itemColor1);
    }

    /**
     * Sets the total price for this panel.
     * @param price The price to display.
     */
    private void setTotalPrice(double price) {
        DecimalFormat formatter = new DecimalFormat("#.00");
        totalPrice.setText(formatter.format(price));
    }

    /**
     * Returns the OrderItem represented by this panel.
     * @return The orderItem represented by this panel.
     */
    public OrderItem getOrderItem() {
        return orderItem;
    }

    /**
     * Sets the order item represented by this panel
     * to the given order item.
     * @param orderItem
     *            The orderItem to set.
     */
    public void setOrderItem(OrderItem orderItem) {
        this.orderItem = orderItem;
    }

    /**
     * Updates the quantity for this item.
     */
    public void updateQuantity() {
        quantity.setText(String.valueOf(orderItem.getQuantity()));
        setTotalPrice(orderItem.getTotal());
    }

}
