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

import cn.hutool.core.collection.CollUtil;
import cn.superdesk.uniorder.common.enums.invoice.InvoiceApplyDimEnum;
import cn.superdesk.uniorder.common.enums.invoice.supplier.InvoiceApplySupplierReceiveInvoiceStatusEnum;
import cn.superdesk.uniorder.common.lamda.AnLamdaUtils;
import cn.superdesk.uniorder.common.security.dto.LoginSysUser;
import cn.superdesk.uniorder.dao.entity.invoice.customer.InvoiceApply;
import cn.superdesk.uniorder.dao.entity.invoice.supplier.InvoiceApplySupplier;
import cn.superdesk.uniorder.dto.param.invoice.supplier.status.InvoiceApplySupplierOpReceiveInvoiceStatusForm;
import cn.superdesk.uniorder.pattern.state.invoice.supplier.invoiceApplySupplier.receiveInvoiceStatus.state.InvoiceApplySupplierFieldReceiveInvoiceStatusState;
import cn.superdesk.uniorder.pattern.state.invoice.supplier.invoiceApplySupplier.receiveInvoiceStatus.state.concrete.InvoiceApplySupplierFieldReceiveInvoiceStatusConcreteState_0_未收票;
import cn.superdesk.uniorder.pattern.state.invoice.supplier.invoiceApplySupplier.receiveInvoiceStatus.state.concrete.InvoiceApplySupplierFieldReceiveInvoiceStatusConcreteState_1_已收票;
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 InvoiceApplySupplierFieldReceiveInvoiceStatusContext {

	private InvoiceApplySupplierFieldReceiveInvoiceStatusState state_0_未收票;
	private InvoiceApplySupplierFieldReceiveInvoiceStatusState state_1_已收票;

	private InvoiceApplySupplierFieldReceiveInvoiceStatusState state;

	private InvoiceApplySupplier entity;

	private InvoiceApplyDimEnum invoiceApplyDimEnum;

	private List<InvoiceApplySupplier>                     entitys;
	private InvoiceApplySupplierOpReceiveInvoiceStatusForm form;

	private LoginSysUser loginSysUser;


	public InvoiceApplySupplierFieldReceiveInvoiceStatusContext(InvoiceApplySupplier entity, LoginSysUser loginSysUser) {

		this.state_0_未收票 = new InvoiceApplySupplierFieldReceiveInvoiceStatusConcreteState_0_未收票(this);
		this.state_1_已收票 = new InvoiceApplySupplierFieldReceiveInvoiceStatusConcreteState_1_已收票(this);

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

		switch (InvoiceApplySupplierReceiveInvoiceStatusEnum.get(this.entity.getReceiveInvoiceStatus())) {

			case 未收票:
				state = state_0_未收票;
				break;
			case 已收票:
				state = state_1_已收票;
				break;

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

		}
	}

	public InvoiceApplySupplierFieldReceiveInvoiceStatusContext(List<InvoiceApplySupplier> entitys, InvoiceApplySupplierOpReceiveInvoiceStatusForm form, LoginSysUser loginSysUser) {

		this.state_0_未收票 = new InvoiceApplySupplierFieldReceiveInvoiceStatusConcreteState_0_未收票(this);
		this.state_1_已收票 = new InvoiceApplySupplierFieldReceiveInvoiceStatusConcreteState_1_已收票(this);

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

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

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

		InvoiceApplySupplierReceiveInvoiceStatusEnum switchEnum = InvoiceApplySupplierReceiveInvoiceStatusEnum.未收票;

		if (CollUtil.isEmpty(receiveInvoiceStatus)) {
			switchEnum = InvoiceApplySupplierReceiveInvoiceStatusEnum.未收票;
		}
		if (receiveInvoiceStatus.size() == 1) {
			switchEnum = InvoiceApplySupplierReceiveInvoiceStatusEnum.get(receiveInvoiceStatus.get(0));
		} else {
			switchEnum = InvoiceApplySupplierReceiveInvoiceStatusEnum.未收票;
		}

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

		switch (switchEnum) {

			case 未收票:
				state = state_0_未收票;
				break;
			case 已收票:
				state = state_1_已收票;
				break;

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

		}
	}

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

	// 已收票(1, "已收票"),
	public void opReceive() {
		state.opReceive();
	}


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