/*******************************************************************************
 *  Copyright 2017 Centre for Development of Advanced Computing(C-DAC), Pune
 *  
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *  
 *    http://www.apache.org/licenses/LICENSE-2.0
 *  
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *******************************************************************************/
package cdac.medinfo.sdk.hl7282.baselibs;

import cdac.medinfo.sdk.hl7282.baselibs.segmentfactory.DataElementFactory;
import cdac.medinfo.sdk.hl7282.commoninterface.IDataType;
import cdac.medinfo.sdk.hl7282.commoninterface.ISingleCollection;
import cdac.medinfo.sdk.hl7282.exceptions.InvalidComponentTypeException;
import cdac.medinfo.sdk.hl7282.exceptions.InvalidDataTypeException;
import cdac.medinfo.sdk.hl7282.exceptions.MissingValueException;
import cdac.medinfo.sdk.hl7282.utils.EnumDataType;
import cdac.medinfo.sdk.hl7282.utils.IDataTypeMap;

/**
 * RPT - Repeat pattern<p>
 * This data type should be used where it is necessary to define the 
 * frequency at which an event is to take place.
 * <p>Represents an HL7 RPT data type. 
 * This data type has the following attributes:</p><p>
 *  1. Repeat Pattern Code (CWE)<b>Required</b><br>
 *  2. Calendar Alignment (ID)<b>Optional</b><br>
 *  3. Phase Range Begin Value (NM)<b>Optional</b><br>
 *  4. Phase Range End Value (NM)<b>Optional</b><br>
 *  5. Period Quantity (NM)<b>Optional</b><br>
 *  6. Period Units (CWE)<b>Conditional</b><br>
 *  7. Institution Specified Time (ID)<b>Optional</b><br>
 *  8. Event (ID)<b>Optional</b><br>
 *  9. Event Offset Quantity (NM)<b>Optional</b><br>
 * 10. Event Offset Units (CWE)<b>Conditional</b><br>
 * 11. General Timing Specification (GTS)<b>Optional</b><br><p>
 * This class provides accessors & mutators for the elements mentioned above.
 * This class override the equal method, to check whether some other object is "equal to" this one.
 * 
 * <p>User can also create Data Type through {@link DataElementFactory} 
 */
public class RPT extends CompositeDataType    
{
	


	/**
	 * Default Constructor for RPT
	 */
	public RPT()
	{
		super(EnumDataType.HL7_DT_RPT);
		 
	}

	/**
	 * Constructor  for CQ class which takes four arguments and 
	 * sets the value in the respective member variable.
	 * @param iMinLength The Minimum length the DataType can have.
	 * @param iMaxLength The Maximum length the DataType can have.
	 * @param iCLength The Conformance length the DataType can have.
	 * @param iComponentType The Type of the component i.e Component or a subcomponent.
	 * @throws InvalidComponentTypeException - if Component Type is not specified or valid. 
	 */ 
	public RPT(int iMinLength, int iMaxLength, String strCLength,int iComponentType) throws InvalidComponentTypeException
	{
		super(EnumDataType.HL7_DT_RPT);
		this.iMinLength = iMinLength;
		this.iMaxLength = iMaxLength;
		this.iConformanceLength = strCLength;
		this.iComponentType = iComponentType;
		if(!isValidComponentType())
		{
			throw new InvalidComponentTypeException(this.getClass().getSimpleName());
		}
	}


