package org.ei.drishti.service;
import org.ei.drishti.domain.Action;
import org.ei.drishti.domain.Child;
import org.ei.drishti.domain.EligibleCouple;
import org.ei.drishti.domain.Mother;
import org.ei.drishti.dto.ActionData;
import org.ei.drishti.dto.AlertStatus;
import org.ei.drishti.dto.BeneficiaryType;
import org.ei.drishti.repository.AllActions;
import org.ei.drishti.repository.AllChildren;
import org.ei.drishti.repository.AllEligibleCouples;
import org.ei.drishti.repository.AllMothers;
import org.joda.time.DateTime;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
import static org.ei.drishti.dto.BeneficiaryType.*;
@Service
public class ActionService {
private AllActions allActions;
private AllMothers allMothers;
private AllChildren allChildren;
private AllEligibleCouples allEligibleCouples;
@Autowired
public ActionService(AllActions allActions, AllMothers allMothers, AllChildren allChildren, AllEligibleCouples allEligibleCouples) {
this.allActions = allActions;
this.allMothers = allMothers;
this.allChildren = allChildren;
this.allEligibleCouples = allEligibleCouples;
}
public List<Action> getNewAlertsForANM(String anmIdentifier, long timeStamp) {
return allActions.findByANMIDAndTimeStamp(anmIdentifier, timeStamp);
}
public void alertForBeneficiary(BeneficiaryType beneficiaryType, String caseID, String scheduleName, String visitCode, AlertStatus alertStatus, DateTime startDate, DateTime expiryDate) {
// TODO: Get rid of this horrible if-else after Motech-Platform fixes the bug related to metadata in motech-schedule-tracking.
String anmIdentifier;
if (mother.equals(beneficiaryType)) {
Mother mother = allMothers.findByCaseId(caseID);
anmIdentifier = mother == null ? null : mother.anmIdentifier();
} else if (child.equals(beneficiaryType)) {
Child child = allChildren.findByCaseId(caseID);
anmIdentifier = child == null ? null : child.anmIdentifier();
} else if (ec.equals(beneficiaryType)) {
EligibleCouple eligibleCouple = allEligibleCouples.findByCaseId(caseID);
anmIdentifier = eligibleCouple == null ? null : eligibleCouple.anmIdentifier();
} else {
throw new IllegalArgumentException("Beneficiary Type : " + beneficiaryType + " is of unknown type");
}
if (anmIdentifier != null) {
allActions.addOrUpdateAlert(new Action(caseID, anmIdentifier, ActionData.createAlert(beneficiaryType, scheduleName, visitCode, alertStatus, startDate, expiryDate)));
}
}
public void markAllAlertsAsInactive(String entityId) {
allActions.markAllAsInActiveFor(entityId);
}
public void markAlertAsInactive(String anmId, String entityId, String scheduleName) {
allActions.markAlertAsInactiveFor(anmId, entityId, scheduleName);
}
public void markAlertAsClosed(String caseId, String anmIdentifier, String visitCode, String completionDate) {
allActions.add(new Action(caseId, anmIdentifier, ActionData.markAlertAsClosed(visitCode, completionDate)));
}
public void closeMother(String caseId, String anmIdentifier, String reasonForClose) {
allActions.add(new Action(caseId, anmIdentifier, ActionData.closeMother(reasonForClose)));
}
public void reportForIndicator(String anmIdentifier, ActionData actionData) {
allActions.add(new Action("", anmIdentifier, actionData));
}
public void deleteReportActions() {
allActions.deleteAllByTarget("report");
}
}