/* ----------------------------------------------------------------------------
 * This file was automatically generated by SWIG (http://www.swig.org).
 * Version 1.3.35
 *
 * Do not make changes to this file unless you know what you are doing--modify
 * the SWIG interface file instead.
 * ----------------------------------------------------------------------------- */

namespace be.portugal.eid {

using System;
using System.Runtime.InteropServices;

/// <summary>
/// This class represent a reader.
/// You get reader object from the ReaderSet 
///     either by its index (getReaderByNum) or by its name (getReaderByName).
/// Once you have a reader object, you can check if a card is present (isCardPresent).
/// Then you can ask which type of card is in the reader with getCardType()
///		and then get a card object using one of this method :
///			getCard, getEIDCard, getKidsCard, getForeignerCard or getSISCard.
/// </summary>
public class PTEID_ReaderContext : PTEID_Object
    {
  private HandleRef swigCPtr;

  internal PTEID_ReaderContext(IntPtr cPtr, bool cMemoryOwn) : base(pteidlib_dotNetPINVOKE.PTEID_ReaderContextUpcast(cPtr), cMemoryOwn) {
    swigCPtr = new HandleRef(this, cPtr);
  }

  internal static HandleRef getCPtr(PTEID_ReaderContext obj) {
    return (obj == null) ? new HandleRef(null, IntPtr.Zero) : obj.swigCPtr;
  }

  ~PTEID_ReaderContext() {
    Dispose();
  }

  public override void Dispose() {
    lock(this) {
      if(swigCPtr.Handle != IntPtr.Zero && swigCMemOwn) {
        swigCMemOwn = false;
        pteidlib_dotNetPINVOKE.delete_PTEID_ReaderContext(swigCPtr);
      }
      swigCPtr = new HandleRef(null, IntPtr.Zero);
      GC.SuppressFinalize(this);
      base.Dispose();
    }
  }

  /// <summary>
  /// Construct using a fileType and fileName.
  /// </summary>
  /// <remarks>
  /// No physical reader are connected.
  /// </remarks>
  public PTEID_ReaderContext(PTEID_FileType fileType, string fileName)
      : this(pteidlib_dotNetPINVOKE.new_PTEID_ReaderContext__SWIG_0((int)fileType, fileName), true)
  {
    if (pteidlib_dotNetPINVOKE.SWIGPendingException.Pending) throw pteidlib_dotNetPINVOKE.SWIGPendingException.Retrieve();
  }

  /// <summary>
  /// Construct using a fileType and its content (for compatibility with SetRawFile).
  /// </summary>
  /// <remarks>
  /// No physical reader are connected.
  /// </remarks>
  public PTEID_ReaderContext(PTEID_FileType fileType, PTEID_ByteArray data)
      : this(pteidlib_dotNetPINVOKE.new_PTEID_ReaderContext__SWIG_1((int)fileType, PTEID_ByteArray.getCPtr(data)), true)
  {
    if (pteidlib_dotNetPINVOKE.SWIGPendingException.Pending) throw pteidlib_dotNetPINVOKE.SWIGPendingException.Retrieve();
  }

  /// <summary>
  /// Construct using Raw data for Eid.
  /// </summary>
  /// <remarks>
  /// No physical reader are connected.
  /// </remarks>
  public PTEID_ReaderContext(PTEID_RawData_Eid data)
      : this(pteidlib_dotNetPINVOKE.new_PTEID_ReaderContext__SWIG_2(PTEID_RawData_Eid.getCPtr(data)), true)
  {
    if (pteidlib_dotNetPINVOKE.SWIGPendingException.Pending) throw pteidlib_dotNetPINVOKE.SWIGPendingException.Retrieve();
  }

