/*******************************************************************************
 *  Copyright 2017-2018 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.dcm30_04.services.association;

import cdac.medinfo.sdk.dcm30_04.services.dicomnetcommons.EnumSCUSCPRole;
import cdac.medinfo.sdk.dcm30_04.services.pdu.AsyncOpsWindowItem;
import cdac.medinfo.sdk.dcm30_04.services.pdu.ImplClassUIDItem;
import cdac.medinfo.sdk.dcm30_04.services.pdu.ImplVersionNameItem;
import cdac.medinfo.sdk.dcm30_04.services.pdu.MaxLengthItem;
import cdac.medinfo.sdk.dcm30_04.services.pdu.SCUSCPRoleSelectionItem;
import cdac.medinfo.sdk.dcm30_04.services.pdu.SOPClassExtNegotiationItem;
import cdac.medinfo.sdk.dcm30_04.services.pdu.ServiceClassApplicationInformation;
import cdac.medinfo.sdk.dcm30_04.services.pdu.UserInfoItem;
import cdac.medinfo.sdk.dcm30_04.utils.ISingleCollection;
import cdac.medinfo.sdk.dcm30_04.utils.SingleCollection;

/**
 * This class provides DICOM Application Entity user information.
 * implements getters and setters required in UserInfo for Association.
 */
public class UserInfo  
{

	/** The maxlength. */
	int imaxlength = -1;

	/** The implementation class uid. */
	String strImplclassUID;	

	/** The implementation version name. */
	String strImplVersionName;

	/** The maximum number of operations performed. */
	short shMaxNumOpsPerformed = 1;

	/** The maximum number of operations invoked. */
	short shMaxNumOpsInvoked = 1;

	/** The enum representing SCU-SCP role. */
	EnumSCUSCPRole enumSCUSCPRole;

	/** SOP SCU SCP Role Selection class UID. */
	String strSCUSCPRoleSOPClassUID ;

	ISingleCollection<SOPClassExtendedNegotiationSubItemFields> objSOPClassExtendedNegotiationSubItemFieldsCollection = null;


	/**
	 * Adds SOP CLASS EXTENDED NEGOTIATION SUB-ITEM FIELDS to User Information.
	 * @param instance of SOP CLASS EXTENDED NEGOTIATION SUB-ITEM FIELDS.
	 */
	public void addSOPClassExtendedNegotiationSubItemField(SOPClassExtendedNegotiationSubItemFields objExtendedNegotiationSubItemFields)
	{
		if(null == objSOPClassExtendedNegotiationSubItemFieldsCollection)
			objSOPClassExtendedNegotiationSubItemFieldsCollection = new SingleCollection<SOPClassExtendedNegotiationSubItemFields>();
		objSOPClassExtendedNegotiationSubItemFieldsCollection.add(objExtendedNegotiationSubItemFields);
	}	
	

	public void setSOPClassExtendedNegotiationSubItemFields(ISingleCollection<SOPClassExtendedNegotiationSubItemFields> objCollection)
	{
		this.objSOPClassExtendedNegotiationSubItemFieldsCollection = objCollection;
	}	

	
	/**
	 * Gets Collection of SOP CLASS EXTENDED NEGOTIATION SUB-ITEM FIELDS from User Information.
	 * @return Collection of SOP CLASS EXTENDED NEGOTIATION SUB-ITEM FIELDS.
	 */
	public ISingleCollection<SOPClassExtendedNegotiationSubItemFields> getSOPClassExtendedNegotiationSubItemFields()
	{
		return objSOPClassExtendedNegotiationSubItemFieldsCollection;
	}


	/**
	 * Retrieves the SCU-SCP role.
	 * @return EnumSCUSCPRole - the SCUSCP role
	 */
	public EnumSCUSCPRole getSCUSCPRole() 
	{
		return enumSCUSCPRole;
	}

	/**
	 * Sets the SCU-SCP role.
	 * @param enumSCUSCPRole Enum value of SCUSCP role
	 *            
	 */
	public void setSCUSCPRole(EnumSCUSCPRole enumSCUSCPRole) 
	{
		this.enumSCUSCPRole = enumSCUSCPRole;
	}

	/**
	 * Retrieves the supported maximum length.
	 * @return int - value of maximum length
	 */
	public int getmaxlength() 
	{
		return imaxlength;
	}

	/**
	 * Sets the supported maximum length.
	 * @param imaxlength  int value of supported maximum length.
	 */
	public void setmaxlength(int imaxlength) 
	{
		this.imaxlength = imaxlength;
	}

	/**
	 * Retrieves the maximum number of operations invoked.
	 * @return short - value of maximum number of operations invoked.
	 */
	public short getMaxNumOpsInvoked() 
	{
		return shMaxNumOpsInvoked;
	}

