package org.taktik.connector.business.recipe.executor.mock;

import java.io.IOException;
import java.util.ArrayList;
import java.util.GregorianCalendar;
import java.util.List;
import java.util.UUID;

import org.apache.log4j.Logger;

import org.bouncycastle.util.encoders.Base64;

import org.taktik.connector.business.recipeprojects.core.exceptions.IntegrationModuleException;
import org.taktik.connector.business.recipeprojects.core.utils.IOUtils;
import org.taktik.connector.business.recipeprojects.core.utils.PropertyHandler;
import org.taktik.connector.business.recipe.executor.ExecutorIntegrationModuleImpl;
import be.recipe.services.executor.ListNotificationsItem;
import java.util.Date;

public class ExecutorIntegrationModuleMock extends ExecutorIntegrationModuleImpl {
	private static final Logger LOG = Logger.getLogger(ExecutorIntegrationModuleMock.class);
	private static final String MOCK_NOTIF = "H4sIAAAAAAAAAO1YTVPjOBA9w69w5byO5XwwhBKhXEmGyRAC5RiWOSq2iLXjr5VkEvbXb8uObTmB2Z2t3cNWATlEcre6X/fTawO+2sWR8UK5YGly2bG7qGPQxE8DlmwuO/PVnXl+PhyZdudqfIqTVLJn5hMJtgb4JeKyE0qZXViWoPyF+VR0OfVZRrtr2jFOTwqbi51gtd12u+1u+92Ub6weQrb1dLtY+SGNickSIUnil36CXYhie5GW4X4QyNDT6u5E0BmfGgaWdCfHMmTCgA9pGWGreKisvsc05DEVgmzo+PQEh5QElMO3E6zSCQgP1OIE+4GxuuxMpubKc5ZTx512jNVjWbAxIOnZPTTEll9YY0v3xaxwnU/Nm9vZF7f0Gyi/Tzb6ZJ8ju2v3+gNssdI8IJKqI0cmOjN7PWwVG+qJZDEdo7ML1L9ACECoZZEpTfZJAwA/I1y+Fgst9JfJveN637Skq+BVXB3jsXUGDMnCV8F8RpIKJ7g8My5kQiCRxzTl2GrW++ckZtFrsQF95uw3oBpYNbtF0paWNRSvhoOLJjOayJ8EZ3fPBvDbhQ9Cg5+BSHhMfCbkWxitY1CuStCkfwWphQMT/3uSbiMabOIaWp2YM7lZ3v26mE2vb2dLT8svTSjnqsYVyY6OgaAVffFzGlVFfIt/ZZV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apW+e9zp3nX76GeOx8yfhTqNYzyhlwjF+LDAXrJAaxL4WbPtA6MMFs/laJN4SQxtTn/5YwDNtqXhBVJgbtkipdFMTd9Mbmtdds2NuaVa34IzFuEq5wGA3WaIasBF5WI9XJtSEH9p36yqkCkCXlIN6FG7cMhEZnhu4tjsr1QiCrgSCh3xuA9uvYG8F14UiLjA48NpxwvR8AlZ9nQL2gtKlqXpD1xuFpWtnkLWEEXgM8aD8l1fohvIps+1Ix2APbN4qjl0I+a3I3rvwxfGWEb5ydLfFdNWVYiGt2kNL1/WRRZGDsS3YPzArnd+OAw8d6+wg/T84MHJ8SR5sNpuaXpoHnzKhyYMF7Oaxn8WFBcHfDhm+s+Em3v+ODde+25s/oMTmIyPOqUCBjT3Kfzs2LH8094vZsBwXHkuGjWar0foJMtwaKqKskcd8l7s2JuF5P+pVLiobOnRmJZ45k2HeEDW+c+Mm3td2RBJhvB+T5B+TOK7/Go9JFOsXy+L6jfL+1xMQ2t43gzD5m3fOhpL+/5uzfwEC3nENxyMAAA==";
	
	/**
	 * Instantiates a new executor integration module mock.
	 * 
	 * @throws IntegrationModuleException the integration module exception
	 */
	public ExecutorIntegrationModuleMock() throws IntegrationModuleException {
		super();
	}

	/** {@inheritDoc} */
	@Override
	public void createFeedback(String prescriberId, String rid,
			byte[] feedbackText) throws IntegrationModuleException {
	}

	@Override
	protected void init() throws IntegrationModuleException {
	}
	