	/**
	 * Constructor for RPT class which takes five arguments and 
	 * sets the value in the respective member variable.
	 * @param iMinLength The Minimum length the DataType can have.
	 * @param iMaxLength The Maximum length the DataType can have.
	 * @param iCLength The Conformance length the DataType can have.
	 * @param iTableNo The Table number to which the value for the component is specified. 
	 * @param iComponentType The Type of the component i.e Component or a subcomponent.
	 * @throws InvalidComponentTypeException - if Component Type is not specified or valid. 
	 */
	public RPT(int iMinLength, int iMaxLength, String strCLength,int iTableNo,int iComponentType) throws InvalidComponentTypeException
	{
		super(EnumDataType.HL7_DT_RPT);
		this.iMinLength = iMinLength;
		this.iMaxLength = iMaxLength;
		this.iConformanceLength = strCLength;
		this.iTableNo = iTableNo;
		this.iComponentType = iComponentType;
		if(!isValidComponentType())
		{
			throw new InvalidComponentTypeException(this.getClass().getSimpleName());		
		}
	}
	/**
	 * Constructor for RPT class which takes two arguments and 
	 * sets the value in the respective member variable.
	 * @param objIDataTypeMap object of Data Type Map.
	 * @param iComponentType The Type of the component i.e Component or a subcomponent.
	 * @throws InvalidComponentTypeException - if Component Type is not specified or valid. 
	 */
	public RPT(int iComponentType,IDataTypeMap objIDataTypeMap)
	{
		super(EnumDataType.HL7_DT_RPT);
		this.objIDataTypeMap = objIDataTypeMap;
		this.iComponentType = iComponentType;
		if(!isValidComponentType())
		{
			throw new InvalidComponentTypeException(this.getClass().getSimpleName());
		}
	}
	/**
	 * Constructor for RPT class which takes five arguments and 
	 * sets the value in the respective member variable.
	 * @param objIDataTypeMap object of Data Type Map.
	 * @param iComponentType The Type of the component i.e Component or a subcomponent.
	 * @param iMinLength The Minimum length the DataType can have.
	 * @param iMaxLength The Maximum length the DataType can have.
	 * @param iCLength The Conformance length the DataType can have.
	 * @throws InvalidComponentTypeException - if Component Type is not specified or valid. 
	 */
	public RPT(int iComponentType,int iMinLength, int iMaxLength, String strCLength,IDataTypeMap objIDataTypeMap)
	{
		super(EnumDataType.HL7_DT_RPT);
		this.objIDataTypeMap = objIDataTypeMap;
		this.iComponentType = iComponentType;
		this.iMinLength = iMinLength;
		this.iMaxLength = iMaxLength;
		this.iConformanceLength = strCLength;

		if(!isValidComponentType())
		{
			throw new InvalidComponentTypeException(this.getClass().getSimpleName());
		}
	}
	/**
	 * Constructor for RPT class which takes six arguments and 
	 * sets the value in the respective member variable.
	 * @param objIDataTypeMap object of Data Type Map.
	 * @param iComponentType The Type of the component i.e Component or a subcomponent.
	 * @param iMinLength The Minimum length the DataType can have.
	 * @param iMaxLength The Maximum length the DataType can have.
	 * @param iCLength The Conformance length the DataType can have.
	 * @param iTableNo The Table number to which the value for the component is specified. 
	 * @throws InvalidComponentTypeException - if Component Type is not specified or valid. 
	 */
	public RPT(int iComponentType,int iMinLength, int iMaxLength, String strCLength,int ITableNo,IDataTypeMap objIDataTypeMap)
	{
		super(EnumDataType.HL7_DT_RPT);
		this.objIDataTypeMap = objIDataTypeMap;
		this.iComponentType = iComponentType;
		this.iTableNo = ITableNo;
		this.iMinLength = iMinLength;
		this.iMaxLength = iMaxLength;
		this.iConformanceLength = strCLength;

		if(!isValidComponentType())
		{
			throw new InvalidComponentTypeException(this.getClass().getSimpleName());
		}
	}

	/**
	 * Sets the RepeatPatternCode.
	 * @param objRepeatPatternCode A code representing the repeat pattern defined by the other components of this data type. 
	 */
	public void setRepeatPatternCode(CWE objRepeatPatternCode)
	{
		setComponentType(1,objRepeatPatternCode); 
	}

