package portlet;

import java.io.IOException;

import java.util.StringTokenizer;

import javax.portlet.ActionRequest;
import javax.portlet.ActionResponse;
import javax.portlet.GenericPortlet;
import javax.portlet.PortletContext;
import javax.portlet.PortletException;
import javax.portlet.PortletMode;
import javax.portlet.PortletPreferences;
import javax.portlet.PortletRequestDispatcher;
import javax.portlet.RenderRequest;
import javax.portlet.RenderResponse;
import javax.portlet.WindowState;

import portlet.resource.ProductsBundle;

public class Products extends GenericPortlet {
    protected String getTitle(RenderRequest request) {
        // Get the customized title. This defaults to the declared title.
        PortletPreferences prefs = request.getPreferences();
        return prefs.getValue(PORTLETTITLE_KEY, super.getTitle(request));
    }

    protected void doDispatch(RenderRequest request,
                              RenderResponse response) throws PortletException,
                                                              IOException {
        // Do nothing if window state is minimized.
        WindowState state = request.getWindowState();
        if (state.equals(WindowState.MINIMIZED)) {
            super.doDispatch(request, response);
            return;
        }

        // Get the content type for the response.
        String contentType = request.getResponseContentType();

        // Get the requested portlet mode.
        PortletMode mode = request.getPortletMode();

        // Reference a request dispatcher for dispatching to web resources
        PortletContext context = getPortletContext();
        PortletRequestDispatcher rd = null;

        // Dispatch based on content type and portlet mode.
        if (contentType.equals("text/html")) {
            if (mode.equals(PortletMode.VIEW)) {
                rd = context.getRequestDispatcher("/Products/html/view.jsp");
            } else if (mode.equals(EDIT_DEFAULTS)) {
                rd = context.getRequestDispatcher("/Products/html/edit_defaults.jsp");
            } else if (mode.equals(PortletMode.EDIT)) {
                rd = context.getRequestDispatcher("/Products/html/edit.jsp");
            } else {
                super.doDispatch(request,response);
            }
        } else {
            super.doDispatch(request,response);
        }
        if (rd != null) {
            rd.include(request, response);
        } else {
            super.doDispatch(request, response);
        }
    }
    public static final String APPLY_ACTION = "apply_action";
    public static final String OK_ACTION = "ok_action";
    public static final String PORTLETTITLE_KEY = "portletTitle";
    private static final PortletMode EDIT_DEFAULTS = new PortletMode("edit_defaults");

    private String[] buildValueArray(String values) {
        if (values.indexOf(',') < 0) {
            return new String[] {values};
        }
        StringTokenizer st = new StringTokenizer(values, ",");
        String[] valueArray = new String[st.countTokens()];
        int i = 0;
        while (st.hasMoreTokens()) {
            valueArray[i] = st.nextToken().trim();
            i++;
        }
        return valueArray;
    }

    // Form field names
    public static final String PARAMETER1 = "productId";
    public static final String FORM_PARAMETER1 = "form_Parameter1";
    public static final String FORM_SUBMIT = "dosub";
    // Portlet Modes
    public static final String MODE_NAME_PARAM = "mode";
    public static final String MODE_VIEW = "view";


    public void processAction(ActionRequest request,
                              ActionResponse response) throws PortletException,
                                                              IOException {

        // Determine what kind of action we have by examining the mode parameter
        boolean viewMode =
            MODE_VIEW.equals(request.getParameter(MODE_NAME_PARAM));

        // Extract the form field parameter and pass it through as a portlet parameter
        String param1 = request.getParameter(FORM_PARAMETER1);
        if (param1 == null) {
            param1 = ProductsBean.DEFAULT_PRODUCT_ID;
        }

        if (viewMode) {
            // Set the new parameter values. These will be intepreted by the
            // container as navigational parameters as the names match the names of
            // the declared parameters.
            response.setRenderParameter(PARAMETER1, param1);
        } else {
            // Determine which action.
            String okAction = request.getParameter(OK_ACTION);
            String applyAction = request.getParameter(APPLY_ACTION);

            if (okAction != null || applyAction != null) {
                // Save the preferences.
                PortletPreferences prefs = request.getPreferences();
                String param = request.getParameter(PORTLETTITLE_KEY);
                prefs.setValues(PORTLETTITLE_KEY, buildValueArray(param));
                prefs.store();
                if (okAction != null) {
                    response.setPortletMode(PortletMode.VIEW);
                    response.setWindowState(WindowState.NORMAL);
                }
            }
        }
    }
}
