/*******************************************************************************
 *  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.segments;

import cdac.medinfo.sdk.hl7282.baselibs.CCD;
import cdac.medinfo.sdk.hl7282.baselibs.CWE;
import cdac.medinfo.sdk.hl7282.baselibs.CX;
import cdac.medinfo.sdk.hl7282.baselibs.DTM;
import cdac.medinfo.sdk.hl7282.baselibs.HD;
import cdac.medinfo.sdk.hl7282.baselibs.ID;
import cdac.medinfo.sdk.hl7282.commoninterface.IDataType;
import cdac.medinfo.sdk.hl7282.commoninterface.ISingleCollection;
import cdac.medinfo.sdk.hl7282.exceptions.FieldMissingException;
import cdac.medinfo.sdk.hl7282.exceptions.InvalidSegmentException;
import cdac.medinfo.sdk.hl7282.utils.EnumSegments;

/**
 * BLG - Billing Segment.<p>
 * This segment is used to provide billing information, on the ordered service, 
 * to the filling application.
 * <p>Represents an HL7 BLG message segment. 
 * This segment has the following fields:</p><p>
 * BLG-1: When to Charge (CCD)<b>Optional</b><br> 
 * BLG-2: Charge Type (ID)<b>Optional</b><br> 
 * BLG-3: Account ID (CX)<b>Optional</b><br> 
 * BLG-4: Charge Type Reason (CWE)<b>Optional</b><br><p>
 * This class provides mechanism to validate message against HL7 v 2.8.2 standard.
 * The class provides accessors & mutators for the elements mentioned above.
 * The class has validate method which validates Message against HL7 v 2.8.2 standard.
 *
 * <p>User can also create Segment through {@link SegmentFactory}
 */
 public class BLG extends Segment    
{
	

	/**
	 * Constructs the BLG segment.
	 */
	public BLG()
	{super(EnumSegments.HL7_SEG_BLG);
	}

	//Integer values used in getter/setter is unique item id defined 
	//for each attribute.

	/**
	 * Retrieves the Account ID.
	 * This field specify the value for Next of Kin Associated Partys Identifiers.
	 * @return CX - accountID
	 */
	public CX getAccountID() 
	{
		ISingleCollection<IDataType> objDataCollection =getDataType(236);
		if(objDataCollection != null && objDataCollection.size() != 0) 
			return (CX)objDataCollection.elementAt(0);
		else
			return null;
	}

	/**
	 * Retrieves the Charge Type.
	 * @return ID - chargeType
	 */
	public ID getChargeType() 
	{
		ISingleCollection<IDataType> objDataCollection =getDataType(235);
		if(objDataCollection != null && objDataCollection.size() != 0) 
			return (ID)objDataCollection.elementAt(0);
		else
			return null;
	}

	/**
	 * Retrieves the Charge Type Reason.
	 * This field specify the value for field Confidentiality Code. Refer to User Defined Table 0475 - Charge Type Reason for suggested values.  
	 * @return CWE - chargeTypeReason
	 */
	public CWE getChargeTypeReason() 
	{
		ISingleCollection<IDataType> objDataCollection =getDataType(1645);
		if(objDataCollection != null && objDataCollection.size() != 0) 
			return (CWE)objDataCollection.elementAt(0);
		else
			return null;
	}

	/**
	 * Retrieves the When To Charge.
	 * This field specify the value for when to charge for the ordered service. 
	 * @return CCD - whenToCharge
	 */
	public CCD getWhenToCharge() 
	{
		ISingleCollection<IDataType> objDataCollection =getDataType(234);
		if(objDataCollection != null && objDataCollection.size() != 0) 
			return (CCD)objDataCollection.elementAt(0);
		else
			return null;
	}

	/**
	 * Set when to charge for the ordered service. 
	 * @param strInvocationEvent Specifies the code for the event precipitating/triggering the charge activity. Refer to HL7 Table 0100 - Invocation event for valid values.
	 * @param objDatetime Specifies the exact time to charge for the ordered service
	 */
	public void setWhenToCharge(String strInvocationEvent, DTM objDatetime) 
	{
		CCD whenToCharge  = (CCD)getDataElement(234);
		whenToCharge.setInvocationEvent(strInvocationEvent);
		whenToCharge.setDateTime(objDatetime);
		setWhenToCharge(whenToCharge);
	}

	/**
	 * Set when to charge for the ordered service.
	 * @param objwhenToCharge Object of When to Charge
	 */
	public void setWhenToCharge(CCD objwhenToCharge)
	{
		setDataType(234,objwhenToCharge);

	}

	/**
	 * Set the Charge Type.
	 * @param strChargeType This field identifies someone or something other than the patient to be billed for this service.
	 * 						  Refer to HL7 Table 0122 - Charge Type for valid values.
	 */
	public void setChargeType(String strChargeType) 
	{
		ID chargeType  = (ID)getDataElement(235);
		chargeType.setValue(strChargeType);
		setChargeType(chargeType);
	}

