//#############################################################################
//#                                                                           #
//#  Copyright (C) <2014>  <IMS MAXIMS>                                       #
//#                                                                           #
//#  This program is free software: you can redistribute it and/or modify     #
//#  it under the terms of the GNU Affero General Public License as           #
//#  published by the Free Software Foundation, either version 3 of the       #
//#  License, or (at your option) any later version.                          # 
//#                                                                           #
//#  This program 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 Affero General Public License for more details.                      #
//#                                                                           #
//#  You should have received a copy of the GNU Affero General Public License #
//#  along with this program.  If not, see <http://www.gnu.org/licenses/>.    #
//#                                                                           #
//#############################################################################
//#EOH
// This code was generated by Daniel Laffan using IMS Development Environment (version 1.65 build 3163.31063)
// Copyright (C) 1995-2008 IMS MAXIMS plc. All rights reserved.
package ims.ocrr.forms.investigationscomponent;
import ims.ocrr.forms.investigationscomponent.GenForm.grdResultsRow;
import ims.ocrr.forms.investigationscomponent.GenForm.grdResultsRowCollection;
import ims.configuration.gen.ConfigFlag;
import ims.domain.exceptions.DomainInterfaceException;
import ims.framework.exceptions.CodingRuntimeException;
import ims.framework.utils.Color;
import ims.framework.utils.Date;
import ims.framework.utils.DateTime;
import ims.framework.utils.Image;
import ims.ocrr.vo.OcsPathRadResultVo;
import ims.ocrr.vo.OcsPathRadResultVoCollection;
import ims.ocrr.vo.OrderInvestigationLiteVo;
import ims.ocrr.vo.OrderInvestigationLiteVoCollection;
import ims.ocrr.vo.OrderSpecimenLiteVo;
import ims.ocrr.vo.OrderSpecimenLiteVoCollection;
import ims.ocrr.vo.OrderedInvestigationStatusVo;
import ims.ocrr.vo.lookups.OrderInvStatus;
public class Logic extends BaseLogic
{
	private static final long	serialVersionUID	= 1L;
	//-----------------------------------------------------------------------------------------------------------------------------------------
	//	Component interface methods below here
	//-----------------------------------------------------------------------------------------------------------------------------------------
	/**
	 * WDEV-13944
	 * Initialise component (decide to list investigation for selected referral or all referrals) 
	 */
	public void initialise(Boolean canViewConfidentialInvsResults, Boolean canViewConfidentialInvsOrdered)
	{
		if(canViewConfidentialInvsOrdered == null || canViewConfidentialInvsResults == null)
			throw new CodingRuntimeException("mandatory params are null in method initialise");
		
		form.getLocalContext().setcanViewConfidentialInvsResults(canViewConfidentialInvsResults);
		form.getLocalContext().setcanViewConfidentialInvsOrdered(canViewConfidentialInvsOrdered);
		
		
		populateScreenFromData();
	}
	
	//-----------------------------------------------------------------------------------------------------------------------------------------
	