	/**
	 *Sets the CalendarAlignment.
	 *@param strCalendarAlignment Specifies an alignment of the repetition to a calendar  
	 */
	public void setCalendarAlignment(String strCalendarAlignment)
	{
		if(strCalendarAlignment!=null)
			setComponentType(2, strCalendarAlignment);
		else
			setCalendarAlignment((ID)null);
	}

	/**
	 *Sets the CalendarAlignment.
	 *@param objID Specifies an alignment of the repetition to a calendar 
	 */
	public void setCalendarAlignment(ID objID)
	{
		setComponentType(2,objID);
	}

	/**
	 *Sets the PhaseRangeBeginValue.
	 *@param strPhaseRangeBeginValue Used for Calendar aligned repeat patterns to determine the 
	 * 									amount of time from the beginning of particular RPT-2 (Calendar Alignment) 
	 * 									to the beginning of the phase. 
	 */
	public void setPhaseRangeBeginValue(String strPhaseRangeBeginValue)
	{
		if(strPhaseRangeBeginValue!=null)
			setComponentType(3, strPhaseRangeBeginValue);
		else
			setPhaseRangeBeginValue((NM)null);
	}

	/**
	 *Sets the PhaseRangeBeginValue.
	 *@param strPhaseRangeBeginValue Used for Calendar aligned repeat patterns to determine the 
	 * 									amount of time from the beginning of particular RPT-2 (Calendar Alignment) 
	 * 									to the beginning of the phase. 
	 */
	public void setPhaseRangeBeginValue(NM objNM)
	{
		setComponentType(3,objNM);
	}

	/**
	 * Sets the PhaseRangeEndValue.
	 * @param strPhaseRangeEndValue Used for Calendar aligned repeat patterns to determine the amount 
	 * 								  of time from the beginning of particular RPT-2 (Calendar Alignment) 
	 * 								to the end of the phase. 
	 */
	public void setPhaseRangeEndValue(String strPhaseRangeEndValue)
	{
		if(strPhaseRangeEndValue!=null)
			setComponentType(4, strPhaseRangeEndValue);
		else
			setPhaseRangeEndValue((NM)null);
	}  

	/**
	 * Sets the PhaseRangeEndValue.
	 * @param objNM Used for Calendar aligned repeat patterns to determine the amount 
	 * 								  of time from the beginning of particular RPT-2 (Calendar Alignment) 
	 * 								to the end of the phase. 
	 */
	public void setPhaseRangeEndValue(NM objNM)
	{
		setComponentType(4,objNM);
	}
	/**
	 *Sets the PeriodQuantity.
	 *@param strPeriodQuantity A time duration specifying the frequency at which the periodic interval repeats.  
	 */
	public void setPeriodQuantity(String strPeriodQuantity)
	{
		if(strPeriodQuantity!=null)
			setComponentType(5, strPeriodQuantity);
		else
			setPeriodQuantity((NM)null);
	}

	/**
	 *Sets the PeriodQuantity.
	 *@param objNM A time duration specifying the frequency at which the periodic interval repeats. 
	 */
	public void setPeriodQuantity(NM objNM)
	{
		setComponentType(5,objNM);
	}


	/**
	 *Sets the PeriodUnits.
	 *@param strPeriodUnits Defines the units used for RPT-5 (Period Quantity).  
	 */
	public void setPeriodUnits(String strPeriodUnits)
	{
		if(strPeriodUnits!=null)
			setComponentType(6, strPeriodUnits);
		else
			setPeriodUnits((CWE)null);
	}
	
	/**
	 *Sets the PeriodUnits.
	 *@param objCWE Defines the units used for RPT-5 (Period Quantity). 
	 */
	public void setPeriodUnits(CWE objCWE)
	{
		setComponentType(6,objCWE);
	}

