package olstore.session.helper;

import java.util.Collection;
import java.util.Iterator;
import java.util.Vector;
import java.util.ArrayList;

import olstore.dto.PictureValue;

import olstore.entity.ItemLocal;
import olstore.entity.ItemLocalHome;
import olstore.entity.TypeLocalHome;
import olstore.entity.TypeLocal;
import olstore.entity.UserLocal;
import olstore.entity.UserLocalHome;
import olstore.framework.EJBHomeFactory;
import olstore.entity.PictureLocalHome;
import olstore.entity.PictureLocal;

import olstore.session.ShoppingCartLocal;
import olstore.session.ShoppingCartLocalHome;

import javax.ejb.SessionBean;
import javax.ejb.SessionContext;
import javax.ejb.CreateException;
import javax.activation.DataHandler;
import javax.activation.FileDataSource;

import java.net.URL;
import java.awt.image.BufferedImage;
import javax.imageio.ImageIO;
import java.net.URLConnection;

import javax.xml.soap.MimeHeader;
//import javax.xml.rpc.handler.MessageContext;
import javax.xml.rpc.handler.soap.SOAPMessageContext;
import javax.xml.soap.SOAPMessage;
import javax.xml.soap.AttachmentPart;

import org.apache.axis.MessageContext;
import org.apache.axis.attachments.Attachments;
import org.apache.axis.attachments.AttachmentsImpl;
import org.apache.axis.Message;

/**
 * @author mwringe
 * 
 * @ejb.bean name="ShoppingCartHelper" type="Stateless"
 *           view-type="remote-service-endpoint" jndi-name="ShoppingCartRemote"
 * 
 * @ejb.transaction type="Required"
 * 
 *  -- This is need for JOnAS. If you are not using JOnAS you can safetly remove
 * the tags below.
 * 
 * @jonas.bean ejb-name="ShoppingCartHelper" jndi-name="ShoppingCartRemote"
 *  --
 *  
 */
public class ShoppingCartHelperBean implements SessionBean {

    private SessionContext ejbContext;

    public void setSessionContext(SessionContext ctx) {
        ejbContext = ctx;
    }

    public void ejbRemove() {
    }

    public void ejbCreate() throws CreateException {
    }

    public void ejbPassivate() {
    }

    public void ejbActivate() {
    }

    /**
     * 
     * @return All items on special (name, description and price)
     * @throws Exception
     * 
     * @ejb.interface-method view-type="remote-service-endpoint"
     *                       generate="service-endpoint"
     *                       service-endpoint-extends="java.rmi.Remote"
     *  
     */
    public boolean createAndCheckoutOrder(String name, String pwd,
            String[][] cartContents) throws Exception {
        EJBHomeFactory factory = EJBHomeFactory.getInstance();
        ShoppingCartLocalHome home = (ShoppingCartLocalHome) factory
                .getLocalHome(EJBHomeFactory.SHOPPINGCART);

        try {
            UserLocal user = getUser(name, pwd);
            if (user == null)
                return false;
            ShoppingCartLocal cart = home.create(user);

            for (int i = 0; i < cartContents.length; i++) {
                cart.updateQuantity(Integer.valueOf(cartContents[i][0]),
                        Integer.valueOf(cartContents[i][1]));

            }
            cart.createOrders();
        } catch (Exception e) {
            return false;
        }

        return true;

    }

    private UserLocal getUser(String name, String pwd) throws Exception {
        EJBHomeFactory factory = EJBHomeFactory.getInstance();
        UserLocalHome userHome = (UserLocalHome) factory
                .getLocalHome(EJBHomeFactory.USER);
        UserLocal user = userHome.findByUsername(name);
        if (user.getPasswd().equals(pwd)) {
            return user;

        } else
            return null;
    }
}
