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

import java.util.Locale;

import cdac.medinfo.sdk.dcm30_04.baselibs.DataSet;
import cdac.medinfo.sdk.dcm30_04.baselibs.ExplicitDE;
import cdac.medinfo.sdk.dcm30_04.baselibs.ImplicitDE;
import cdac.medinfo.sdk.dcm30_04.baselibs.TransferSyntax;
import cdac.medinfo.sdk.dcm30_04.commoninterface.IDataElement;
import cdac.medinfo.sdk.dcm30_04.commoninterface.IDataSet;
import cdac.medinfo.sdk.dcm30_04.commoninterface.IDictionaryReader;
import cdac.medinfo.sdk.dcm30_04.commoninterface.ITag;
import cdac.medinfo.sdk.dcm30_04.dicommodel.print.PrinterConfigurationIOD;
import cdac.medinfo.sdk.dcm30_04.dicommodel.print.PrinterIOD;
import cdac.medinfo.sdk.dcm30_04.enums.EnumCommandField;
import cdac.medinfo.sdk.dcm30_04.enums.EnumQueueStatus;
import cdac.medinfo.sdk.dcm30_04.enums.EnumSOPClassUIDs;
import cdac.medinfo.sdk.dcm30_04.services.dicomnetcommons.EnumImplOpsStatus;
import cdac.medinfo.sdk.dcm30_04.services.dicomnetcommons.ICommandSet;
import cdac.medinfo.sdk.dcm30_04.services.dimsecommons.DicomMessage;
import cdac.medinfo.sdk.dcm30_04.utils.DicomConfig;
import cdac.medinfo.sdk.dcm30_04.utils.DicomConstants;
import cdac.medinfo.sdk.dcm30_04.utils.DicomLogger;
import cdac.medinfo.sdk.dcm30_04.utils.ISingleCollection;
import cdac.medinfo.sdk.dcm30_04.utils.ResourceBundleWrapper;
import cdac.medinfo.sdk.dcm30_04.utils.StandardMethods;

/**
 * This class is for the Configuration of Printer related information.
 * Various paramets related to Printer like Printer Name, its Status, Manufacturer etc.
 * can be set.
 */
public class PrinterConfiguration 
{
	private PrinterIOD objPrinterIOD;
	private EnumQueueStatus enumQueueStatus;
	private PrinterConfigurationIOD objPrinterConfigurationIOD;

	/**
	 * This is default Constructor for PrinterConfiguration.
	 */
	public PrinterConfiguration() {
		super();
	}
	
	/**
	 * This function sets the Printer IOD populated with all attributes.
	 * @param objPrinterIOD instance of Printer IOD>
	 */
	public void setPrintIOD(PrinterIOD objPrinterIOD)
	{
		this.objPrinterIOD = objPrinterIOD;
	}

	/**
	 * This function gets the Printer IOD.
	 * @return PrinterIOD - instance of Printer IOD.
	 */
	public PrinterIOD getPrinterIOD()
	{
		return objPrinterIOD;
	}

	/**
	 * This function sets the PrinterConfigurationIOD.
	 * @param objPrinterConfigurationIOD instance of Printer Configuration IOD.
	 */
	public void setPrinterConfigurationInfo(PrinterConfigurationIOD objPrinterConfigurationIOD)
	{
		this.objPrinterConfigurationIOD = objPrinterConfigurationIOD;
	}

	/**
	 * This function gets the PrinterConfigurationIOD.
	 * @return objPrinterConfigurationIOD instance of Printer Configuration.
	 */
	public PrinterConfigurationIOD getPrinterConfigurationInfo()
	{
		return objPrinterConfigurationIOD;
	}


	/**
	 * This function sets the Queue Status. It can be Normal , Full etc.
	 * @param enumQueueStatus Enum value of Queue Status.
	 */
	public void setQueueStatus(EnumQueueStatus enumQueueStatus)
	{
		this.enumQueueStatus = enumQueueStatus;
	}

