package cn.superdesk.uniorder.pattern.state.invoice.customer.invoiceApply.receiveInvoiceStatus.context;

import cn.hutool.core.collection.CollUtil;
import cn.superdesk.uniorder.common.enums.invoice.InvoiceApplyDimEnum;
import cn.superdesk.uniorder.common.enums.invoice.customer.InvoiceApplyCustomerReceiveInvoiceStatusEnum;
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.dto.param.invoice.customer.status.InvoiceApplyCustomerOpReceiveInvoiceStatusForm;
import cn.superdesk.uniorder.pattern.state.invoice.customer.invoiceApply.receiveInvoiceStatus.state.InvoiceApplyFieldReceiveInvoiceStatusState;
import cn.superdesk.uniorder.pattern.state.invoice.customer.invoiceApply.receiveInvoiceStatus.state.concrete.InvoiceApplyFieldReceiveInvoiceStatusConcreteState_0_未领票;
import cn.superdesk.uniorder.pattern.state.invoice.customer.invoiceApply.receiveInvoiceStatus.state.concrete.InvoiceApplyFieldReceiveInvoiceStatusConcreteState_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 InvoiceApplyFieldReceiveInvoiceStatusContext {

	private InvoiceApplyFieldReceiveInvoiceStatusState state_0_未领票;
	private InvoiceApplyFieldReceiveInvoiceStatusState state_1_已领票;

	private InvoiceApplyFieldReceiveInvoiceStatusState state;

	private InvoiceApply entity;

	private InvoiceApplyDimEnum invoiceApplyDimEnum;

	private List<InvoiceApply>                             entitys;
	private InvoiceApplyCustomerOpReceiveInvoiceStatusForm form;

	private LoginSysUser loginSysUser;


	public InvoiceApplyFieldReceiveInvoiceStatusContext(InvoiceApply entity, LoginSysUser loginSysUser) {

		this.state_0_未领票 = new InvoiceApplyFieldReceiveInvoiceStatusConcreteState_0_未领票(this);
		this.state_1_已领票 = new InvoiceApplyFieldReceiveInvoiceStatusConcreteState_1_已领票(this);

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

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

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

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

		}
	}

	public InvoiceApplyFieldReceiveInvoiceStatusContext(List<InvoiceApply> entitys, InvoiceApplyCustomerOpReceiveInvoiceStatusForm form, LoginSysUser loginSysUser) {

		this.state_0_未领票 = new InvoiceApplyFieldReceiveInvoiceStatusConcreteState_0_未领票(this);
		this.state_1_已领票 = new InvoiceApplyFieldReceiveInvoiceStatusConcreteState_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(InvoiceApply::getReceiveInvoiceStatus).collect(Collectors.toList());

		InvoiceApplyCustomerReceiveInvoiceStatusEnum switchEnum = InvoiceApplyCustomerReceiveInvoiceStatusEnum.未领票;

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

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

		switch (switchEnum) {

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

			default:
//				state = state_3_待提交;
				break;

		}
	}

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

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


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