	/**
	 * Set the Charge Type.
	 * @param chargeType Object of Charge Type.
	 */
	public void setChargeType(ID chargeType)
	{
		setDataType(235,chargeType);

	}

	/**
	 * Set the Account ID.
	 * This field specify the value for Next of Kin Associated Partys Identifiers.
	 * @param strIDNumber Specifies the value of the identifier itself.
	 * @param strCheckDigit It is part of the identifying number used in the sending application. 
	 * @param strCheckDigitScheme Contains the code identifying the check digit scheme employed.
	 * 								Suggested Values NP,ISO,M10,M11.[Refer table No.0061].
	 * @param objAssigningAuthority The assigning authority is a unique name of the system (or organization or agency 
	 * 									or department) that creates the data. . 
	 * @param strIdentifierTypeCode Specifies a code corresponding to the type of identifier.In some cases, this code may be
	 * 								 used as a qualifier to the "Assigning authority" component.[Refer table No 0203]
	 * @param objAssigningFacility Specifies the place or location identifier where the identifier was first assigned 
	 * 									to the patient. 
	 * @param strEffectiveDate Specifies the first date, if known, on which the address is valid and active.
	 * @param strExpirationDate Specifies the last date, if known, on which the address is valid and active.
	 * @param objAssigningJurisdiction Specifies the geo-political body that assigned the identifier. 
	 * @param objAssigningAgencyOrDepartment Specifies agency or department that assigned the identifier. 
	 * @param strSecurityCheck is used to communicate the version code. 
	 * @param objSecurityCheckSCheme is used to transmit information intended to validate veracity of the supplied identifier
	 */ 
	public void setAccountID (String strIDNumber,String strCheckDigit, String strCheckDigitScheme, HD objAssigningAuthority,
			String strIdentifierTypeCode,HD objAssigningFacility,String strEffectiveDate,String strExpirationDate, 
			CWE objAssigningJurisdiction, CWE objAssigningAgencyOrDepartment, String strSecurityCheck, String strSecurityCheckScheme)
	{
		CX cx  = (CX)getDataElement(236);
		cx.setIdNumber(strIDNumber);
		cx.setCheckDigit(strCheckDigit);
		cx.setCheckDigitScheme(strCheckDigitScheme);
		cx.setAssigningAuthority(objAssigningAuthority);
		cx.setIdentifierTypeCode(strIdentifierTypeCode);
		cx.setAssigningFacility(objAssigningFacility);
		cx.setEffectiveDate(strEffectiveDate);
		cx.setExpirationDate(strExpirationDate);
		cx.setAssigningJurisdiction(objAssigningJurisdiction);
		cx.setAssigningAgencyOrDepartment(objAssigningAgencyOrDepartment); 
		cx.setSecurityCheck(strCheckDigit);
		cx.setSecurityCheckScheme(strSecurityCheckScheme);
		setAccountID(cx);

	} 