	/**
	 * This function gets the Queue Status.
	 * @return EnumQueueStatus - Gets the enum value of Queue Status.
	 */
	public EnumQueueStatus getQueueStatus()
	{
		return enumQueueStatus;
	}

	/**
	 * This function retrieves the PrinterIOD or PrinterConfigurationRetrieval IOD.
	 * @param objDicomMessage instance of Dicom Message.
	 * @param objRstDatasetList instance of DataSet Collection.
	 * @return short - Status of retrieve operation.
	 */
	@SuppressWarnings("unchecked")
	short retrieve(DicomMessage objDicomMessage, ISingleCollection<IDataSet> objRstDatasetList)
	{
		EnumCommandField enumRequestType = objDicomMessage.getCommandType();
		String strSOPClassUID = null;
		ICommandSet objCommandSet = objDicomMessage.getCommandSet();
		IDataSet objRstDataSet = new DataSet();
		switch(enumRequestType)
		{
		case N_GET_RQ :
		{
			ISingleCollection<ITag> objTagCollection = null;
			Object objValue =  objCommandSet.getValue(0x0000,0x1005);
			strSOPClassUID = (String)objCommandSet.getValue(0x0000,0x0003);
			if(objValue != null)
			{
				objTagCollection = (ISingleCollection<ITag>)objValue;
			}
			EnumSOPClassUIDs enumSOPClassUIDs = StandardMethods.getSOPClassUID(strSOPClassUID);
			if(enumSOPClassUIDs.equals(EnumSOPClassUIDs.PRINTERIOD))
			{
				if(objPrinterIOD != null)
				{
					IDataSet objIDataSetPrinterIOD = objPrinterIOD.getDataSet();
					if(objTagCollection != null)
					{
						for(ITag objITag : objTagCollection)
						{
							addDataElement(objITag.getGroupNumber(),objITag.getElementNumber(),objRstDataSet,objIDataSetPrinterIOD);
						}
						objRstDatasetList.add(objRstDataSet);
					}
					else
					{
						addDataElement(objRstDataSet,objIDataSetPrinterIOD);
						objRstDatasetList.add(objRstDataSet);
					}
					return EnumImplOpsStatus.SUCCESS.getValue();
				}
				else
				{
					if(DicomConfig.createInstance().isLoggingOn())
					{
						DicomLogger objDicomLogger =DicomLogger.getLogger();
						objDicomLogger.log(ResourceBundleWrapper.getValue("exceptions","Print.PrinterConfigurationNotSet", new Locale(DicomConfig.createInstance().getLocale())));
					}
					return EnumImplOpsStatus.FAILURE.getValue();
				}
			}
			else if(enumSOPClassUIDs.equals(EnumSOPClassUIDs.PRINTERCONFIGURATIONRETRIEVAL))
			{
				if(objPrinterConfigurationIOD != null)
				{
					IDataSet objIDataSetobjPrinterConfigurationIOD = objPrinterConfigurationIOD.getDataSet();
					if(objTagCollection != null)
					{
						for(ITag objITag : objTagCollection)
						{
							addDataElement(objITag.getGroupNumber(),objITag.getElementNumber(),objRstDataSet,objIDataSetobjPrinterConfigurationIOD);
						}
						objRstDatasetList.add(objRstDataSet);
					}
					else
					{
						addDataElement(objRstDataSet,objIDataSetobjPrinterConfigurationIOD);
						objRstDatasetList.add(objRstDataSet);
					}
					return EnumImplOpsStatus.SUCCESS.getValue();
				}
				else
				{
					if(DicomConfig.createInstance().isLoggingOn())
					{
						DicomLogger objDicomLogger =DicomLogger.getLogger();
						objDicomLogger.log(ResourceBundleWrapper.getValue("exceptions","Print.PrinterConfigurationNotSet", new Locale(DicomConfig.createInstance().getLocale())));
					}
					return EnumImplOpsStatus.FAILURE.getValue();
				}
			}
		}
		default :
		{
			if(DicomConfig.createInstance().isLoggingOn())
			{
				DicomLogger objDicomLogger =DicomLogger.getLogger();
				objDicomLogger.log(ResourceBundleWrapper.getValue("exceptions","ServiceRequestNotSupported", new Locale(DicomConfig.createInstance().getLocale())));
			}
			return EnumImplOpsStatus.FAILURE.getValue();
		}
		}
	}