	/**
	 *Sets the InstitutionSpecifiedTime.
	 *@param strInstitutionSpecifiedTime a code that indicates whether the exact timing is up to the party executing the schedule  
	 */
	public void setInstitutionSpecifiedTime(String strInstitutionSpecifiedTime)
	{
		if(strInstitutionSpecifiedTime!=null)
			setComponentType(7, strInstitutionSpecifiedTime);
		else
			setInstitutionSpecifiedTime((ID)null);
	}

	/**
	 *Sets the InstitutionSpecifiedTime.
	 *@param objID a code that indicates whether the exact timing is up to the party executing the schedule 
	 */
	public void setInstitutionSpecifiedTime(ID objID)
	{
		setComponentType(7,objID);
	}

	/**
	 *Sets the Event.
	 *@param strEvent A code for a common (periodical) activity of daily living.  
	 */
	public void setEvent(String strEvent)
	{
		if(strEvent!=null)
			setComponentType(8, strEvent);
		else
			setEvent((ID)null);
	}

	/**
	 *Sets the Event.
	 *@param objID A code for a common (periodical) activity of daily living. 
	 */
	public void setEvent(ID objID)
	{
		setComponentType(8,objID);
	}

	/**
	 *Sets the EventOffsetQuantity.
	 *@param strEventOffsetQuantity An interval that marks the offsets for the beginning, width and end of the 
	 * 									event-related periodic interval measured from the time each such event actually occurred. 
	 */
	public void setEventOffsetQuantity(String strEventOffsetQuantity)
	{
		if(strEventOffsetQuantity!=null)
			setComponentType(9, strEventOffsetQuantity);
		else
			setEventOffsetQuantity((NM)null);
	}

	/**
	 *Sets the EventOffsetQuantity.
	 *@param strEventOffsetQuantity An interval that marks the offsets for the beginning, width and end of the 
	 * 									event-related periodic interval measured from the time each such event actually occurred. 
	 */
	public void setEventOffsetQuantity(NM objNM)
	{
		setComponentType(9,objNM);
	}


	/**
	 *Sets the EventOffsetUnits.
	 *@param strEventOffsetUnits Defines the units used for RPT-9 (Event Offset Quantity).  
	 */
	public void setEventOffsetUnits(String strEventOffsetUnits)
	{
		if(strEventOffsetUnits!=null)
			setComponentType(10, strEventOffsetUnits);
		else
			setEventOffsetUnits((CWE)null);
	}
	
	/**
	 *Sets the EventOffsetUnits.
	 *@param objCWE Defines the units used for RPT-9 (Event Offset Quantity). 
	 */
	public void setEventOffsetUnits(CWE objCWE)
	{
		setComponentType(10,objCWE);
	}

	/**
	 *Sets the GeneralTimingSpecification.
	 *@param strGeneralTimingSpecification The General Timing Specification as defined by the Version 3 Data Types document. 
	 */
	public void setGeneralTimingSpecification(String strGeneralTimingSpecification)
	{
		if(strGeneralTimingSpecification!=null)
			setComponentType(11, strGeneralTimingSpecification);
		else
			setGeneralTimingSpecification((GTS)null);
	}

	/**
	 *Sets the GeneralTimingSpecification.
	 *@param objGTS The General Timing Specification as defined by the Version 3 Data Types document.
	 */
	public void setGeneralTimingSpecification(GTS objGTS)
	{
		setComponentType(11,objGTS);
	}

	/**
	 * Returns Repeat Pattern Code
	 * @return CWE - data type.
	 */
	public CWE getRepeatPatternCode()
	{
		ISingleCollection<IDataType> objDataCollection =getComponentType(1);
		if(objDataCollection != null && objDataCollection.size() != 0) 
			return (CWE)objDataCollection.elementAt(0);
		else
			return null;
	}

	/**
	 * Returns Calendar Alignment
	 * @return ID - data type.
	 */
	public ID getCalendarAlignment()
	{
		ISingleCollection<IDataType> objDataCollection =getComponentType(2);
		if(objDataCollection != null && objDataCollection.size() != 0) 
			return (ID)objDataCollection.elementAt(0);
		else
			return null;
	}

