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

// 获取流程某个环节审批状态
export const getProcessStatus = (process: IProcess) => {
  const { approvalWay, accounts } = process.participantInfo;
  // 指定用户 退回上一环节为上报审批时，判断流程状态和指定用户一样
  if (approvalWay === ApprovalWay.ACCOUNT || approvalWay === ApprovalWay.REPORT) {
    const { status } = accounts[0];
    return status;
  }
  // 会签 或者 依次审批
  if (approvalWay === ApprovalWay.JOINTLYSIGN || approvalWay === ApprovalWay.ORDERAPPROVAL) {
    // 只要有一个人审批回退，即失败
    if (accounts.some(account => account.status === ProcessStatus.FAILED)) return ProcessStatus.FAILED;
    // 所有人都通过，即成功
    if (accounts.every(account => account.status === ProcessStatus.APPROVED)) return ProcessStatus.APPROVED;
    // 所有人都等待, 即等待
    if (accounts.every(account => account.status === ProcessStatus.WAITING)) return ProcessStatus.WAITING;
    // 否则正在进行
    return ProcessStatus.ACTIVATING;
  }
  // 或签
  if (approvalWay === ApprovalWay.ORSIGN) {
    // 只要有一个人审批回退，即失败
    if (accounts.some(account => account.status === ProcessStatus.FAILED)) return ProcessStatus.FAILED;
    // 只要有一个人审批通过，即成功
    if (accounts.some(account => account.status === ProcessStatus.APPROVED)) return ProcessStatus.APPROVED;
    // 所有人都等待, 即等待
    if (accounts.every(account => account.status === ProcessStatus.WAITING)) return ProcessStatus.WAITING;
    // 否则正在进行
    return ProcessStatus.ACTIVATING;
  }
  // 默认等待
  return ProcessStatus.WAITING;
};

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

// 获取当前的环节（整个流程结束之后，当前环节为 null）
export const getCurrentProcess = (processes: IProcess[]) => {
  if (isEmpty(processes)) {
    return null;
  }
  for (let i = 0; i < processes.length; i += 1) {
    const {
      participantInfo: { approvalWay, accounts },
    } = processes[i];
    // 指定用户 或 上报审批
    if (approvalWay === ApprovalWay.ACCOUNT || approvalWay === ApprovalWay.REPORT) {
      const { status } = accounts[0];
      if (status === ProcessStatus.ACTIVATING) {
        return processes[i];
      }
    }
    // 会签 或者 依次审批
    if (approvalWay === ApprovalWay.JOINTLYSIGN || approvalWay === ApprovalWay.ORDERAPPROVAL) {
      if (
        accounts.every(account => account.status !== ProcessStatus.FAILED) &&
        accounts.some(account => account.status === ProcessStatus.ACTIVATING)
      ) {
        return processes[i];
      }
    }
    // 或签
    if (approvalWay === ApprovalWay.ORSIGN) {
      if (accounts.every(account => account.status === ProcessStatus.ACTIVATING)) {
        return processes[i];
      }
    }
  }
  return null;
};

// 获取某个环节的前一个环节
export const getPrevProcess = (processes: IProcess[], curProcess: IProcess) => {
  if (isEmpty(processes)) {
    return null;
  }
  for (let i = 1; i < processes.length; i += 1) {
    const process = processes[i];
    if (process.ID === curProcess.ID) {
      return processes[i - 1];
    }
  }
  return null;
};

// 获取某个环节的下一个环节
export const getNextProcess = (processes: IProcess[], curProcess: IProcess) => {
  if (isEmpty(processes)) {
    return null;
  }
  for (let i = 0; i < processes.length - 1; i += 1) {
    const process = processes[i];
    if (process.ID === curProcess.ID) {
      return processes[i + 1];
    }
  }
  return null;
};

// 获取上报环节
export const getReportProcess = (processes: IProcess[]) => {
  if (isEmpty(processes)) {
    return null;
  }
  return processes[0];
};

// 是否是上报环节
export const isReportProcess = (process: IProcess) => {
  return process.participantInfo && process.participantInfo.approvalWay === ApprovalWay.REPORT;
};