	/** {@inheritDoc} */
	@Override
	public org.taktik.connector.business.recipe.executor.domain.GetPrescriptionForExecutorResult getPrescription(String rid)
			throws IntegrationModuleException {
		LOG.info("GetPrescriptionForExecutorResult : " + rid);
		org.taktik.connector.business.recipe.executor.domain.GetPrescriptionForExecutorResult result =
			new org.taktik.connector.business.recipe.executor.domain.GetPrescriptionForExecutorResult();
		result.setCreationDate(GregorianCalendar.getInstance());
		result.setEncryptionKeyId(UUID.randomUUID().toString().replaceAll("-", ""));
		result.setPatientId("84071230581l");
		result.setPrescriberId("12659389004l");
		result.setPrescriptionType("P0");
		result.setRid("BEP0" + 
				UUID.randomUUID().toString().replaceAll("-", "").substring(0, 8));
		result.setTimestampingId(UUID.randomUUID().toString().replaceAll("-", ""));
		result.setEncryptionKey("fake key".getBytes());
		result.setSealedContent(MOCK_PRESC.getBytes());
		
		if( getPropertyHandler().hasProperty("MOCK_PRESC") ){
			try {
				LOG.info("Loading prescription from " + getPropertyHandler().getProperty("MOCK_PRESC"));
				result.setPrescription(
						IOUtils.getBytes(
						IOUtils.getResourceAsStream(getPropertyHandler().getProperty("MOCK_PRESC"))));
			} catch (IOException e) {
				e.printStackTrace();
				byte [] b = compressedB64toString(MOCK_PRESC).getBytes();
				result.setSealedContent(b);
				result.setPrescription(b);
			}
		}else{
			byte [] b = compressedB64toString(MOCK_PRESC).getBytes();
			result.setSealedContent(b);
			result.setPrescription(b);
		}
		return result;
	}

	/** {@inheritDoc} */
	@Override
	public List<ListNotificationsItem> listNotifications(boolean readFlag)
			throws IntegrationModuleException {
		LOG.info("listNotifications : " + readFlag);
		
		byte[] bytes = null;
		if( getPropertyHandler().hasProperty("MOCK_NOTIF")  ){
			try {
				LOG.info("Loading prescription from " + getPropertyHandler().getProperty("MOCK_NOTIF"));
				bytes = IOUtils.getBytes(
								IOUtils.getResourceAsStream(getPropertyHandler().getProperty("MOCK_NOTIF")));
			} catch (IOException e) {
				e.printStackTrace();
				bytes =	compressedB64toString(MOCK_NOTIF).getBytes();
			}
		}else{
			bytes =	compressedB64toString(MOCK_NOTIF).getBytes();
		}
		
		List<ListNotificationsItem> list = new ArrayList<ListNotificationsItem>();
		for( int i = 0 ; i < 10 ; i++){
			org.taktik.connector.business.recipe.executor.domain.ListNotificationsItem item = new org.taktik.connector.business.recipe.executor.domain.ListNotificationsItem(new ListNotificationsItem());
			item.setContent(bytes);
			item.setSentBy("12659389004l");
			item.setSentDate(new Date());
			list.add(item);
		}
		return list;
	}

	
	/**
	 * Compressed b64to string.
	 *
	 * @param s the s
	 * @return the string
	 */
	private String compressedB64toString(String s){
		byte[] bytes = Base64.decode( 
				s.getBytes());
		try {
			bytes = IOUtils.decompress(bytes);
		} catch (IOException e) {
			e.printStackTrace();
		}
		return new String (bytes);
	}
	
	
	/** {@inheritDoc} */
	@Override
	public void markAsArchived(String rid) throws IntegrationModuleException {
		LOG.info("markAsArchived : " + rid);
	}

	/** {@inheritDoc} */
	@Override
	public void markAsDelivered(String rid) throws IntegrationModuleException {
		LOG.info("markAsDelivered : " + rid);
	}

	/** {@inheritDoc} */
	@Override
	public void markAsUndelivered(String rid) throws IntegrationModuleException {
		LOG.info("markAsUndelivered : " + rid);
	}

	/** {@inheritDoc} */
	@Override
	public void revokePrescription(String rid, String reason)
			throws IntegrationModuleException {
		LOG.info("revoked : " + rid);
	}
	
	/*private byte[] getResponse(String fileName) {
		String fs = System.getProperty("file.separator");
		File file = new File("."+fs+"src"+fs+"main"+fs+"resources"+fs+"mock"+fs+fileName);
		try {
			FileInputStream fis = new FileInputStream(file);
			byte[] xml = new byte[(int)file.length()];
			fis.read(xml);
			return xml;
		} catch (FileNotFoundException e1) {
			e1.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return null;
	}*/
}
