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

import cn.hutool.core.collection.CollUtil;
import cn.superdesk.uniorder.common.enums.invoice.InvoiceApplyDimEnum;
import cn.superdesk.uniorder.common.enums.invoice.supplier.InvoiceApplySupplierMakeInvoiceStatusEnum;
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.makeInvoiceStatus.state.InvoiceApplySupplierFieldMakeInvoiceStatusState;
import cn.superdesk.uniorder.pattern.state.invoice.supplier.invoiceApplySupplier.makeInvoiceStatus.state.concrete.InvoiceApplySupplierFieldMakeInvoiceStatusConcreteState_0_未开票;
import cn.superdesk.uniorder.pattern.state.invoice.supplier.invoiceApplySupplier.makeInvoiceStatus.state.concrete.InvoiceApplySupplierFieldMakeInvoiceStatusConcreteState_1_已开票;
import cn.superdesk.uniorder.pattern.state.invoice.supplier.invoiceApplySupplier.makeInvoiceStatus.state.concrete.InvoiceApplySupplierFieldMakeInvoiceStatusConcreteState_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 InvoiceApplySupplierFieldMakeInvoiceStatusContext {

	private InvoiceApplySupplierFieldMakeInvoiceStatusState state_0_未开票;
	private InvoiceApplySupplierFieldMakeInvoiceStatusState state_1_已开票;
	private InvoiceApplySupplierFieldMakeInvoiceStatusState state_2_已撤消;

	private InvoiceApplySupplierFieldMakeInvoiceStatusState state;

	private InvoiceApplySupplier entity;

	private InvoiceApplyDimEnum invoiceApplyDimEnum;

	private List<InvoiceApplySupplier>                         entitys;
	private InvoiceApplySupplierOpCommonDimAccountingOrderForm form;

	private LoginSysUser loginSysUser;


	public InvoiceApplySupplierFieldMakeInvoiceStatusContext(InvoiceApplySupplier entity, LoginSysUser loginSysUser) {

		this.state_0_未开票 = new InvoiceApplySupplierFieldMakeInvoiceStatusConcreteState_0_未开票(this);
		this.state_1_已开票 = new InvoiceApplySupplierFieldMakeInvoiceStatusConcreteState_1_已开票(this);
		this.state_2_已撤消 = new InvoiceApplySupplierFieldMakeInvoiceStatusConcreteState_2_已撤消(this);

		this.invoiceApplyDimEnum = InvoiceApplyDimEnum.票申请;

		this.entity = entity;
		this.loginSysUser = loginSysUser;

		switch (InvoiceApplySupplierMakeInvoiceStatusEnum.get(this.entity.getMakeInvoiceStatus())) {

			case 未开票:
				state = state_0_未开票;
				break;
			case 已开票:
				state = state_1_已开票;
				break;
			case 已撤消:
				state = state_2_已撤消;
				break;

			default:
//				state = state_0_未开票;
				break;

		}
	}

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

		this.state_0_未开票 = new InvoiceApplySupplierFieldMakeInvoiceStatusConcreteState_0_未开票(this);
		this.state_1_已开票 = new InvoiceApplySupplierFieldMakeInvoiceStatusConcreteState_1_已开票(this);
		this.state_2_已撤消 = new InvoiceApplySupplierFieldMakeInvoiceStatusConcreteState_2_已撤消(this);

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

		this.loginSysUser = loginSysUser;

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

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

		InvoiceApplySupplierMakeInvoiceStatusEnum switchEnum = InvoiceApplySupplierMakeInvoiceStatusEnum.未开票;

		if (CollUtil.isEmpty(makeInvoiceStatus)) {
			switchEnum = InvoiceApplySupplierMakeInvoiceStatusEnum.未开票;
		}
		if (makeInvoiceStatus.size() == 1) {
			switchEnum = InvoiceApplySupplierMakeInvoiceStatusEnum.get(makeInvoiceStatus.get(0));
		} else {
			switchEnum = InvoiceApplySupplierMakeInvoiceStatusEnum.未开票;
		}

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

		switch (switchEnum) {

			case 未开票:
				state = state_0_未开票;
				break;
			case 已开票:
				state = state_1_已开票;
				break;
			case 已撤消:
				state = state_2_已撤消;
				break;

			default:
//				state = state_0_未开票;
				break;

		}
	}

	// 未开票(0, "未开票"),
	public void opInit() {
		state.opInit();
	}

	// 已开票(1, "已开票"),
	public void opComplete() {
		state.opComplete();
	}

	// 已撤消(2, "已撤消"),
	public void opBecomeInvalid() {
		state.opBecomeInvalid();
	}


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