	/**
	 * Returns Phase Range Begin Value
	 * @return NM - data type.
	 */
	public NM getPhaseRangeBeginValue()
	{
		ISingleCollection<IDataType> objDataCollection =getComponentType(3);
		if(objDataCollection != null && objDataCollection.size() != 0) 
			return (NM)objDataCollection.elementAt(0);
		else
			return null;
	}

	/**
	 * Returns Phase Range End Value
	 * @return NM - data type.
	 */
	public NM getPhaseRangeEndValue()
	{
		ISingleCollection<IDataType> objDataCollection =getComponentType(4);
		if(objDataCollection != null && objDataCollection.size() != 0) 
			return (NM)objDataCollection.elementAt(0);
		else
			return null;
	}

	/**
	 * Returns Period Quantity
	 * @return NM - data type.
	 */
	public NM getPeriodQuantity()
	{
		ISingleCollection<IDataType> objDataCollection =getComponentType(5);
		if(objDataCollection != null && objDataCollection.size() != 0) 
			return (NM)objDataCollection.elementAt(0);
		else
			return null;
	}

	/**
	 * Returns Period Units
	 * @return CWE - data type.
	 */
	public CWE getPeriodUnits()
	{
		ISingleCollection<IDataType> objDataCollection =getComponentType(6);
		if(objDataCollection != null && objDataCollection.size() != 0) 
			return (CWE)objDataCollection.elementAt(0);
		else
			return null;
	}

	/**
	 * Returns Institution Specified Time
	 * @return ID - data type.
	 */
	public ID getInstitutionSpecifiedTime()
	{
		ISingleCollection<IDataType> objDataCollection =getComponentType(7);
		if(objDataCollection != null && objDataCollection.size() != 0) 
			return (ID)objDataCollection.elementAt(0);
		else
			return null;
	}

	/**
	 * Returns Event
	 * @return ID - data type.
	 */
	public ID getEvent()
	{
		ISingleCollection<IDataType> objDataCollection =getComponentType(8);
		if(objDataCollection != null && objDataCollection.size() != 0) 
			return (ID)objDataCollection.elementAt(0);
		else
			return null;
	}

	/**
	 * Returns Event Offset Quantity
	 * @return NM - data type.
	 */
	public NM getEventOffsetQuantity()
	{
		ISingleCollection<IDataType> objDataCollection =getComponentType(9);
		if(objDataCollection != null && objDataCollection.size() != 0) 
			return (NM)objDataCollection.elementAt(0);
		else
			return null;
	}

	/**
	 * Returns Event Offset Units
	 * @return CWE - data type.
	 */
	public CWE getEventOffsetUnits()
	{
		ISingleCollection<IDataType> objDataCollection =getComponentType(10);
		if(objDataCollection != null && objDataCollection.size() != 0) 
			return (CWE)objDataCollection.elementAt(0);
		else
			return null;
	}

	/**
	 * Returns General Timing Specification
	 * @return GTS - data type.
	 */
	public GTS getGeneralTimingSpecification()
	{
		ISingleCollection<IDataType> objDataCollection =getComponentType(11);
		if(objDataCollection != null && objDataCollection.size() != 0) 
			return (GTS)objDataCollection.elementAt(0);
		else
			return null;
	}