  /// <summary>
  /// Construct using Raw data for Sis.
  /// </summary>
  /// <remarks>
  /// No physical reader are connected.
  /// </remarks>
  public PTEID_ReaderContext(PTEID_RawData_Sis data)
      : this(pteidlib_dotNetPINVOKE.new_PTEID_ReaderContext__SWIG_3(PTEID_RawData_Sis.getCPtr(data)), true)
  {
    if (pteidlib_dotNetPINVOKE.SWIGPendingException.Pending) throw pteidlib_dotNetPINVOKE.SWIGPendingException.Retrieve();
  }

  /// <summary>Return the name of the reader.</summary>
  public string getName() {
    string ret = pteidlib_dotNetPINVOKE.PTEID_ReaderContext_getName(swigCPtr);
    if (pteidlib_dotNetPINVOKE.SWIGPendingException.Pending) throw pteidlib_dotNetPINVOKE.SWIGPendingException.Retrieve();
    return ret;
  }

  /// <summary>Return true if a card is present and false otherwise.</summary>
  public bool isCardPresent() {
    bool ret = pteidlib_dotNetPINVOKE.PTEID_ReaderContext_isCardPresent(swigCPtr);
    if (pteidlib_dotNetPINVOKE.SWIGPendingException.Pending) throw pteidlib_dotNetPINVOKE.SWIGPendingException.Retrieve();
    return ret;
  }

  /// <summary>Release the card.</summary>
  /// <param name="bAllReference">
  /// If true all the invalid reference/pointer are destroyed.
  /// PUT THIS PARAMETER TO TRUE IS THREAD UNSAFE. 
  /// You have to be sure that you will not use any old reference/pointer after this release
  /// </param>
  public void releaseCard(bool bAllReference)
  {
    pteidlib_dotNetPINVOKE.PTEID_ReaderContext_releaseCard__SWIG_0(swigCPtr, bAllReference);
    if (pteidlib_dotNetPINVOKE.SWIGPendingException.Pending) throw pteidlib_dotNetPINVOKE.SWIGPendingException.Retrieve();
  }

  /// <summary>Release the card.</summary>
  public void releaseCard()
  {
    pteidlib_dotNetPINVOKE.PTEID_ReaderContext_releaseCard__SWIG_1(swigCPtr);
    if (pteidlib_dotNetPINVOKE.SWIGPendingException.Pending) throw pteidlib_dotNetPINVOKE.SWIGPendingException.Retrieve();
  }

  /// <summary>Return true if a card has changed since the last called (with the same ulOldId parameter).</summary>
  public bool isCardChanged(ref uint ulOldId) {
    bool ret = pteidlib_dotNetPINVOKE.PTEID_ReaderContext_isCardChanged(swigCPtr, ref ulOldId);
    if (pteidlib_dotNetPINVOKE.SWIGPendingException.Pending) throw pteidlib_dotNetPINVOKE.SWIGPendingException.Retrieve();
    return ret;
  }

  /// <summary> 
  /// Return the type of the card in the reader.
  /// Throw PTEID_ExNoCardPresent exception if no card is present. 
  /// </summary>
  public PTEID_CardType getCardType()
  {
    PTEID_CardType ret = (PTEID_CardType)pteidlib_dotNetPINVOKE.PTEID_ReaderContext_getCardType(swigCPtr);
    if (pteidlib_dotNetPINVOKE.SWIGPendingException.Pending) throw pteidlib_dotNetPINVOKE.SWIGPendingException.Retrieve();
    return ret;
  }

  /// <summary> 
  /// Get the card in the reader.
  /// Instantiation is made regarding the type of the card 
  ///			(PTEID_EIDCard, PTEID_KidsCard, PTEID_ForeignerCard or PTEID_SISCard).
  /// </summary>
  /// <remarks>
  /// If no card is present in the reader, exception PTEID_ExNoCardPresent is thrown.
  /// If the card type is not supported, exception PTEID_ExCardTypeUnknown is thrown.
  /// </remarks>
  public PTEID_Card getCard()
  { 
	PTEID_CardType cardType=getCardType();

	switch(cardType)
	{
	case PTEID_CardType.PTEID_CARDTYPE_EID:			
	case PTEID_CardType.PTEID_CARDTYPE_KIDS:
	case PTEID_CardType.PTEID_CARDTYPE_FOREIGNER:
		return getEIDCard();			

	case PTEID_CardType.PTEID_CARDTYPE_SIS:			
		return getSISCard();			

    default:										
		throw new PTEID_ExCardTypeUnknown();
	}
}