	/**
	 * Sets the maximum number of operations invoked.
	 * @param shMaxNumOpsInvoked short value of maximum number of operations invoked.
	 */
	public void setMaxNumOpsInvoked(short shMaxNumOpsInvoked) 
	{
		this.shMaxNumOpsInvoked = shMaxNumOpsInvoked;
	}

	/**
	 * Retrieves the maximum number of operations performed.
	 * @return short - value of maximum number of operations performed
	 */
	public short getMaxNumOpsPerformed()
	{
		return shMaxNumOpsPerformed;
	}

	/**
	 * Sets the maximum number of operations performed.
	 * @param shMaxNumOpsPerformed short value of maximum number of operations
	 */
	public void setMaxNumOpsPerformed(short shMaxNumOpsPerformed) 
	{
		this.shMaxNumOpsPerformed = shMaxNumOpsPerformed;
	}

	/**
	 * Retrieves the Implementation Class UID
	 * @return String - value of Implementation Class UID
	 */
	public String getImplclassUID()
	{
		return strImplclassUID;
	}

	/**
	 * Sets the Implementation Class UID
	 * @param strImplclassUID String value of Implementation Class UID
	 */
	public void setImplclassUID(String strImplclassUID) 
	{
		this.strImplclassUID = strImplclassUID;
	}

	/**
	 * Retrieves the Implementation version name.
	 * @return String - value of  Implementation version name
	 */
	public String getImplVersionName() 
	{
		return strImplVersionName;
	}

	/**
	 * Sets the Implementation version name.
	 * @param strImplVersionName String value of Implementation version name
	 */
	public void setImplVersionName(String strImplVersionName) 
	{
		this.strImplVersionName = strImplVersionName;
	}

	/**
	 * Retrieves the SOP Class UID.
	 * @return String - value of SOP Class UID.
	 */
	public String getSCUSCPRoleSelectionSOPclassuid() 
	{
		return strSCUSCPRoleSOPClassUID;
	}

	/**
	 * Sets the the SOP Class UID.
	 * @param strSOPclassuid String value of SOP Class UID.
	 */
	public void setSCUSCPRoleSelectionSOPclassuid(String strSOPclassuid) 
	{
		this.strSCUSCPRoleSOPClassUID = strSOPclassuid;
	}


	/**
	 * Sets the Communication mode parameters according to Synchronous mode.
	 */
	public void setSynchronousMode()
	{
		this.shMaxNumOpsInvoked = 1;
		this.shMaxNumOpsPerformed = 1;
	}

	/**
	 * Sets the Communication mode parameters according to Fully Asynchronous mode.
	 */
	public void setInfiniteOpsAsynchronousMode()
	{
		this.shMaxNumOpsInvoked = 0;
		this.shMaxNumOpsPerformed = 0;
	}

	/**
	 * Initializes the UserInfo according to the given UserInfoItem
	 * @param objUserInfoItem instance of UserInfoItem
	 */
	public void populate(UserInfoItem objUserInfoItem)
	{
		if(objUserInfoItem != null)
		{
			MaxLengthItem objMaxLengthItem = objUserInfoItem.getMaxLengthItem();
			if(objMaxLengthItem != null)
				this.imaxlength = objMaxLengthItem.getMaxLength();

			ImplClassUIDItem objImplClassUIDItem = objUserInfoItem.getImplClassUIDItem();
			if(objImplClassUIDItem != null)
				this.strImplclassUID = objImplClassUIDItem.getImplClassUID();

			ImplVersionNameItem objImplVersionNameItem = objUserInfoItem.getImplVersionNameItem();
			if(objImplVersionNameItem != null)
				this.strImplVersionName = objImplVersionNameItem.getImplVersionName();

			AsyncOpsWindowItem objAsyncOpsWindowItem = objUserInfoItem.getAsyncOpsWindowItem();
			if(objAsyncOpsWindowItem != null)
			{
				this.shMaxNumOpsInvoked = objAsyncOpsWindowItem.getMaxNumOpsInvoked();
				this.shMaxNumOpsPerformed = objAsyncOpsWindowItem.getMaxNumOpsPerformed();
			}

			SCUSCPRoleSelectionItem objSCUSCPRoleSelectionItem = objUserInfoItem.getSCUSCPRoleSelectionItem();
			if(objSCUSCPRoleSelectionItem != null)
			{
				this.enumSCUSCPRole = objSCUSCPRoleSelectionItem.getRole();
				this.strSCUSCPRoleSOPClassUID = objSCUSCPRoleSelectionItem.getSOPclassuid();
			}

			ISingleCollection<SOPClassExtNegotiationItem> objSOPClassExtNegotiationItem =  objUserInfoItem.getSOPClassExtNegotiationItems();
			if(objSOPClassExtNegotiationItem != null)
			{				
				objSOPClassExtendedNegotiationSubItemFieldsCollection = new SingleCollection<SOPClassExtendedNegotiationSubItemFields>();
				
				for (SOPClassExtNegotiationItem item : objSOPClassExtNegotiationItem) 
				{
					SOPClassExtendedNegotiationSubItemFields temp = new SOPClassExtendedNegotiationSubItemFields();
					temp.setElementCoercion(item.getServiceAppInfo().getElementCoercion());
					temp.setLevelofDigitalSignatureSupport(item.getServiceAppInfo().getLevelofDigitalSignatureSupport());
					temp.setLevelofSupport(item.getServiceAppInfo().getLevelofSupport());
					temp.setSOPClassExtNegotiationSOPclassuid(item.getSOPClassUID());
					
					objSOPClassExtendedNegotiationSubItemFieldsCollection.add(temp);
				}
			}

		}
	}