	/** 
	 * Set the Account ID.
	 * This field specify the value for Next of Kin Associated Partys Identifiers.
	 * @param objaccountID Object of Account ID.
	 */ 
	public void setAccountID(CX objaccountID)
	{
		setDataType(236,objaccountID);

	}
	/**
	 * Set the Charge Type Reason.
	 * This field specify the value for field Confidentiality Code. Refer to User Defined Table 0475 - Charge Type Reason for suggested values.  
	 * @param strIdentifier Specifies of characters (the code) that uniquely identifies the item being referenced. 
	 * @param strText Specifies The descriptive or textual name of the identifier, e.g., myocardial infarction. 
	 * @param strNameOfCodingSystem Identifies the coding scheme being used in the identifier component.
	 * @param strAlternateIdentifier Specifies an alternate sequence of characters (the code) that uniquely identifies the item being referenced.
	 * @param strAlternateText Specifies the descriptive or textual name of the alternate identifier. See usage note in section introduction.
	 * @param strNameOfAlternateCodingSystem Identifies the coding scheme being used in the alternate identifier component. 
	 * @param strCodingSystemVersionID A sequence of characters that uniquely identifies a second alternate code.
	 * @param strAlternateCodingSystemVersionId This component carries the version for the coding system identified by components 4-6.
	 * @param strOriginalText The text as seen and/or selected by the user who entered the data.
	 * @param strSecondAlternateIdentifier A sequence of characters that uniquely identifies a second alternate code.
	 * @param strSecondAlternateText The descriptive or textual name of the alternate identifier.
	 * @param strNameofSecondAlternateCodingSystem Identifies the coding scheme being used in the alternate identifier component.
	 * @param strSecondAlternateCodingSystemVersionID This component carries the version for the coding system identified by components 10-12.
	 * @param strCodingSystemOID This component contains the ISO Object Identifier (OID) for the coding system or value set named in CWE.3.
	 * @param strValueSetOID This component contains the ISO Object Identifier (OID) to allow identification of the value set from which the value in CWE.1 is obtained.
	 * @param strValueSetVersionID This component carries the version for the value set identified by CWE.15. The version is a date. The date is the date/time that the value set being used was published.
	 * @param strAlternateCodingSystemOID This component contains the ISO Object Identifier (OID) for the coding system or value set named in CWE.6.
	 * @param strAlternateValueSetOID This component contains the ISO Object Identifier (OID) to allow identification of the value set from which the value in CWE.4 is obtained.
	 * @param strAlternateValueSetVersionID This component carries the version for the value set identified by CWE.18. The version is a date.
	 * @param strSecondAlternateCodingSystemOID This component contains the ISO Object Identifier (OID) for the coding system or value set named in CWE.12.
	 * @param strSecondAlternateValueSetOID This component contains the ISO Object Identifier (OID) to allow identification of the value set from which the value in CWE.10 is obtained.
	 * @param strSecondAlternateValueSetVersionID This component carries the version for the value set identified by CWE.21. The version is a date.
	 */	
	public void setChargeTypeReason(String strIdentifier,String strText,String strNameOfCodingSystem,
			String strAlternateIdentifier,String strAlternateText,String strNameOfAlternateCodingSystem, String strCodingSystemVersionID, String 				strAlternateCodingSystemVersionId,
			String strOriginalText, String  strSecondAlternateIdentifier, String strSecondAlternateText, 
			String strNameofSecondAlternateCodingSystem, String strSecondAlternateCodingSystemVersionID, String strCodingSystemOID, String strValueSetOID,
			String strValueSetVersionID, String strAlternateCodingSystemOID, String strAlternateValueSetOID, String strAlternateValueSetVersionID,
			String  strSecondAlternateCodingSystemOID, String strSecondAlternateValueSetOID, String strSecondAlternateValueSetVersionID)
	{
		CWE cwe  = (CWE)getDataElement(1645);
		cwe.setIdentifier(strIdentifier);
		cwe.setText(strText);
		cwe.setNameOfCodingSystem(strNameOfCodingSystem);
		cwe.setAlternateIdentifier(strAlternateIdentifier);
		cwe.setAlternateText(strAlternateText);
		cwe.setNameOfAlternateCodingSystem(strNameOfAlternateCodingSystem); 
		cwe.setCodingSystemVersionID(strCodingSystemVersionID);
		cwe.setAlternateCodingSystemVersionId(strAlternateCodingSystemVersionId);
		cwe.setOriginalText(strOriginalText);
		cwe.setSecondAlternateIdentifier(strSecondAlternateIdentifier);
		cwe.setSecondAlternateText(strSecondAlternateText);
		cwe.setNameofSecondAlternateCodingSystem(strNameofSecondAlternateCodingSystem);
		cwe.setSecondAlternateCodingSystemVersionID(strSecondAlternateCodingSystemVersionID);
		cwe.setCodingSystemOID(strCodingSystemOID);
		cwe.setValueSetOID(strValueSetOID);
		cwe.setValueSetVersionID(strValueSetVersionID);
		cwe.setAlternateCodingSystemOID(strAlternateCodingSystemOID);
		cwe.setAlternateValueSetOID(strAlternateValueSetOID);
		cwe.setAlternateValueSetVersionID(strAlternateValueSetVersionID);
		cwe.setSecondAlternateCodingSystemOID(strSecondAlternateCodingSystemOID);
		cwe.setSecondAlternateValueSetOID(strSecondAlternateValueSetOID);
		cwe.setSecondAlternateValueSetVersionID(strSecondAlternateValueSetVersionID);
		setChargeTypeReason(cwe);

	} 

	/** 
	 * Set the Charge Type Reason.
	 * This field specify the value for field Confidentiality Code. Refer to User Defined Table 0475 - Charge Type Reason for suggested values.  
	 * @param objchargeTypeReason Object of Charge Type Reason.
	 */ 
	public void setChargeTypeReason(CWE objchargeTypeReason)
	{
		setDataType(1645,objchargeTypeReason);

	}

	/** 
	 *  Validates HL7 Message objects .
	 * Validate method checks for validation each and every segments in side the Message object against specified message Structure. 
	 * Validation checks include optionality of segment or group.
	 * It also verifies the number of times segment or group is repeated in message.
	 * e.g 
	 * {PSH} while parsing PSH segment validate method will check for its mandatory presence.
	 * [PSH] in this case validate will throw exception if one tries to enter more than one PSH segment.
	 * @return boolean - true or false.
	 * @throws InvalidSegmentException Thrown when Segment Validation Fails.
	 */
	public boolean validate()throws InvalidSegmentException
	{
		//All fields are optional.
		try
		{
			super.validate();

			return true;
		}
		catch(FieldMissingException ex)
		{
			throw new InvalidSegmentException(enumSegments.getValue(),ex);
		}
	}
}