//#############################################################################
//#                                                                           #
//#  Copyright (C) <2015>  <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/>.    #
//#                                                                           #
//#  IMS MAXIMS provides absolutely NO GUARANTEE OF THE CLINICAL SAFTEY of    #
//#  this program.  Users of this software do so entirely at their own risk.  #
//#  IMS MAXIMS only ensures the Clinical Safety of unaltered run-time        #
//#  software that it builds, deploys and maintains.                          #
//#                                                                           #
//#############################################################################
//#EOH
// This code was generated by Bogdan Tofei using IMS Development Environment (version 1.80 build 5007.25751)
// Copyright (C) 1995-2013 IMS MAXIMS. All rights reserved.

package ims.core.forms.staffmessaging;

import ims.framework.controls.DynamicGridCell;
import ims.framework.controls.DynamicGridColumn;
import ims.framework.controls.DynamicGridRow;
import ims.framework.enumerations.Alignment;
import ims.framework.enumerations.DynamicCellType;
import ims.framework.enumerations.VerticalAlignment;
import ims.framework.utils.DateTime;

public class Logic extends BaseLogic
{
	private static final long serialVersionUID = 1L;
	private static final String	COLUMN_SELECT     		= "0";
	private static final String	COLUMN_USER_IMAGE		= "1";
	private static final String	COLUMN_USER_NAME		= "2";
	private static final String	COLUMN_WAS_REPLYED      = "3";
	private static final String	COLUMN_DATE_TIME      	= "4";
	private static final String	COLUMN_MESSAGE          = "5";
	private static final String	COLUMN_FORWARD_IMAGE    = "6";
	private static final String	COLUMN_REPLY_IMAGE      = "7";
	private static final String	COLUMN_MARK_AS_READ     = "8";
	private static final String	COLUMN_DELETE           = "9";
	

	@Override
	protected void onFormOpen(Object[] args) throws ims.framework.exceptions.PresentationLogicException
	{
		form.htmNo().setHTML("<b><font color=" + "Red" + " size=" + 2 + ">(1)</font></b>");
		form.imgMail().setValue(form.getImages().Admin.Mail48);
		
		createGridColumns();
		populateMessagesGrid();
	}

	private void createGridColumns()
	{
		
		DynamicGridColumn columnName = form.dyngrdMessages().getColumns().newColumn(" ", COLUMN_SELECT);
		
		columnName.setWidth(25);
		columnName.setAlignment(Alignment.LEFT);
		columnName.setReadOnly(true);
		columnName.setCanGrow(Boolean.TRUE);
		
		columnName = form.dyngrdMessages().getColumns().newColumn(" ", COLUMN_USER_IMAGE);
		columnName.setWidth(55);
		columnName.setAlignment(Alignment.LEFT);
		columnName.setReadOnly(true);
		columnName.setCanGrow(Boolean.TRUE);
		
		columnName = form.dyngrdMessages().getColumns().newColumn("MOS", COLUMN_USER_NAME);
		columnName.setWidth(90);
		columnName.setAlignment(Alignment.LEFT);
		columnName.setReadOnly(true);
		columnName.setCanGrow(Boolean.TRUE);
		
		columnName = form.dyngrdMessages().getColumns().newColumn(" ", COLUMN_WAS_REPLYED);
		columnName.setWidth(30);
		columnName.setAlignment(Alignment.LEFT);
		columnName.setReadOnly(true);
		columnName.setCanGrow(Boolean.TRUE);
		
		columnName = form.dyngrdMessages().getColumns().newColumn("Date Time", COLUMN_DATE_TIME);
		columnName.setWidth(100);
		columnName.setAlignment(Alignment.LEFT);
		columnName.setReadOnly(true);
		columnName.setCanGrow(Boolean.TRUE);
		
		columnName = form.dyngrdMessages().getColumns().newColumn("Message", COLUMN_MESSAGE);
		columnName.setWidth(420);
		columnName.setAlignment(Alignment.LEFT);
		columnName.setReadOnly(true);
		columnName.setCanGrow(Boolean.TRUE);
		
		columnName = form.dyngrdMessages().getColumns().newColumn(" ", COLUMN_REPLY_IMAGE);
		columnName.setWidth(30);
		columnName.setAlignment(Alignment.LEFT);
		columnName.setReadOnly(true);
		columnName.setCanGrow(Boolean.TRUE);
		
		columnName = form.dyngrdMessages().getColumns().newColumn(" ", COLUMN_FORWARD_IMAGE);
		columnName.setWidth(30);
		columnName.setAlignment(Alignment.LEFT);
		columnName.setReadOnly(true);
		columnName.setCanGrow(Boolean.TRUE);
		
		columnName = form.dyngrdMessages().getColumns().newColumn(" ", COLUMN_MARK_AS_READ);
		columnName.setWidth(30);
		columnName.setAlignment(Alignment.LEFT);
		columnName.setReadOnly(true);
		columnName.setCanGrow(Boolean.TRUE);
		
		columnName = form.dyngrdMessages().getColumns().newColumn(" ", COLUMN_DELETE);
		columnName.setWidth(30);
		columnName.setAlignment(Alignment.LEFT);
		columnName.setReadOnly(true);
		columnName.setCanGrow(Boolean.TRUE);
		
	}

