package com.nexstreaming.hlsaes128getkeysample;

/**
 * Created by Ian.Lee on 14/11/2016.
 */

public class NexHLSAES128DRMManager {

	private IAESCallbackListener mAESCallbackListener = null;

	/**
	 * \brief Initializes and registers NexHLSAES128DRMManager.
	 *
	 * \param strEngineLibName The relevant engine library name as a \c string.
	 */
	public native int initDRMManager(String strEnginePath);

	/**
	 * For internal use only. Please do not use.
	 */
	public native int initDRMManagerMulti(Object nexPlayerHandle, String strEnginePath);


	/**
	 * \brief This method sets and registers a \c IAESCallbackListener listener.
	 *
	 * @param listener      IAESCallbackListener
	 *
	 * \see NexPlayer.IHTTPABRTrackChangeListener
	 *
	 * \since version 6.44
	 */
	public void setAESCallbackListener(IAESCallbackListener listener)
	{
		mAESCallbackListener = listener;
	}

	//Called by native
	private boolean isSupportKeyAttr(String strKeyAttr, String strURL) {
		if(mAESCallbackListener != null)
		{
			return mAESCallbackListener.isSupportKeyAttr(strKeyAttr, strURL);
		}
		else
			return false;
	}

	//Called by native
	private byte[] getKeyFromExternal(String strKeyURL) {
		if(mAESCallbackListener != null)
		{
			return mAESCallbackListener.getKeyFromExternal(strKeyURL);
		}
		else
			return null;
	}


	/**
	 * @brief An interface for GetKeyExternal and IsSupportKey Callbacks.
	 * This must be implemented by the UI.
	 */
	public interface IAESCallbackListener
	{
		/**
		 * \brief   Callback function that verifies whether or not a key attribute is supported by the DRM module.
		 * This callback is called when NexPlayer meets #EXT-X-KEY tags while NexPlayer is parsing
		 * playlists. If the callback returns a non-zero value, NexPlayer will not call DRM functions even if that function is registered.
		 *
		 * \param  strURL		The URL of the playlist.
		 * \param  strKeyAttr	Key information of the segment that has been received.
		 *
		 * \returns				If the DRM supports that key attribute, then it should return <b>TRUE</b>.
		 * 						Then, NexPlayer will call DRM callbacks depending on the encryption method.
		 * 						If the DRM does not support the key, then it should return <b>FALSE</b>.
		 * 						In this case, NexPlayer will not call the "getKeyFromExternal" callbacks
		 * 						and it will decrypt using its internal decryption function.
		 *
		 * */
		boolean isSupportKeyAttr(String strKeyAttr, String strURL);

		/**
		 * \brief  Callback function to retrieve an encryption key from an HLS playlist over HTTPS for descrambling.
		 * This function is called each time a new playlist is received and an encryption key is available in the playlist.
		 *
		 * \param strKeyUrl		A pointer to the URL of the encryption key.
		 *
		 * \returns 			A byte array where the encryption key information will be stored.
		 *
		 * */
		byte[] getKeyFromExternal(String strKeyUrl);
	}

	static {
		System.loadLibrary("nexhlsaes128callbacks");
	}

}
