package de.thomasjacob.improve.presentation.issue.search;

import de.thomasjacob.improve.logic.DaoLocator;
import de.thomasjacob.improve.logic.issue.Issue;
import de.thomasjacob.improve.logic.project.Project;
import de.thomasjacob.improve.logic.user.User;
import de.thomasjacob.improve.presentation.ImproveAction;
import de.thomasjacob.improve.presentation.NavigationFiller;
import de.thomasjacob.improve.presentation.login.LoginManager;
import de.thomasjacob.webcore.TechnicalException;
import de.thomasjacob.webcore.presentation.HtmlOption;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

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

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessage;
import org.apache.struts.action.ActionMessages;


public class IssuesVPAction extends ImproveAction
{
	private static final int NAME_MAX_SHOW_LENGTH = 40;

	/*
	 * (non-Javadoc)
	 *
	 * @see de.thomasjacob.webcore.presentation.BaseAction#executeChecked(org.apache.struts.action.ActionMapping,
	 * org.apache.struts.action.ActionForm, javax.servlet.http.HttpServletRequest,
	 * javax.servlet.http.HttpServletResponse)
	 */
	protected ActionForward executeChecked(ActionMapping mapping, ActionForm form, HttpServletRequest request,
		HttpServletResponse response) throws TechnicalException
	{
		HttpSession session = request.getSession();

		if (!LoginManager.getInstance(session).isLoggedIn())
		{
			return mapping.findForward("login");
		}

		NavigationFiller.getInstance().fill(mapping, request, session);
		IssueSearchForm issueSearchForm = IssueSearchForm.getInstance(request);

		List projectIdOptions = new ArrayList();
		projectIdOptions.add(new HtmlOption("", "-any-         "));
		List projects = DaoLocator.getInstance().getProjectDao().findProjects();
		Iterator iterator = projects.iterator();
		while (iterator.hasNext())
		{
			Project project = (Project) iterator.next();
			projectIdOptions.add(new HtmlOption(String.valueOf(project.getId()), project.getName()));
		}
		Collections.sort(projectIdOptions);
		request.setAttribute("filterProjectIdOptions", projectIdOptions);

		List reporterOptions = new ArrayList();
		reporterOptions.add(new HtmlOption("", "-anyone-      "));
		List users = DaoLocator.getInstance().getUserDao().findUsers();
		iterator = users.iterator();
		while (iterator.hasNext())
		{
			User user = (User) iterator.next();
			reporterOptions.add(new HtmlOption(String.valueOf(user.getId()), user.getName()));
		}
		Collections.sort(reporterOptions);
		request.setAttribute("filterReporterOptions", reporterOptions);

		List developerOptions = new ArrayList();
		developerOptions.add(new HtmlOption("", "-anyone-    "));
		developerOptions.add(new HtmlOption("0", "-unassigned-"));
		iterator = users.iterator();
		while (iterator.hasNext())
		{
			User user = (User) iterator.next();
			developerOptions.add(new HtmlOption(String.valueOf(user.getId()), user.getName()));
		}
		Collections.sort(developerOptions);
		request.setAttribute("filterDeveloperOptions", developerOptions);

		if (hasActionErrors(request))
		{
			request.setAttribute("issues", Collections.EMPTY_LIST);
			return mapping.findForward("success");
		}

		if (Boolean.TRUE.equals(request.getAttribute("doNotSearch")))
		{
			request.setAttribute("issues", Collections.EMPTY_LIST);
			return mapping.findForward("success");
		}

		List issues = DaoLocator.getInstance().getIssueDao().findIssues(issueSearchForm.getFilterProjectId(),
			issueSearchForm.getFilterType(), issueSearchForm.getFilterPriority(), issueSearchForm.getFilterStatus(),
			issueSearchForm.getFilterReporter(), issueSearchForm.getFilterDeveloper(), issueSearchForm.getFilterIssueId(),
			issueSearchForm.getFilterSearchString(), issueSearchForm.getOrder());
		if (issues.isEmpty())
		{
			getActionMessages(request).add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage("IssuesVPAction.noIssues"));
			request.setAttribute("issues", Collections.EMPTY_LIST);
			return mapping.findForward("success");
		}

		List viewIssues = new ArrayList();
		Iterator issuesIterator = issues.iterator();
		while (issuesIterator.hasNext())
		{
			Issue issue = (Issue) issuesIterator.next();
			IssueValueObject issueValueObject = new IssueValueObject();

			issueValueObject.setProject(issue.getProject().getName());
			issueValueObject.setType(issue.getTypeName());
			issueValueObject.setPriority(issue.getPriority());
			issueValueObject.setStatus(issue.getStatusName());

			issueValueObject.setReporter(issue.getReporter().getName());

			if (issue.getDeveloper() != null)
			{
				issueValueObject.setDeveloper(issue.getDeveloper().getName());
			}
			else
			{
				issueValueObject.setDeveloper("-");
			}

			issueValueObject.setIssueId(issue.getId());
			if (issue.getName().length() > NAME_MAX_SHOW_LENGTH)
			{
				issueValueObject.setName(issue.getName().substring(0, NAME_MAX_SHOW_LENGTH - 3) + "...");
			}
			else
			{
				issueValueObject.setName(issue.getName());
			}

			Map issueParameters = new HashMap();
			issueParameters.put("issueId", new Integer(issue.getId()));
			issueValueObject.setIssueParameters(issueParameters);

			viewIssues.add(issueValueObject);
		}

		request.setAttribute("issues", viewIssues);
		return mapping.findForward("success");
	}
}