	private DynamicGridColumn getColumnById(String identifier)
	{
		return form.dyngrdMessages().getColumns().getByIdentifier(identifier);
	}
	
	private void populateMessagesGrid()
	{
		form.dyngrdMessages().getRows().clear();
		
		
		//add first row
		DynamicGridRow row = form.dyngrdMessages().getRows().newRow();
		
		row.setSelectable(false);
		
		DynamicGridCell cellSelect = row.getCells().newCell(getColumnById(COLUMN_SELECT), DynamicCellType.BOOL);
		
		cellSelect.getColumn().setAlignment(Alignment.CENTER);
		cellSelect.getColumn().setVerticalAlignment(VerticalAlignment.MIDDLE);
		cellSelect.setReadOnly(false);
		cellSelect.setValue(Boolean.TRUE);
		
		DynamicGridCell cellImage = row.getCells().newCell(getColumnById(COLUMN_USER_IMAGE), DynamicCellType.IMAGE);
		
		cellImage.setValue(form.getImages().Admin.HCP48);
		cellImage.setReadOnly(true);
		
		DynamicGridCell cellUsername = row.getCells().newCell(getColumnById(COLUMN_USER_NAME), DynamicCellType.STRING);
		
		cellUsername.setReadOnly(true);
		cellUsername.getColumn().setVerticalAlignment(VerticalAlignment.MIDDLE);
		
		cellUsername.setValue("<b>Neil Turner</b>");
		
		DynamicGridCell cellDateTime = row.getCells().newCell(getColumnById(COLUMN_DATE_TIME), DynamicCellType.STRING);
		
		cellDateTime.setReadOnly(true);
		cellDateTime.getColumn().setVerticalAlignment(VerticalAlignment.MIDDLE);
		cellDateTime.setValue((new DateTime()).toString());
		
		DynamicGridCell cellMessage = row.getCells().newCell(getColumnById(COLUMN_MESSAGE), DynamicCellType.HTMLVIEW);
		
		cellMessage.setReadOnly(true);
		cellMessage.setValue("<div><b>That's not surprising if you're on furosemide. You could try...</b></div>");
		
		addLastThreeColumns(row);
		
		
		
		//add second row
		row = form.dyngrdMessages().getRows().newRow();
		row.setSelectable(false);
		
		DynamicGridCell cellSelect1 = row.getCells().newCell(getColumnById(COLUMN_SELECT), DynamicCellType.BOOL);
		
		cellSelect1.getColumn().setAlignment(Alignment.CENTER);
		cellSelect1.getColumn().setVerticalAlignment(VerticalAlignment.MIDDLE);
		cellSelect1.setReadOnly(false);
		
		DynamicGridCell cellImage1 = row.getCells().newCell(getColumnById(COLUMN_USER_IMAGE), DynamicCellType.IMAGE);
		
		cellImage1.setValue(form.getImages().Admin.Nurse48);
		cellImage1.setReadOnly(true);
		
		DynamicGridCell cellUsername1 = row.getCells().newCell(getColumnById(COLUMN_USER_NAME), DynamicCellType.STRING);
		
		cellUsername1.setReadOnly(true);
		cellUsername1.getColumn().setVerticalAlignment(VerticalAlignment.MIDDLE);
		
		cellUsername1.setValue("<b>Jenny Ure</b>");
		
		DynamicGridCell cellReplyed = row.getCells().newCell(getColumnById(COLUMN_WAS_REPLYED), DynamicCellType.IMAGE);
		
		cellReplyed.setReadOnly(true);
		cellReplyed.getColumn().setVerticalAlignment(VerticalAlignment.MIDDLE);
		
		cellReplyed.setValue(form.getImages().Core.Maximize);
		
		DynamicGridCell cellDateTime1 = row.getCells().newCell(getColumnById(COLUMN_DATE_TIME), DynamicCellType.STRING);
		
		cellDateTime1.setReadOnly(true);
		cellDateTime1.getColumn().setVerticalAlignment(VerticalAlignment.MIDDLE);
		cellDateTime1.setValue("29/10/2013 13:41");
		
		DynamicGridCell cellMessage1 = row.getCells().newCell(getColumnById(COLUMN_MESSAGE), DynamicCellType.HTMLVIEW);
		
		cellMessage1.setReadOnly(true);
		cellMessage1.setValue("<div>Hi Neil - There are 4 'additional' people at the end of list - Chester etc - do you want me to message them as well or are they for something else ?</div>");
		
		addLastThreeColumns(row);
		
		
		
		//add third row
		row = form.dyngrdMessages().getRows().newRow();
		row.setSelectable(false);
		
		DynamicGridCell cellSelect2 = row.getCells().newCell(getColumnById(COLUMN_SELECT), DynamicCellType.BOOL);
		
		cellSelect2.getColumn().setAlignment(Alignment.CENTER);
		cellSelect2.getColumn().setVerticalAlignment(VerticalAlignment.MIDDLE);
		cellSelect2.setReadOnly(false);
		
		DynamicGridCell cellImage2 = row.getCells().newCell(getColumnById(COLUMN_USER_IMAGE), DynamicCellType.IMAGE);
		
		cellImage2.setValue(form.getImages().Admin.MosAdmin48);
		cellImage2.setReadOnly(true);
		
		DynamicGridCell cellUsername2 = row.getCells().newCell(getColumnById(COLUMN_USER_NAME), DynamicCellType.STRING);
		
		cellUsername2.setReadOnly(true);
		cellUsername2.getColumn().setVerticalAlignment(VerticalAlignment.MIDDLE);
		
		cellUsername2.setValue("<b>Jim Jones</b>");
		
		DynamicGridCell cellDateTime2 = row.getCells().newCell(getColumnById(COLUMN_DATE_TIME), DynamicCellType.STRING);
		
		cellDateTime2.setReadOnly(true);
		cellDateTime2.getColumn().setVerticalAlignment(VerticalAlignment.MIDDLE);
		cellDateTime2.setValue("29/10/2013 12:20");
		
		DynamicGridCell cellMessage2 = row.getCells().newCell(getColumnById(COLUMN_MESSAGE), DynamicCellType.HTMLVIEW);
		
		cellMessage2.setReadOnly(true);
		cellMessage2.setValue("<div>If you need any further clarifications, please contact me.</div>");
		
		addLastThreeColumns(row);
		
	}

