package nc.impl.pu.m25.action;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import nc.bs.framework.common.NCLocator;
import nc.bs.ic.base.InvoiceAfterDeleteEvent;
import nc.bs.pu.m25.maintain.InvoiceDeleteBP;
import nc.bs.pu.m25.plugin.InvoicePluginPoint;
import nc.impl.pubapp.pattern.data.bill.tool.BillTransferTool;
import nc.impl.pubapp.pattern.rule.processer.AroundProcesser;
import nc.itf.pu.m25.IInvoiceQuery;
import nc.vo.ml.NCLangRes4VoTransl;
import nc.vo.pu.m25.entity.InvoiceVO;
import nc.vo.pu.m25.enumeration.InvoiceUserAnswerType;
import nc.vo.pu.m25.env.InvoiceUIToBSEnv;
import nc.vo.pu.m25.exception.InvoiceDelResumeException;
import nc.vo.pu.m25.rule.maintain.InvoiceDelStatusChkRule;
import nc.vo.pu.pub.util.ApproveFlowUtil;
import nc.vo.pub.BusinessException;
import nc.vo.pubapp.pattern.data.ValueUtils;
import nc.vo.pubapp.pattern.exception.ExceptionUtils;
import org.apache.commons.lang.ArrayUtils;

public class InvoiceDeleteAction {
	public void delete(InvoiceVO[] invoiceVOs, InvoiceUIToBSEnv[] envs) {
		InvoiceVO[] fees = this.queryFeeVO(invoiceVOs);
		InvoiceVO[] unApproveFees = this.pickupUnapproveFeeVO(fees);
		InvoiceVO[] deleteFees = null;
		InvoiceVO[] cutRelationFees = fees;
		InvoiceUIToBSEnv env = ArrayUtils.isEmpty(envs) ? new InvoiceUIToBSEnv() : envs[0];
		if (!ArrayUtils.isEmpty(unApproveFees)) {
			InvoiceUserAnswerType atype = this.getAnswerType(env);
			if (InvoiceUserAnswerType.NO_ANSWER == atype) {
				ExceptionUtils.wrappException(new InvoiceDelResumeException(
						NCLangRes4VoTransl.getNCLangRes().getStrByID("4004050_0", "04004050-0034")));
			} else if (InvoiceUserAnswerType.YES == atype) {
				deleteFees = unApproveFees;
				cutRelationFees = this.pickupApproveFeeVO(fees);
			}
		}

		BillTransferTool<InvoiceVO> tool = new BillTransferTool(invoiceVOs);
		if (!ArrayUtils.isEmpty(fees)) {
			new BillTransferTool(fees);
		}

		InvoiceVO[] vos = (InvoiceVO[]) tool.getClientFullInfoBill();
		AroundProcesser<InvoiceVO> processer = new AroundProcesser(InvoicePluginPoint.DELETE);
		this.addRule(processer);
		processer.before(vos);
		(new InvoiceDeleteBP(env)).delete(vos, deleteFees, cutRelationFees);
		processer.after(vos);
	}

	private void addRule(AroundProcesser<InvoiceVO> processer) {
		processer.addBeforeFinalRule(new InvoiceDelStatusChkRule());
		processer.addAfterFinalRule(new InvoiceAfterDeleteEvent());	
	}

	private InvoiceUserAnswerType getAnswerType(InvoiceUIToBSEnv env) {
		if (null == env) {
			return InvoiceUserAnswerType.NO_ANSWER;
		} else {
			return null == env.getDelAnswer() ? InvoiceUserAnswerType.NO_ANSWER : env.getDelAnswer();
		}
	}

	private InvoiceVO[] pickupApproveFeeVO(InvoiceVO[] fees) {
		InvoiceVO[] unfees = this.pickupUnapproveFeeVO(fees);
		if (ArrayUtils.isEmpty(unfees)) {
			return fees;
		} else {
			Set<InvoiceVO> feeset = new HashSet(Arrays.asList(fees));
			Set<InvoiceVO> unfeeset = new HashSet(Arrays.asList(unfees));
			feeset.removeAll(unfeeset);
			return (InvoiceVO[]) feeset.toArray(new InvoiceVO[feeset.size()]);
		}
	}

	private InvoiceVO[] pickupUnapproveFeeVO(InvoiceVO[] fees) {
		if (ArrayUtils.isEmpty(fees)) {
			return null;
		} else {
			List<InvoiceVO> unfees = new ArrayList();
			InvoiceVO[] arr$ = fees;
			int len$ = fees.length;

			for (int i$ = 0; i$ < len$; ++i$) {
				InvoiceVO vo = arr$[i$];
				if (ApproveFlowUtil.isCanDel(vo)) {
					unfees.add(vo);
				}
			}

			return (InvoiceVO[]) unfees.toArray(new InvoiceVO[unfees.size()]);
		}
	}

	private InvoiceVO[] queryFeeVO(InvoiceVO[] vos) {
		Set<String> ids = new HashSet();
		InvoiceVO[] arr$ = vos;
		int len$ = vos.length;

		for (int i$ = 0; i$ < len$; ++i$) {
			InvoiceVO vo = arr$[i$];
			if (!ValueUtils.getBoolean(vo.getParentVO().getBfee())) {
				ids.add(vo.getParentVO().getPk_invoice());
			}
		}

		if (0 == ids.size()) {
			return null;
		} else {
			IInvoiceQuery querysrv = (IInvoiceQuery) NCLocator.getInstance().lookup(IInvoiceQuery.class);
			InvoiceVO[] feeVos = null;

			try {
				feeVos = querysrv.queryFee((String[]) ids.toArray(new String[ids.size()]));
			} catch (BusinessException var7) {
				ExceptionUtils.wrappException(var7);
			}

			return feeVos;
		}
	}
}