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

import cn.hutool.core.collection.CollUtil;
import cn.superdesk.uniorder.common.enums.invoice.InvoiceApplyDimEnum;
import cn.superdesk.uniorder.common.enums.invoice.customer.InvoiceApplyCustomerMailTickStatusEnum;
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.OpMailTickStatusFormV2_1;
import cn.superdesk.uniorder.pattern.state.invoice.customer.invoiceApply.mailTickStatus.state.InvoiceApplyFieldMailTickStatusState;
import cn.superdesk.uniorder.pattern.state.invoice.customer.invoiceApply.mailTickStatus.state.concrete.InvoiceApplyFieldMailTickStatusConcreteState_0_未邮寄;
import cn.superdesk.uniorder.pattern.state.invoice.customer.invoiceApply.mailTickStatus.state.concrete.InvoiceApplyFieldMailTickStatusConcreteState_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 InvoiceApplyFieldMailTickStatusContext {

	private InvoiceApplyFieldMailTickStatusState state_0_未邮寄;
	private InvoiceApplyFieldMailTickStatusState state_1_已邮寄;

	private InvoiceApplyFieldMailTickStatusState state;

	private InvoiceApply entity;

	private InvoiceApplyDimEnum invoiceApplyDimEnum;

	private List<InvoiceApply>       entitys;
	private OpMailTickStatusFormV2_1 form;

	private LoginSysUser loginSysUser;


	public InvoiceApplyFieldMailTickStatusContext(InvoiceApply entity, LoginSysUser loginSysUser) {

		this.state_0_未邮寄 = new InvoiceApplyFieldMailTickStatusConcreteState_0_未邮寄(this);
		this.state_1_已邮寄 = new InvoiceApplyFieldMailTickStatusConcreteState_1_已邮寄(this);

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

		switch (InvoiceApplyCustomerMailTickStatusEnum.get(this.entity.getMailTickStatus())) {

			case 未寄票:
				state = state_0_未邮寄;
				break;
			case 已寄票:
				state = state_1_已邮寄;
				break;

			default:
//				state = state_0_未邮寄;
				break;

		}
	}

	public InvoiceApplyFieldMailTickStatusContext(List<InvoiceApply> entitys, OpMailTickStatusFormV2_1 form, LoginSysUser loginSysUser) {

		this.state_0_未邮寄 = new InvoiceApplyFieldMailTickStatusConcreteState_0_未邮寄(this);
		this.state_1_已邮寄 = new InvoiceApplyFieldMailTickStatusConcreteState_1_已邮寄(this);

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

		/****************************************** 异常就按 未邮寄 逻辑 start ******************************************/

		List<Integer> mailTickStatus = entitys.stream().filter(AnLamdaUtils.distinctByKey(o -> o.getMailTickStatus())).map(InvoiceApply::getMailTickStatus).collect(Collectors.toList());

		InvoiceApplyCustomerMailTickStatusEnum switchEnum = InvoiceApplyCustomerMailTickStatusEnum.未寄票;

		if (CollUtil.isEmpty(mailTickStatus)) {
			switchEnum = InvoiceApplyCustomerMailTickStatusEnum.未寄票;
		}
		if (mailTickStatus.size() == 1) {
			switchEnum = InvoiceApplyCustomerMailTickStatusEnum.get(mailTickStatus.get(0));
		} else {
			switchEnum = InvoiceApplyCustomerMailTickStatusEnum.未寄票;
		}

		/****************************************** 异常就按 未邮寄 逻辑   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 opComplete() {
		state.opComplete();
	}


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