package org.opencps.dossiermgt.action.util;

import java.util.List;

import org.opencps.dossiermgt.action.PaymentFileActions;
import org.opencps.dossiermgt.action.impl.PaymentFileActionsImpl;
import org.opencps.dossiermgt.model.Dossier;
import org.opencps.dossiermgt.model.PaymentFile;
import org.opencps.dossiermgt.model.ProcessOption;
import org.opencps.dossiermgt.model.ServiceConfig;
import org.opencps.dossiermgt.service.DossierLocalServiceUtil;
import org.opencps.dossiermgt.service.ProcessOptionLocalServiceUtil;
import org.opencps.dossiermgt.service.ServiceConfigLocalServiceUtil;

import com.liferay.portal.kernel.exception.PortalException;
import com.liferay.portal.kernel.util.GetterUtil;
import com.liferay.portal.kernel.util.Validator;

public class DossierMgtUtils {
	public static ProcessOption getProcessOption(String serviceInfoCode, String govAgencyCode, String dossierTemplateNo,
			long groupId) throws PortalException {

		ServiceConfig config = ServiceConfigLocalServiceUtil.getBySICodeAndGAC(groupId, serviceInfoCode, govAgencyCode);

		return ProcessOptionLocalServiceUtil.getByDTPLNoAndServiceCF(groupId, dossierTemplateNo,
				config.getServiceConfigId());
	}

	protected Dossier getDossier(long groupId, String refId) throws PortalException {

		Dossier dossier = null;

		dossier = DossierLocalServiceUtil.getByRef(groupId, refId);

		if (Validator.isNull(dossier)) {
			long dossierId = GetterUtil.getLong(refId);

			if (dossierId != 0) {
				dossier = DossierLocalServiceUtil.fetchDossier(dossierId);
			}
		}

		return dossier;
	}

	public static boolean checkPreCondition(String[] preConditions, Dossier dossier) {
		boolean result = true;
		
		
		
		for (String preCondition : preConditions) {
			switch (preCondition) {
			case "payok":
				result = result && checkPayOk(dossier);
				break;

			case "cancelling":
				result = result && checkCancelling(dossier);
				break;

			case "reject_cancelling":
				result = result && checkCancelling(dossier);
				break;

			case "correcting":
				result = result && checkCorrecting(dossier);
				break;

			default:
				break;
			}
		}

		return result;
	}

	private static boolean checkPayOk(Dossier dossier) {
		boolean result = true;
		PaymentFileActions actions = new PaymentFileActionsImpl();
		List<PaymentFile> paymentFiles = actions.getPaymentFiles(dossier.getDossierId());
		if (paymentFiles != null) {
			for (PaymentFile paymentFile : paymentFiles) {
				if (paymentFile.getPaymentStatus() != 2) {
					result = result && false;
				}
			}
		}
		return result;
	}

	private static boolean checkCancelling(Dossier dossier) {
		if (dossier.getCancellingDate() != null) {
			return true;
		}
		return false;
	}

	private static boolean checkCorrecting(Dossier dossier) {
		if (dossier.getCorrecttingDate() != null) {
			return true;
		}

		return false;
	}
	
}

@@ -2077,16 +2077,279 @@
 break;%0A%0A
+%09%09%09case %22reject_correcting%22:%0A%09%09%09%09result = result && checkCorrecting(dossier);%0A%09%09%09%09break;%0A%0A%09%09%09case %22submitting%22:%0A%09%09%09%09result = result && checkSubmitting(dossier);%0A%09%09%09%09break;%0A%0A%09%09%09case %22reject_submitting%22:%0A%09%09%09%09result = result && checkSubmitting(dossier);%0A%09%09%09%09break;%0A%0A
 %09%09%09defau
@@ -3078,24 +3078,173 @@
  true;%0A%09%09%7D%0A%0A
+%09%09return false;%0A%09%7D%0A%09%0A%09%0A%09private static boolean checkSubmitting(Dossier dossier) %7B%0A%09%09if (dossier.getEndorsementDate() != null) %7B%0A%09%09%09return true;%0A%09%09%7D%0A%0A
 %09%09return fal

Update dossier backend