	protected void onFormOpen(Object[] args) throws ims.framework.exceptions.PresentationLogicException
	{
	
	}
	/**
	 * WDEV-13944
	 * Function used to populate screen with investigation
	 * Component might be used in more than one form - that is why is populating based on parameter
	 */
	private void populateScreenFromData()
	{
		form.grdResults().getRows().clear();
		form.grdResults().setReadOnly(false);
		OrderInvestigationLiteVoCollection results;
		try
		{
			results = domain.listResults(form.getGlobalContext().Core.getPatientShort());						
		}
		catch (DomainInterfaceException e)
		{
			updateTotal();
			engine.showMessage(e.getMessage());
			return;
		}
		if (results == null || results.size() == 0)
		{
			updateTotal();
			return;
		}
		Integer nNewResUnseenDays = new Integer(ConfigFlag.DOM.OCS_NEWRES_UNSEEN_CUTOFF.getValue());
		Date dateUnseen = new Date().addDay(-1 * nNewResUnseenDays.intValue());
		for (int x = 0; x < results.size(); x++)
		{
			addResult(results.get(x), dateUnseen);
		}
		cleanUpResultGrid();
		updateTotal();
	}
	private void updateTotal()
	{
		StringBuffer total = new StringBuffer();
		total.append("<b>");
		total.append("Total: ");
		total.append(form.grdResults().getRows().size());
		total.append("</b>");
		form.grdResults().setFooterValue(total.toString());
	}
	/**
	 * if there are any parent rows with no children - remove them ie. there are
	 * no viewable results for this specimen - WDEV-3953
	 */
	private void cleanUpResultGrid()
	{
		grdResultsRow pRow;
		for (int i = form.grdResults().getRows().size(); i > 0; i--)
		{
			pRow = form.grdResults().getRows().get(i - 1);
			if (pRow.getRows().size() == 0 && pRow.getColTestName() == null)
				form.grdResults().getRows().remove(i - 1);
		}
	}
	private void addResult(OrderInvestigationLiteVo orderInvestigationLiteVo, Date dateUnseen)
	{
		if (orderInvestigationLiteVo == null)
			return;
		// WDEV-3953
		/*boolean isConfidentialInv = orderInvestigationLiteVo.getInvestigationIsNotNull() && orderInvestigationLiteVo.getInvestigation().getInvestigationIndexIsNotNull() && orderInvestigationLiteVo.getInvestigation().getInvestigationIndex().getConfidentialTestIsNotNull() && orderInvestigationLiteVo.getInvestigation().getInvestigationIndex().getConfidentialTest().booleanValue();
		if (isConfidentialInv)
		{
			if (!form.getLocalContext().getcanViewConfidentialInvsOrdered())
			{
				return;
			}
			if (orderInvestigationLiteVo.getPathResultDetailsIsNotNull() || orderInvestigationLiteVo.getRadReportingDetailsIsNotNull())
			{
				if (!form.getLocalContext().getcanViewConfidentialInvsResults())
				{
					return;
				}
			}
		}*/
		grdResultsRow parentRow = createOrFindSpecimenGridRow(orderInvestigationLiteVo);
		if (parentRow == null)
			return;
		grdResultsRow row = null;
		if (parentRow.getColTestName() == null)
			row = parentRow;
		else
		{
			row = parentRow.getRows().newRow();
			row.setSelectable(false);
		}
		OcsPathRadResultVo res = new OcsPathRadResultVo();
		if(orderInvestigationLiteVo.getInvestigationIsNotNull() && orderInvestigationLiteVo.getInvestigation().getInvestigationIndexIsNotNull())
			res.setCategory(orderInvestigationLiteVo.getInvestigation().getInvestigationIndex().getCategory());
		res.setOrderInvestigation(orderInvestigationLiteVo);
		row.setValue(res);
		// Test Name
		if (orderInvestigationLiteVo.getInvestigationIsNotNull() && orderInvestigationLiteVo.getInvestigation().getInvestigationIndexIsNotNull() && orderInvestigationLiteVo.getInvestigation().getInvestigationIndex().getNameIsNotNull())
		{
			row.setColTestName(orderInvestigationLiteVo.getInvestigation().getInvestigationIndex().getName());
		}
		// ABN
		// WDEV-16224 - modifications following OCS DFT model changes
		if (orderInvestigationLiteVo.getResultDetailsIsNotNull() && orderInvestigationLiteVo.getResultDetails().getPathologyResultDetailsIsNotNull())
		{
			for (int i=0; i<orderInvestigationLiteVo.getResultDetails().getPathologyResultDetails().size(); i++)
			{
				if (orderInvestigationLiteVo.getResultDetails().getPathologyResultDetails().get(i).getIsAbnormalIsNotNull() && orderInvestigationLiteVo.getResultDetails().getPathologyResultDetails().get(i).getIsAbnormal().booleanValue())
				{
					row.setColABN(form.getImages().Core.CriticalError);
					row.setTooltipForColABN("Abnormal Result");
					break;
				}
			}
		}
		// Status
		if (orderInvestigationLiteVo.getOrdInvCurrentStatusIsNotNull() && orderInvestigationLiteVo.getOrdInvCurrentStatus().getOrdInvStatusIsNotNull())
		{
			OrderInvStatus currStat = orderInvestigationLiteVo.getOrdInvCurrentStatus().getOrdInvStatus();
			Image image = currStat.getImage();
			String szTooltip = generateStatusTooltip(orderInvestigationLiteVo.getOrdInvCurrentStatus());
			if (orderInvestigationLiteVo.getRepDateTimeIsNotNull() && dateUnseen != null)
			{
				if (currStat.equals(OrderInvStatus.NEW_RESULT) || currStat.equals(OrderInvStatus.UPDATED_RESULT))
				{
					if (orderInvestigationLiteVo.getRepDateTime().getDate().isLessThan(dateUnseen))
					{
						row.setBold(true);
						szTooltip = (szTooltip + "<br>Unseen");
					}
				}
				else if (currStat.equals(OrderInvStatus.REVIEW))
				{
					if (orderInvestigationLiteVo.getOrdInvCurrentStatus().getChangeDateTime().getDate().isLessThan(dateUnseen))
					{
						row.setBold(true);
						szTooltip = (szTooltip + "<br>Requires Attention");
					}
					else
						szTooltip = (szTooltip + "<br>" + OrderInvStatus.REVIEW.toString());
				}
			}
			row.setColStatus(image);
			row.setTooltipForColStatus(szTooltip);
			
			if(orderInvestigationLiteVo.getOrdInvCurrentStatusIsNotNull() && orderInvestigationLiteVo.getOrdInvCurrentStatus().getOrdInvStatusIsNotNull()
					&& (orderInvestigationLiteVo.getOrdInvCurrentStatus().getOrdInvStatus().equals(OrderInvStatus.CANCEL_REQUEST)
							|| orderInvestigationLiteVo.getOrdInvCurrentStatus().getOrdInvStatus().equals(OrderInvStatus.CANCELLED)))
				row.setBackColor(ConfigFlag.UI.CANCELLED_INVESTIGATION_ROW_COLOUR.getValue());
			else
				row.setBackColor(parentRow.getBackColor());
			
			row.setReadOnly(false);
		}
	}
	private grdResultsRow createOrFindSpecimenGridRow(OrderInvestigationLiteVo orderInvestigationLiteVo)
	{


Answer: 		if (orderInvestigationLiteVo == null)