	/*
	 * This function adds the DataElement to the resultant DataSet.
	 */
	private void addDataElement(IDataSet objRstDataSet, IDataSet objIDataSetPrinterIOD)
	{
		if(objRstDataSet.getTransferSyntax().getTransferSyntaxUID().equals(DicomConstants.TS_EXPLICIT_VR_LITTLE_ENDIAN) || 
				objRstDataSet.getTransferSyntax().getTransferSyntaxUID().equals(DicomConstants.TS_EXPLICIT_VR_BIG_ENDIAN))
		{
			for(IDataElement objIDataElement : objIDataSetPrinterIOD)
			{
				IDataElement objTempDataElement =getExplicitDE(objIDataElement,objRstDataSet.getDictionaryReader());
				objRstDataSet.addDataElement(objTempDataElement);
			}
		}
		else
		{
			for(IDataElement objIDataElement : objIDataSetPrinterIOD)
			{
				IDataElement objTempDataElement =getImplicitDE(objIDataElement,objRstDataSet.getDictionaryReader());
				objRstDataSet.addDataElement(objTempDataElement);
			}
		}
	}

	/*
	 * This function adds the DataElement to the resultant DataSet.
	 */
	private void addDataElement(int GroupNo, int ElementNo, IDataSet objRstDataSet, IDataSet  objIDataSetPrinterIOD)
	{

		if(objRstDataSet.getTransferSyntax().getTransferSyntaxUID().equals(DicomConstants.TS_EXPLICIT_VR_LITTLE_ENDIAN) || 
				objRstDataSet.getTransferSyntax().getTransferSyntaxUID().equals(DicomConstants.TS_EXPLICIT_VR_BIG_ENDIAN))
		{
			for(IDataElement objIDataElement : objIDataSetPrinterIOD)
			{
				if(objIDataElement.getTag().getGroupNumber() == GroupNo && objIDataElement.getTag().getElementNumber() == ElementNo)
				{

					IDataElement objTempDataElement =getExplicitDE(objIDataElement,objRstDataSet.getDictionaryReader());
					objRstDataSet.addDataElement(objTempDataElement);
				}
			}
		}
		else
		{

			for(IDataElement objIDataElement : objIDataSetPrinterIOD)
			{
				if(objIDataElement.getTag().getGroupNumber() == GroupNo && objIDataElement.getTag().getElementNumber() == ElementNo)
				{
					IDataElement objTempDataElement =getImplicitDE(objIDataElement,objRstDataSet.getDictionaryReader());
					objRstDataSet.addDataElement(objTempDataElement);
				}
			}
		}
	}

	private IDataElement getImplicitDE(IDataElement objIDataElement, IDictionaryReader objIDictionaryReader)
	{
		IDataElement objTempDataElement = new ImplicitDE(objIDataElement.getTag().getGroupNumber(), objIDataElement.getTag().getElementNumber(),objIDataElement.getValue(), new TransferSyntax(),objIDictionaryReader);
		return objTempDataElement;
	}

	private IDataElement getExplicitDE(IDataElement objIDataElement,IDictionaryReader objIDictionaryReader)
	{
		String strVR = objIDictionaryReader.getVR(objIDataElement.getTag().getGroupNumber(), objIDataElement.getTag().getElementNumber());
		int iVR = StandardMethods.getiVRtype(strVR);
		IDataElement objTempDataElement = new ExplicitDE(objIDataElement.getTag().getGroupNumber(), objIDataElement.getTag().getElementNumber(),iVR,objIDataElement.getValue());
		return objTempDataElement;
	}
}