	/**
	 * The Method validates the Input value,and if validated properly then returns true.
	 * Here the validetion is based on the compulsory fields for the Datatype. 
	 * @return boolean - true or false.
	 * @throws InvalidDataTypeException - if Invalid values is entered or Mandatory field is missing.
	 */
	public boolean validate() throws InvalidDataTypeException
	{
		super.validate();
		/*
		 * frist field is required.
		 * if 6th field is present then 5th is must.
		 * if 10th field is present then 9th is must.
		 */
		if(getComponentType(1)==null)
		{
			throw new MissingValueException(this.getClass().getSimpleName() + "Repeat Pattern Code");
		}
		else if(getComponentType(6)!=null)
		{
			if(getComponentType(5)==null)
			{
				throw new MissingValueException(this.getClass().getSimpleName() + "Period Units 'required if Period Quantity  is populated");
			}
		}
		if(getComponentType(10)!=null)
		{
			if(getComponentType(9)==null)
			{
				throw new MissingValueException(this.getClass().getSimpleName() + "Event Offset Quantity 'should not be valued unless there is a value in 'Event '");
			}
		}
		return true;
	}

	/**
	 * This method checks the equality of Object.
	 * @param objObject object of Object class.
	 * @return boolean - shows success or failure of the comparison.
	 */
	public boolean equals(Object objObject)
	{
		if((objObject == null ) || (!(objObject instanceof RPT)))
			return false;

		if(this.getRepeatPatternCode() == null)
		{
			if(((RPT)objObject).getRepeatPatternCode() != null)
				return false;
		}
		else if(false == (this.getRepeatPatternCode().equals(((RPT)objObject).getRepeatPatternCode())))
			return false;

		if(this.getCalendarAlignment() == null)
		{
			if(((RPT)objObject).getCalendarAlignment() != null)
				return false;
		}
		else if(false == (this.getCalendarAlignment().equals(((RPT)objObject).getCalendarAlignment())))
			return false;

		if(this.getPhaseRangeBeginValue() == null)
		{
			if(((RPT)objObject).getPhaseRangeBeginValue() != null)
				return false;
		}
		else if(false == (this.getPhaseRangeBeginValue().equals(((RPT)objObject).getPhaseRangeBeginValue())))
			return false;

		if(this.getPhaseRangeEndValue() == null)
		{
			if(((RPT)objObject).getPhaseRangeEndValue() != null)
				return false;
		}
		else if(false == (this.getPhaseRangeEndValue().equals(((RPT)objObject).getPhaseRangeEndValue())))
			return false;

		if(this.getPeriodQuantity() == null)
		{
			if(((RPT)objObject).getPeriodQuantity() != null)
				return false;
		}
		else if(false == (this.getPeriodQuantity().equals(((RPT)objObject).getPeriodQuantity())))
			return false;

		if(this.getPeriodUnits() == null)
		{
			if(((RPT)objObject).getPeriodUnits() != null)
				return false;
		}
		else if(false == (this.getPeriodUnits().equals(((RPT)objObject).getPeriodUnits())))
			return false;

		if(this.getInstitutionSpecifiedTime() == null)
		{
			if(((RPT)objObject).getInstitutionSpecifiedTime() != null)
				return false;
		}
		else if(false == (this.getInstitutionSpecifiedTime().equals(((RPT)objObject).getInstitutionSpecifiedTime())))
			return false;

		if(this.getEvent() == null)
		{
			if(((RPT)objObject).getEvent() != null)
				return false;
		}
		else if(false == (this.getEvent().equals(((RPT)objObject).getEvent())))
			return false;

		if(this.getEventOffsetQuantity() == null)
		{
			if(((RPT)objObject).getEventOffsetQuantity() != null)
				return false;
		}
		else if(false == (this.getEventOffsetQuantity().equals(((RPT)objObject).getEventOffsetQuantity())))
			return false;

		if(this.getEventOffsetUnits() == null)
		{
			if(((RPT)objObject).getEventOffsetUnits() != null)
				return false;
		}
		else if(false == (this.getEventOffsetUnits().equals(((RPT)objObject).getEventOffsetUnits())))
			return false;

		if(this.getGeneralTimingSpecification() == null)
		{
			if(((RPT)objObject).getGeneralTimingSpecification() != null)
				return false;
		}
		else if(false == (this.getGeneralTimingSpecification().equals(((RPT)objObject).getGeneralTimingSpecification())))
			return false;

		return true;
	}
}