	/// <summary> 
	/// Get the EIDcard in the reader. 
	///		Instantiation is made regarding the type of the card 
	///			(PTEID_EIDCard, PTEID_KidsCard, PTEID_ForeignerCard).
	/// </summary>
    /// <remarks>
	/// If no card is present in the reader, exception PTEID_ExNoCardPresent is thrown.
	/// If the card is not an EIDcard, exception PTEID_ExCardBadType is thrown.
    /// </remarks>
  public PTEID_EIDCard getEIDCard() { 
	PTEID_CardType cardType=getCardType();

	switch(cardType)
	{
	case PTEID_CardType.PTEID_CARDTYPE_EID:			
		PTEID_EIDCard ret = new PTEID_EIDCard(pteidlib_dotNetPINVOKE.PTEID_ReaderContext_getEIDCard(swigCPtr), false);	
		if (pteidlib_dotNetPINVOKE.SWIGPendingException.Pending) throw pteidlib_dotNetPINVOKE.SWIGPendingException.Retrieve();
		return ret;

	case PTEID_CardType.PTEID_CARDTYPE_KIDS:			
		return getKidsCard();		

	case PTEID_CardType.PTEID_CARDTYPE_FOREIGNER:		
		return getForeignerCard();	

    default:										
		throw new PTEID_ExCardBadType();
	}
}

  /// <summary> 
  /// Get the KidsCard in the reader.
  /// </summary>
  /// <remarks>
  /// If no card is present in the reader, exception PTEID_ExNoCardPresent is thrown.
  /// If the card is not a KidsCard, exception PTEID_ExCardBadType is thrown.
  /// </remarks>
  public PTEID_KidsCard getKidsCard() {
    PTEID_KidsCard ret = new PTEID_KidsCard(pteidlib_dotNetPINVOKE.PTEID_ReaderContext_getKidsCard(swigCPtr), false);
    if (pteidlib_dotNetPINVOKE.SWIGPendingException.Pending) throw pteidlib_dotNetPINVOKE.SWIGPendingException.Retrieve();
    return ret;
  }

  /// <summary> 
  /// Get the ForeignerCard in the reader.
  /// </summary>
  /// <remarks>
  /// If no card is present in the reader, exception PTEID_ExNoCardPresent is thrown.
  /// If the card is not a ForeignerCard, exception PTEID_ExCardBadType is thrown.
  /// </remarks>
  public PTEID_ForeignerCard getForeignerCard()
  {
    PTEID_ForeignerCard ret = new PTEID_ForeignerCard(pteidlib_dotNetPINVOKE.PTEID_ReaderContext_getForeignerCard(swigCPtr), false);
    if (pteidlib_dotNetPINVOKE.SWIGPendingException.Pending) throw pteidlib_dotNetPINVOKE.SWIGPendingException.Retrieve();
    return ret;
  }

  /// <summary> 
  /// Get the SISCard in the reader.
  /// </summary>
  /// <remarks>
  /// If no card is present in the reader, exception PTEID_ExNoCardPresent is thrown.
  /// If the card is not a SISCard, exception PTEID_ExCardBadType is thrown.
  /// </remarks>
  public PTEID_SISCard getSISCard()
  {
    PTEID_SISCard ret = new PTEID_SISCard(pteidlib_dotNetPINVOKE.PTEID_ReaderContext_getSISCard(swigCPtr), false);
    if (pteidlib_dotNetPINVOKE.SWIGPendingException.Pending) throw pteidlib_dotNetPINVOKE.SWIGPendingException.Retrieve();
    return ret;
  }

