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

import cn.hutool.core.collection.CollUtil;
import cn.superdesk.uniorder.common.enums.invoice.customer.InvoiceApplyCustomerBackAccountStatusEnum;
import cn.superdesk.uniorder.common.enums.invoice.InvoiceApplyDimEnum;
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.OpBackAccountStatusForm;
import cn.superdesk.uniorder.pattern.state.invoice.customer.invoiceApply.backAccountStatus.state.InvoiceApplyFieldBackAccountStatusState;
import cn.superdesk.uniorder.pattern.state.invoice.customer.invoiceApply.backAccountStatus.state.concrete.InvoiceApplyFieldBackAccountStatusConcreteState_0_未回款;
import cn.superdesk.uniorder.pattern.state.invoice.customer.invoiceApply.backAccountStatus.state.concrete.InvoiceApplyFieldBackAccountStatusConcreteState_1_部分回款;
import cn.superdesk.uniorder.pattern.state.invoice.customer.invoiceApply.backAccountStatus.state.concrete.InvoiceApplyFieldBackAccountStatusConcreteState_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 InvoiceApplyFieldBackAccountStatusContext {

	private InvoiceApplyFieldBackAccountStatusState state_0_未回款;
	private InvoiceApplyFieldBackAccountStatusState state_1_部分回款;
	private InvoiceApplyFieldBackAccountStatusState state_2_已回款;

	private InvoiceApplyFieldBackAccountStatusState state;

	private InvoiceApply entity;

	private InvoiceApplyDimEnum invoiceApplyDimEnum;

	private List<InvoiceApply>      entitys;
	private OpBackAccountStatusForm form;

	private LoginSysUser loginSysUser;


	public InvoiceApplyFieldBackAccountStatusContext(InvoiceApply entity, LoginSysUser loginSysUser) {

		this.state_0_未回款 = new InvoiceApplyFieldBackAccountStatusConcreteState_0_未回款(this);
		this.state_1_部分回款 = new InvoiceApplyFieldBackAccountStatusConcreteState_1_部分回款(this);
		this.state_2_已回款 = new InvoiceApplyFieldBackAccountStatusConcreteState_2_已回款(this);

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

		switch (InvoiceApplyCustomerBackAccountStatusEnum.get(this.entity.getBackAccountStatus())) {

			case 未回款:
				state = state_0_未回款;
				break;
			case 部分回款:
				state = state_1_部分回款;
				break;
			case 已回款:
				state = state_2_已回款;
				break;

			default:
//				state = state_0_未回款;
				break;

		}
	}

	public InvoiceApplyFieldBackAccountStatusContext(List<InvoiceApply> entitys, OpBackAccountStatusForm form, LoginSysUser loginSysUser) {

		this.state_0_未回款 = new InvoiceApplyFieldBackAccountStatusConcreteState_0_未回款(this);
		this.state_1_部分回款 = new InvoiceApplyFieldBackAccountStatusConcreteState_1_部分回款(this);
		this.state_2_已回款 = new InvoiceApplyFieldBackAccountStatusConcreteState_2_已回款(this);


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

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

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

		InvoiceApplyCustomerBackAccountStatusEnum switchEnum = InvoiceApplyCustomerBackAccountStatusEnum.未回款;

		if (CollUtil.isEmpty(backAccountStatus)) {
			switchEnum = InvoiceApplyCustomerBackAccountStatusEnum.未回款;
		}
		if (backAccountStatus.size() == 1) {
			switchEnum = InvoiceApplyCustomerBackAccountStatusEnum.get(backAccountStatus.get(0));
		} else {
			switchEnum = InvoiceApplyCustomerBackAccountStatusEnum.未回款;
		}

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

		switch (switchEnum) {

			case 未回款:
				state = state_0_未回款;
				break;
			case 部分回款:
				state = state_1_部分回款;
				break;
			case 已回款:
				state = state_2_已回款;
				break;

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

		}
	}

	// 未回款(0, "未回款"),
	public void opInit() {
		state.opInit();
	}

	// 部分回款(1, "部分回款"),
	public void opPart() {
		state.opPart();
	}

	// 已回款(2, "已回款"),
	public void opComplete() {
		state.opComplete();
	}


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