package com.krishagni.catissueplus.core.biospecimen.services.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Configurable;

import com.krishagni.catissueplus.core.administrative.domain.ScheduledJobRun;
import com.krishagni.catissueplus.core.administrative.services.ScheduledTask;
import com.krishagni.catissueplus.core.biospecimen.domain.CollectionProtocolRegistration;
import com.krishagni.catissueplus.core.biospecimen.domain.PdeNotif;
import com.krishagni.catissueplus.core.biospecimen.domain.PdeNotifLink;
import com.krishagni.catissueplus.core.biospecimen.events.PdeTokenDetail;
import com.krishagni.catissueplus.core.biospecimen.repository.DaoFactory;
import com.krishagni.catissueplus.core.biospecimen.services.PdeTokenGenerator;
import com.krishagni.catissueplus.core.biospecimen.services.PdeTokenGeneratorRegistry;
import com.krishagni.catissueplus.core.common.PlusTransactional;
import com.krishagni.catissueplus.core.common.util.EmailUtil;
import com.krishagni.catissueplus.core.common.util.Utility;

@Configurable
public class PdeReminderNotifTask implements ScheduledTask {

	@Autowired
	private DaoFactory daoFactory;

	@Autowired
	private PdeTokenGeneratorRegistry pdeTokenGeneratorRegistry;

	@Override
	@PlusTransactional
	public void doJob(ScheduledJobRun jobRun) {
		Long lastId = 0L;
		while (true) {
			List<PdeNotif> notifs = daoFactory.getPdeNotifDao().getPendingNotifs(lastId, 100);
			if (notifs.isEmpty()) {
				break;
			}

			lastId = sendReminders(notifs);
		}
	}

	private Long sendReminders(List<PdeNotif> notifs) {
		Long lastId = null;

		for (PdeNotif notif : notifs) {
			List<PdeTokenDetail> tokens = new ArrayList<>();
			for (PdeNotifLink link : notif.getLinks()) {
				if (!link.isPending()) {
					continue;
				}

				PdeTokenGenerator tokenGenerator = pdeTokenGeneratorRegistry.get(link.getType());
				if (tokenGenerator == null) {
					continue;
				}

				PdeTokenDetail token = tokenGenerator.getToken(notif.getCpr(), link.getTokenId());
				if (token == null) {
					continue;
				}

				tokens.add(token);
			}


			sendReminder(notif.getCpr(), tokens);
			lastId = notif.getId();
		}

		return lastId;
	}

	private void sendReminder(CollectionProtocolRegistration cpr, List<PdeTokenDetail> tokens) {
		if (tokens.isEmpty()) {
			return;
		}

		String name = cpr.getParticipant().formattedName();
		if (StringUtils.isBlank(name)) {
			name = cpr.getPpid();
		}

		Map<String, Object> props = new HashMap<>();
		props.put("participantName", name);
		props.put("cpr", cpr);
		props.put("tokens", tokens);
		props.put("cpShortTitle", cpr.getCollectionProtocol().getShortTitle());
		props.put("reminder", true);
		props.put("expiryTime", Utility.getDateTimeString(tokens.get(0).getExpiryTime()));
		props.put("$subject", new Object[] {2, cpr.getCollectionProtocol().getShortTitle(), cpr.getPpid() });

		EmailUtil.getInstance().sendEmail(
			"pde_links",
			new String[] { cpr.getParticipant().getEmailAddress() },
			null,
			props);
	}
}

@@ -568,28 +568,182 @@
 Protocol
-Registration
+;%0Aimport com.krishagni.catissueplus.core.biospecimen.domain.CollectionProtocolRegistration;%0Aimport com.krishagni.catissueplus.core.biospecimen.domain.CpWorkflowConfig
 ;%0Aimport
@@ -1186,24 +1186,76 @@
 orRegistry;%0A
+import com.krishagni.catissueplus.core.common.Pair;%0A
 import com.k
@@ -2828,16 +2828,156 @@
 );%0A%09%09%7D%0A%0A
+%09%09Pair%3CString, String%3E emailTmpl = getEmailTmpl(cpr.getCollectionProtocol());%0A%09%09String%5B%5D rcpt = %7B cpr.getParticipant().getEmailAddress() %7D;%0A
 %09%09Map%3CSt
@@ -3403,17 +3403,16 @@
 d() %7D);%0A
-%0A
 %09%09EmailU
@@ -3439,20 +3439,16 @@
 ndEmail(
-%0A%09%09%09
 %22pde_lin
@@ -3455,86 +3455,489 @@
 ks%22,
-%0A%09%09%09new String%5B%5D %7B cpr.getParticipant().getEmailAddress() %7D,%0A%09%09%09null,%0A%09%09%09props
+ emailTmpl.first(), emailTmpl.second(), rcpt, props);%0A%09%7D%0A%0A%09private Pair%3CString, String%3E getEmailTmpl(CollectionProtocol cp) %7B%0A%09%09String subject  = null;%0A%09%09String template = null;%0A%09%09CpWorkflowConfig.Workflow wf = CpWorkflowTxnCache.getInstance().getWorkflow(cp.getId(), %22pde-settings%22);%0A%09%09if (wf != null && wf.getData() != null) %7B%0A%09%09%09subject  = (String) wf.getData().get(%22emailSubject%22);%0A%09%09%09template = (String) wf.getData().get(%22emailTmpl%22);%0A%09%09%7D%0A%0A%09%09return Pair.make(subject, template
 );%0A%09

Use the CP email settings, if available, to send the reminders to the participants to fill up the survey forms.