package cn.superdesk.uniorder.pattern.state.invoice.supplier.invoiceApplySupplier.payoffAccountStatus.context;

import cn.hutool.core.collection.CollUtil;
import cn.superdesk.uniorder.common.enums.invoice.InvoiceApplyDimEnum;
import cn.superdesk.uniorder.common.enums.invoice.supplier.InvoiceApplySupplierPayOffAccountStatusEnum;
import cn.superdesk.uniorder.common.lamda.AnLamdaUtils;
import cn.superdesk.uniorder.common.security.dto.LoginSysUser;
import cn.superdesk.uniorder.dao.entity.invoice.supplier.InvoiceApplySupplier;
import cn.superdesk.uniorder.dto.param.invoice.supplier.status.InvoiceApplySupplierOpCommonDimAccountingOrderForm;
import cn.superdesk.uniorder.pattern.state.invoice.supplier.invoiceApplySupplier.payoffAccountStatus.state.InvoiceApplySupplierFieldPayoffAccountStatusState;
import cn.superdesk.uniorder.pattern.state.invoice.supplier.invoiceApplySupplier.payoffAccountStatus.state.concrete.InvoiceApplySupplierFieldPayoffAccountStatusConcreteState_0_待付款;
import cn.superdesk.uniorder.pattern.state.invoice.supplier.invoiceApplySupplier.payoffAccountStatus.state.concrete.InvoiceApplySupplierFieldPayoffAccountStatusConcreteState_10_付款中;
import cn.superdesk.uniorder.pattern.state.invoice.supplier.invoiceApplySupplier.payoffAccountStatus.state.concrete.InvoiceApplySupplierFieldPayoffAccountStatusConcreteState_1_部分付款;
import cn.superdesk.uniorder.pattern.state.invoice.supplier.invoiceApplySupplier.payoffAccountStatus.state.concrete.InvoiceApplySupplierFieldPayoffAccountStatusConcreteState_2_已付款;
import lombok.Data;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @Description: 简易状态机 星型结构
 * @Author: linjinyu
 * @CreateDate: 10/12/20 4:28 PM
 * @UpdateUser: linjinyu
 * @UpdateDate: 10/12/20 4:28 PM
 * @UpdateRemark: 修改内容
 * @Version: 1.0
 */
@Data
public class InvoiceApplySupplierFieldPayoffAccountStatusContext {

	private InvoiceApplySupplierFieldPayoffAccountStatusState state_0_待付款;
	private InvoiceApplySupplierFieldPayoffAccountStatusState state_10_付款中;
	private InvoiceApplySupplierFieldPayoffAccountStatusState state_1_部分付款;
	private InvoiceApplySupplierFieldPayoffAccountStatusState state_2_已付款;

	private InvoiceApplySupplierFieldPayoffAccountStatusState state;

	private InvoiceApplySupplier entity;

	private InvoiceApplyDimEnum invoiceApplyDimEnum;

	private List<InvoiceApplySupplier>                         entitys;
	private InvoiceApplySupplierOpCommonDimAccountingOrderForm form;

	private LoginSysUser loginSysUser;


	public InvoiceApplySupplierFieldPayoffAccountStatusContext(InvoiceApplySupplier entity, LoginSysUser loginSysUser) {

		this.state_0_待付款 = new InvoiceApplySupplierFieldPayoffAccountStatusConcreteState_0_待付款(this);
		this.state_10_付款中 = new InvoiceApplySupplierFieldPayoffAccountStatusConcreteState_10_付款中(this);
		this.state_1_部分付款 = new InvoiceApplySupplierFieldPayoffAccountStatusConcreteState_1_部分付款(this);
		this.state_2_已付款 = new InvoiceApplySupplierFieldPayoffAccountStatusConcreteState_2_已付款(this);


		this.invoiceApplyDimEnum = InvoiceApplyDimEnum.票申请;
		this.entity = entity;
		this.loginSysUser = loginSysUser;

		switch (InvoiceApplySupplierPayOffAccountStatusEnum.get(this.entity.getPayoffAccountStatus())) {

			case 待付款:
				state = state_0_待付款;
				break;
			case 付款中:
				state = state_10_付款中;
				break;
			case 部分付款:
				state = state_1_部分付款;
				break;
			case 已付款:
				state = state_2_已付款;
				break;

			default:
//				state = state_0_待付款;
				break;

		}
	}

	public InvoiceApplySupplierFieldPayoffAccountStatusContext(List<InvoiceApplySupplier> entitys, InvoiceApplySupplierOpCommonDimAccountingOrderForm form, LoginSysUser loginSysUser) {

		this.state_0_待付款 = new InvoiceApplySupplierFieldPayoffAccountStatusConcreteState_0_待付款(this);
		this.state_10_付款中 = new InvoiceApplySupplierFieldPayoffAccountStatusConcreteState_10_付款中(this);
		this.state_1_部分付款 = new InvoiceApplySupplierFieldPayoffAccountStatusConcreteState_1_部分付款(this);
		this.state_2_已付款 = new InvoiceApplySupplierFieldPayoffAccountStatusConcreteState_2_已付款(this);


		this.invoiceApplyDimEnum = InvoiceApplyDimEnum.对账单;
		this.entitys = entitys;
		this.form = form;
		this.loginSysUser = loginSysUser;

		/****************************************** 异常就按 未收票 逻辑 start ******************************************/

		List<Integer> payoffAccountStatus = entitys.stream().filter(AnLamdaUtils.distinctByKey(o -> o.getPayoffAccountStatus())).map(InvoiceApplySupplier::getPayoffAccountStatus).collect(Collectors.toList());

		InvoiceApplySupplierPayOffAccountStatusEnum switchEnum = InvoiceApplySupplierPayOffAccountStatusEnum.待付款;

		if (CollUtil.isEmpty(payoffAccountStatus)) {
			switchEnum = InvoiceApplySupplierPayOffAccountStatusEnum.待付款;
		}
		if (payoffAccountStatus.size() == 1) {
			switchEnum = InvoiceApplySupplierPayOffAccountStatusEnum.get(payoffAccountStatus.get(0));
		} else {
			switchEnum = InvoiceApplySupplierPayOffAccountStatusEnum.待付款;
		}

		/****************************************** 异常就按 未收票 逻辑   end ******************************************/

		switch (switchEnum) {

			case 待付款:
				state = state_0_待付款;
				break;
			case 付款中:
				state = state_10_付款中;
				break;
			case 部分付款:
				state = state_1_部分付款;
				break;
			case 已付款:
				state = state_2_已付款;
				break;

			default:
//				state = state_0_待付款;
				break;

		}
	}


	// 待付款(0, "待付款"),
	public int opInit() {
		return state.opInit();
	}

	// 付款中(10, "付款中"),
	public int opRegulatoryPipeline() {
		return state.opRegulatoryPipeline();
	}

	// 部分付款(1, "部分付款"),
	public int opPart() {
		return state.opPart();
	}

	// 已付款(2, "已付款"),
	public int opComplete() {
		return state.opComplete();
	}


	@Override
	public String toString() {
		return "状态 :" + getState().getClass().getName();
	}
}