  /// <summary> 
  /// Specify a callback function to be called each time a card is inserted/remove in/from this reader.
  /// </summary>
  /// <returns>A handle can be used to stop the callbacks when they are no longer needed</returns>
  public uint SetEventCallback(PTEID_SetEventDelegate callback, IntPtr pvRef)
  { 
	pteidlib_dotNetPINVOKE.CUSTOM_SetEventHelper.countRef++;	
	IntPtr ptrCount = new IntPtr(pteidlib_dotNetPINVOKE.CUSTOM_SetEventHelper.countRef);

	//We add the references to the hash table
	pteidlib_dotNetPINVOKE.CUSTOM_SetEventHelper.delegateRef callbackRef =  new pteidlib_dotNetPINVOKE.CUSTOM_SetEventHelper.delegateRef();
	callbackRef.functionRef = callback;
	callbackRef.ptrRef = pvRef;
	callbackRef.handleRef = 0;
	pteidlib_dotNetPINVOKE.CUSTOM_SetEventHelper.delegateRefs.Add(pteidlib_dotNetPINVOKE.CUSTOM_SetEventHelper.countRef,callbackRef);

	//We call the SetEvent with the internal reference (countRef)
	uint ulRslt = pteidlib_dotNetPINVOKE.PTEID_ReaderContext_SetEventCallback(swigCPtr, 0, ptrCount);
	if (pteidlib_dotNetPINVOKE.SWIGPendingException.Pending) 
	{
		pteidlib_dotNetPINVOKE.CUSTOM_SetEventHelper.delegateRefs.Remove(callbackRef);
		throw pteidlib_dotNetPINVOKE.SWIGPendingException.Retrieve();
	}
	else
	{
		callbackRef.handleRef = ulRslt;
	}

	return ulRslt; 
  }

  /// <summary>To tell that the callbacks are not longer needed.</summary>
  /// <param name="ulHandle">is the handle return by SetEventCallback</param>
  public void StopEventCallback(uint ulHandle) { 
	pteidlib_dotNetPINVOKE.PTEID_ReaderContext_StopEventCallback(swigCPtr, ulHandle);
	if (pteidlib_dotNetPINVOKE.SWIGPendingException.Pending) throw pteidlib_dotNetPINVOKE.SWIGPendingException.Retrieve();
	
	//Remove ulHandle from delegateRefs 
	foreach(pteidlib_dotNetPINVOKE.CUSTOM_SetEventHelper.delegateRef callbackRef in pteidlib_dotNetPINVOKE.CUSTOM_SetEventHelper.delegateRefs.Values )
    {
      if(callbackRef.handleRef==ulHandle)
      {
          pteidlib_dotNetPINVOKE.CUSTOM_SetEventHelper.delegateRefs.Remove(callbackRef);
          break;
      }
    }	
  }

  /// <summary>Begin a transaction with the reader.</summary>
  public void BeginTransaction() {
    pteidlib_dotNetPINVOKE.PTEID_ReaderContext_BeginTransaction(swigCPtr);
    if (pteidlib_dotNetPINVOKE.SWIGPendingException.Pending) throw pteidlib_dotNetPINVOKE.SWIGPendingException.Retrieve();
  }

  /// <summary>End the transaction.</summary>
  public void EndTransaction() {
    pteidlib_dotNetPINVOKE.PTEID_ReaderContext_EndTransaction(swigCPtr);
    if (pteidlib_dotNetPINVOKE.SWIGPendingException.Pending) throw pteidlib_dotNetPINVOKE.SWIGPendingException.Retrieve();
  }

  /// <summary>Return true if this is a virtual reader (create from a file).</summary>
  public bool isVirtualReader() {
    bool ret = pteidlib_dotNetPINVOKE.PTEID_ReaderContext_isVirtualReader(swigCPtr);
    if (pteidlib_dotNetPINVOKE.SWIGPendingException.Pending) throw pteidlib_dotNetPINVOKE.SWIGPendingException.Retrieve();
    return ret;
  }

}

}
