/**
 * Copyright (c) 2006 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: Greg Lapouchnian
 *                     Patrick Smith
 *
 */

package olstore.controller;

import java.util.Iterator;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import olstore.domain.logic.OlstoreFacade;
import olstore.domain.manager.CartManager;
import olstore.dto.ShoppingCartItem;

import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.multiaction.MultiActionController;

/**
 * A controller that handles shopping cart related actions.
 */
public class ShoppingCartController extends MultiActionController {

    // The olstore instance provided via Spring.
	private OlstoreFacade olstore;

	/**
	 * The Spring injection setter method for the olstore instance.
	 * @param olstore the instance provided via spring.
	 */
	public void setOlstore(OlstoreFacade olstore) {
		this.olstore = olstore;
	}

	/**
	 * Returns a new model and view for this controller. 
	 * @param request the HttpServletRequest
	 * @param response the HttpServletResponse
	 * @return a new model and view that represents the data for this controller.
	 * @throws Exception
	 */
    public ModelAndView view(HttpServletRequest request, HttpServletResponse response) 
    throws Exception {

        Map model = ControllerHelper.initModel(request, olstore);
        CartManager cartManager = (CartManager) request.getSession().getAttribute("cart");

        model.put("items", cartManager.getItems());
        
        request.setAttribute("page_title", "Shopping Cart Contents  - Olstore");
        return new ModelAndView("cart", model);
    }

    /**
     * Called when we are in an update cart action.
     * @param request the HttpServletRequest.
     * @param response the HttpServletResponse.
     * @return a model and view for representing the updating of a cart.
     * @throws Exception
     */
    public ModelAndView update(HttpServletRequest request, HttpServletResponse response) 
    throws Exception {
        Map model = ControllerHelper.initModel(request, olstore);
        CartManager cartManager = (CartManager) request.getSession().getAttribute("cart");
        model.put("items", cartManager.getItems());
        request.setAttribute("page_title", "Shopping Cart Contents - Olstore");
        
        Iterator i = cartManager.getItems().iterator();
        while(i.hasNext()) {
            int itemId = ((ShoppingCartItem)i.next()).getItemId().intValue();
            int quantity;
            try {
                quantity = Integer.valueOf(request.getParameter(String.valueOf(itemId))).intValue();
                cartManager.updateQuantity(new Integer(itemId), new Integer(quantity));
            } catch(NumberFormatException e) {
                model.put("error", "Quantity invalid. Smarten-up.");
            }
       }
        return new ModelAndView("cart", model);
    }

    /**
     * Called when we are performing an order action.
     * @param request the HttpServletRequest.
     * @param response the HttpServletResponse.
     * @return a model and view to represent the updating order.
     * @throws Exception
     */
    public ModelAndView order(HttpServletRequest request, HttpServletResponse response) 
    throws Exception {
        Map model = ControllerHelper.initModel(request, olstore);
        CartManager cartManager = (CartManager) request.getSession().getAttribute("cart");
        
        cartManager.placeOrder();
        
        model.put("page_title", "Shopping Cart Contents - Olstore");
        model.put("message", "Your order has been placed. Thank you for shopping with us.");
        
        return new ModelAndView("cart", model);
    }
    

	 
}