	private void addLastThreeColumns(DynamicGridRow row)
	{
		DynamicGridCell cellReply = row.getCells().newCell(getColumnById(COLUMN_REPLY_IMAGE), DynamicCellType.IMAGE);
		
		cellReply.getColumn().setAlignment(Alignment.CENTER);
		cellReply.getColumn().setVerticalAlignment(VerticalAlignment.MIDDLE);
		cellReply.setReadOnly(true);
		cellReply.setValue(form.getImages().Correspondence.MailReply16);
		
		DynamicGridCell cellForward = row.getCells().newCell(getColumnById(COLUMN_FORWARD_IMAGE), DynamicCellType.IMAGE);
		
		cellForward.getColumn().setAlignment(Alignment.CENTER);
		cellForward.getColumn().setVerticalAlignment(VerticalAlignment.MIDDLE);
		cellForward.setReadOnly(true);
		cellForward.setValue(form.getImages().Correspondence.MailSend16);
		
		DynamicGridCell cellRead = row.getCells().newCell(getColumnById(COLUMN_MARK_AS_READ), DynamicCellType.IMAGE);
		
		cellRead.getColumn().setAlignment(Alignment.CENTER);
		cellRead.getColumn().setVerticalAlignment(VerticalAlignment.MIDDLE);
		cellRead.setReadOnly(true);
		cellRead.setValue(form.getImages().Core.NotificationSeenLow16);
		
		DynamicGridCell cellDelete = row.getCells().newCell(getColumnById(COLUMN_DELETE), DynamicCellType.IMAGE);
		
		cellDelete.getColumn().setAlignment(Alignment.CENTER);
		cellDelete.getColumn().setVerticalAlignment(VerticalAlignment.MIDDLE);
		cellDelete.setReadOnly(true);
		cellDelete.setValue(form.getImages().Core.Delete);
	}

	@Override
	protected void onFormDialogClosed(ims.framework.FormName formName, ims.framework.enumerations.DialogResult result) throws ims.framework.exceptions.PresentationLogicException
	{
		// TODO Add your code here.
	}

	@Override
	protected void onDyngrdMessagesRowSelectionChanged(ims.framework.controls.DynamicGridRow row)
	{
		// TODO Add your code here.
	}
}
