//#############################################################################
//#                                                                           #
//#  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 Gabriel Maxim using IMS Development Environment (version 1.80 build 5567.19951)
// Copyright (C) 1995-2015 IMS MAXIMS. All rights reserved.

package ims.core.forms.adtupdateadmissiondetail;

import ims.core.vo.AdmissionDetailForADTUpdateAdmissionVo;
import ims.framework.enumerations.DialogResult;
import ims.framework.utils.DateTime;

import java.util.ArrayList;

public class Logic extends BaseLogic
{
	private static final long serialVersionUID = 1L;

	@Override
	protected void onFormModeChanged()
	{
		updateControlsState();
	}
	private void updateControlsState()
	{
		form.cmbSourceOfReferral().setVisible(!Boolean.TRUE.equals(form.getLocalContext().getbIsElectiveListAdmission()));		
		form.lblSourceOfEmergReferral().setVisible(!Boolean.TRUE.equals(form.getLocalContext().getbIsElectiveListAdmission()));
	}
	@Override
	protected void onFormOpen(Object[] args) throws ims.framework.exceptions.PresentationLogicException
	{
		initialise(args);
		open();
	}
	private void initialise(Object[] args)
	{
		if (args != null && args.length > 0 && args[0] instanceof Boolean)
			form.getLocalContext().setbIsElectiveListAdmission((Boolean) args[0]);
		
	}
	private void open()
	{
		populateScreenFromData(form.getGlobalContext().Core.getADTAdmissionDetailUpdate());
		updateControlsState();
		
	}
	private void populateScreenFromData(AdmissionDetailForADTUpdateAdmissionVo adtAdmissionDetailUpdate)
	{		
		if (adtAdmissionDetailUpdate == null)
			return;
		form.cmbPatientStatus().setValue(adtAdmissionDetailUpdate.getPatientCategory());
		form.cmbSourceOfReferral().setValue(adtAdmissionDetailUpdate.getSourceOfEmergencyReferral());
		form.dtimAdmissionDateTime().setValue(adtAdmissionDetailUpdate.getAdmissionDateTime());
		form.chkChaplain().setValue(adtAdmissionDetailUpdate.getRequiresChaplain()); //WDEV-23030
	}
	@Override
	protected void onBtnCancelClick() throws ims.framework.exceptions.PresentationLogicException
	{
		engine.close(DialogResult.CANCEL);
	}
	@Override
	protected void onBtnSaveClick() throws ims.framework.exceptions.PresentationLogicException
	{
		if (save())
			engine.close(DialogResult.OK);
	}
	private boolean save()
	{
		String[] errors = validateUiRules();
		if (errors != null)
		{
			engine.showErrors(errors);
			return false;
		}
		AdmissionDetailForADTUpdateAdmissionVo admissionDetailToUpdate = populateDataFromScreen(form.getGlobalContext().Core.getADTAdmissionDetailUpdate());
		String[] validationErrors = admissionDetailToUpdate.validate();
		if (validationErrors != null)
		{
			engine.showErrors("Validation Errors",errors);
			return false;
		}
		
		form.getGlobalContext().Core.setADTAdmissionDetailUpdate(admissionDetailToUpdate);
		return true;
	}
	private AdmissionDetailForADTUpdateAdmissionVo populateDataFromScreen(AdmissionDetailForADTUpdateAdmissionVo adtAdmissionDetailUpdate)
	{
		if (adtAdmissionDetailUpdate == null)
			return null;
		adtAdmissionDetailUpdate.setAdmissionDateTime(form.dtimAdmissionDateTime().getValue());
		adtAdmissionDetailUpdate.setSourceOfEmergencyReferral(form.cmbSourceOfReferral().getValue());
		adtAdmissionDetailUpdate.setPatientCategory(form.cmbPatientStatus().getValue());
		adtAdmissionDetailUpdate.setRequiresChaplain(form.chkChaplain().getValue()); //WDEV-23030
		
		return adtAdmissionDetailUpdate;
	}
	private String[] validateUiRules()
	{
		ArrayList<String> errors = new ArrayList<String>();
		
		if (form.cmbPatientStatus().getValue() == null)
			errors.add("Patient Category is mandatory.");

		if (form.dtimAdmissionDateTime().getValue() == null)
			errors.add("Admission Date/Time is mandatory.");
		
		if (form.dtimAdmissionDateTime().getValue() != null && form.dtimAdmissionDateTime().getValue().isGreaterThan(new DateTime()))
			errors.add("Admission Date/Time cannot be in the future.");
 		
		if (errors.size() == 0)
			return null;
		return errors.toArray(new String[errors.size()]);
	}
}
