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

import java.io.ByteArrayOutputStream;
import java.io.IOException;

import cdac.medinfo.sdk.dcm30_04.dicomstreams.DicomBufferStream;
import cdac.medinfo.sdk.dcm30_04.services.dicomnetcommons.ConnectionClosedException;
import cdac.medinfo.sdk.dcm30_04.services.dicomnetcommons.EnumErrorCode;
import cdac.medinfo.sdk.dcm30_04.services.dicomnetcommons.IConnectionListener;
import cdac.medinfo.sdk.dcm30_04.services.dicomnetcommons.INetworkAgent;
import cdac.medinfo.sdk.dcm30_04.utils.DicomConfig;
import cdac.medinfo.sdk.dcm30_04.utils.DicomLogger;




/**
 * This class contains method to send the data to the remote entity.
 */
public class PDUWriter 
{

	PDU objPDU = null;

	boolean stop;

//	BlockingQueue<PDU> objPDUQueue;

	/** The obj network agent. */
	private INetworkAgent objINetworkAgent;

	private IConnectionListener objIConnectionListener;

	/** The int write Max Length */
	int iWriteMaxLength;

	/**
	 * Instantiates new PDU Writer.
	 * @param objINetworkAgent
	 */
	public PDUWriter(INetworkAgent objINetworkAgent)
	{
		this.objINetworkAgent = objINetworkAgent;
	}



	/**
	 * This function sets the maximum length that can be written to the remote entity.
	 * @param maxLength int value of maximum length written to the remote entity.
	 */
	public void setWriteMaxLength(int maxLength)
	{
		iWriteMaxLength = maxLength;
	}

	/**
	 *  This function gets the maximum length that can be written to the remote entity.
	 * @return int - int value of Maximum length that can be written.
	 */
	public int getWriteMaxLength() 
	{
		return iWriteMaxLength;
	}


	/**
	 * Adds the connection listener. 
	 * The entity that registers itself as an Connection listener is notified 
	 * if connection with the remote entity is disconnected. 
	 * @param objIConnectionListener  the object of connection listener
	 */
	public void addConnectionListener(IConnectionListener objIConnectionListener)
	{
		this.objIConnectionListener = objIConnectionListener;
	}

	/**
	 * Send data to the remote entity.
	 * @param objPDU the object of PDU to be sent to remote entity.
	 */
	public boolean sendData(PDU objPDU)
	{
		try{
//			System.out.println("PDU Type" + objPDU.getPDUType());
//			System.out.println("PDUWriter sent data to network...");
			DicomBufferStream objDicomBufferStream = new DicomBufferStream();
			ByteArrayOutputStream objByteArrayOutputStream = new ByteArrayOutputStream();
			objDicomBufferStream.setOutputStream(objByteArrayOutputStream);
			objPDU.serialize(objDicomBufferStream);
			byte[] arrTempByte = objDicomBufferStream.toByteArray();
			if(objINetworkAgent != null)
			{
				objINetworkAgent.write(arrTempByte);
				return true;
			}
			
		}
		catch(IOException ioe)
		{
			if(DicomConfig.createInstance().isLoggingOn())
			{
				DicomLogger objDicomLogger = DicomLogger.getLogger();
				objDicomLogger.log(ioe.getMessage());
			}
			stop = true;
			objIConnectionListener.onException(new ConnectionClosedException(ioe),EnumErrorCode.REMOTE_CONNECTION_CLOSED);
			return false;
		}
		return false;
	}

	/**
	 * The Class DummyPDU.
	 */
	class DummyPDU extends PDU
	{
		/**
		 * Instantiates new Dummy PDU.
		 */
		public DummyPDU()
		{

		}

		public int getLength()
		{
			return 0;
		}
	}

	/**
	 * This function closes the PDU Writer.
	 */
	public  void close()
	{
		stop = true;
		//objIConnectionListener = null;
		objINetworkAgent = null;
	}
}
