import { ApprovalWay, ProcessStatus, IProcess, ApprovalStatus, IProcedureProcess } from '@sc/types';
import { isEmpty } from 'lodash';

// 获取流程某个环节审批状态
export const getProcedureProcessStatus = (process: IProcedureProcess): ProcessStatus => {
  if (!process.participantInfo) {
    return ProcessStatus.WAITING;
  }
  const { approvalWay, executor } = process.participantInfo;
  const { reportTime, members } = executor;
  // 只有第一步骤（上报）才有reportTime（数据结构定死的）
  if (reportTime || !members) {
    return ProcessStatus.APPROVED;
  }
  // 指定用户 退回上一环节为上报审批时，判断流程状态和指定用户一样
  if (approvalWay === ApprovalWay.ACCOUNT || approvalWay === ApprovalWay.REPORT) {
    const { approvalStatus } = members[0];
    return approvalStatus as ProcessStatus;
  }
  // 会签 或者 依次审批
  if (approvalWay === ApprovalWay.JOINTLYSIGN || approvalWay === ApprovalWay.ORDERAPPROVAL) {
    // 只要有一个人审批回退，即失败
    if (members.some(member => member.approvalStatus === ProcessStatus.FAILED)) return ProcessStatus.FAILED;
    // 所有人都通过，即成功
    if (members.every(member => member.approvalStatus === ProcessStatus.APPROVED)) return ProcessStatus.APPROVED;
    // 所有人都等待, 即等待
    if (members.every(member => member.approvalStatus === ProcessStatus.WAITING)) return ProcessStatus.WAITING;
    // 否则正在进行
    return ProcessStatus.ACTIVATING;
  }
  // 或签
  if (approvalWay === ApprovalWay.ORSIGN) {
    // 只要有一个人审批回退，即失败
    if (members.some(member => member.approvalStatus === ProcessStatus.FAILED)) return ProcessStatus.FAILED;
    // 只要有一个人审批通过，即成功
    if (members.some(member => member.approvalStatus === ProcessStatus.APPROVED)) return ProcessStatus.APPROVED;
    // 所有人都等待, 即等待
    if (members.every(member => member.approvalStatus === ProcessStatus.WAITING)) return ProcessStatus.WAITING;
    // 否则正在进行
    return ProcessStatus.ACTIVATING;
  }
  // 默认等待
  return ProcessStatus.WAITING;
};

// 获取流程的状态
export const getProcedureApprovalStatus = (processes: IProcedureProcess[]): ApprovalStatus | undefined => {
  if (isEmpty(processes)) {
    return undefined;
  }
  const lastProcess = processes[processes.length - 1];
  const lastProcessStatus = getProcedureProcessStatus(lastProcess);
  // 审批通过
  if (lastProcessStatus === ProcessStatus.APPROVED) {
    return ApprovalStatus.APPROVED;
  }
  // 正在审批
  return ApprovalStatus.PROCESSING;
};

// 获取当前的环节（整个流程结束之后，当前环节为 null）
export const getProcedureCurrentProcess = (processes: IProcedureProcess[]) => {
  if (isEmpty(processes)) {
    return null;
  }
  // 第一个步骤是特殊的数据结构，因此直接区分处理
  if (processes.length === 1) {
    return processes[0];
  }
  for (let i = 1; i < processes.length; i += 1) {
    const process = processes[i];
    if (!process.participantInfo || !process.participantInfo.executor || !process.participantInfo.executor.members) {
      // eslint-disable-next-line no-continue
      continue;
    }
    const {
      participantInfo: {
        approvalWay,
        executor: { members },
      },
    } = process;
    // 指定用户 或 上报审批
    if (approvalWay === ApprovalWay.ACCOUNT || approvalWay === ApprovalWay.REPORT) {
      const { approvalStatus } = members[0];
      if (approvalStatus === ProcessStatus.ACTIVATING) {
        return processes[i];
      }
    }
    // 会签 或者 依次审批
    if (approvalWay === ApprovalWay.JOINTLYSIGN || approvalWay === ApprovalWay.ORDERAPPROVAL) {
      if (
        members.every(member => member.approvalStatus !== ProcessStatus.FAILED) &&
        members.some(member => member.approvalStatus === ProcessStatus.ACTIVATING)
      ) {
        return processes[i];
      }
    }
    // 或签
    if (approvalWay === ApprovalWay.ORSIGN) {
      if (members.every(member => member.approvalStatus === ProcessStatus.ACTIVATING)) {
        return processes[i];
      }
    }
  }
  return null;
};