	/**
	 * Initializes the UserInfo.
	 * @return UserInfoItem - instance of UserInfoItem
	 */
	public UserInfoItem getItem()
	{
		UserInfoItem objUserInfoItem = new UserInfoItem();

		if(this.getmaxlength() != 0)
		{
			MaxLengthItem objMaxLengthItem = new MaxLengthItem();
			objMaxLengthItem.setMaxLength(this.getmaxlength());
			objUserInfoItem.setMaxLengthItem(objMaxLengthItem);
		}

		if(this.getImplclassUID() != null)
		{
			ImplClassUIDItem objImplClassUIDItem = new ImplClassUIDItem();
			objImplClassUIDItem.setImplClassUID(this.getImplclassUID());
			objUserInfoItem.setImplClassUIDItem(objImplClassUIDItem);
		}

		if(this.getImplclassUID() != null)
		{
			ImplVersionNameItem objImplVersionNameItem = new ImplVersionNameItem();
			objImplVersionNameItem.setImplVersionName(this.getImplVersionName());
			objUserInfoItem.setImplVersionNameItem(objImplVersionNameItem);
		}

		AsyncOpsWindowItem objAsyncOpsWindowItem = new AsyncOpsWindowItem();
		objAsyncOpsWindowItem.setMaxNumOpsInvoked(this.getMaxNumOpsInvoked());
		objAsyncOpsWindowItem.setMaxNumOpsPerformed(this.getMaxNumOpsPerformed());
		objUserInfoItem.setAsyncOpsWindowItem(objAsyncOpsWindowItem);

		if(this.getSCUSCPRole() != null || this.getSCUSCPRoleSelectionSOPclassuid() != null)
		{
			SCUSCPRoleSelectionItem objSCUSCPRoleSelectionItem = new SCUSCPRoleSelectionItem();
			objSCUSCPRoleSelectionItem.setRole(this.getSCUSCPRole());
			objSCUSCPRoleSelectionItem.setSOPclassuid(this.getSCUSCPRoleSelectionSOPclassuid());
			if(this.getSCUSCPRoleSelectionSOPclassuid() != null)
				objSCUSCPRoleSelectionItem.setUIDlength((short)this.getSCUSCPRoleSelectionSOPclassuid().length());
			objUserInfoItem.setSCUSCPRoleSelectionItem(objSCUSCPRoleSelectionItem);
		}

		if(this.getSOPClassExtendedNegotiationSubItemFields() != null)
		{
					

			for (SOPClassExtendedNegotiationSubItemFields  element : objSOPClassExtendedNegotiationSubItemFieldsCollection) 
			{
				SOPClassExtNegotiationItem objSOPClassExtNegotiationItem = new SOPClassExtNegotiationItem();
				
				objSOPClassExtNegotiationItem.setSOPClassUID(element.getSOPClassExtNegotiationSOPclassuid());				
				
				ServiceClassApplicationInformation objServiceAppInfo = new ServiceClassApplicationInformation();
				objServiceAppInfo.setElementCoercion(element.getElementCoercion());
				objServiceAppInfo.setLevelofDigitalSignatureSupport(element.getLevelofDigitalSignatureSupport());
				objServiceAppInfo.setLevelofSupport(element.getLevelofSupport());
				
				objSOPClassExtNegotiationItem.setServiceAppInfo(objServiceAppInfo);
				
				objUserInfoItem.addSOPClassExtNegotiationItem(objSOPClassExtNegotiationItem);
			}
		}

		return objUserInfoItem;
	}
	
	/**
	 * Validates the RQUserInfo for mandatory attributes
	 * @return boolean - shows status as true or false
	 */
	public boolean validate()
	{
		return true;
	}
}