import { IRationAss, IThirdRation } from '@sc/types';
import { ceil, floor, round, sortBy } from 'lodash';
import { roundForObj } from '@sc/util';

export interface IAssTime {
  times: number;
  ass: IRationAss | IThirdRation;
}

export interface IStateList {
  index: number;
  content: string;
  addCode?: string; // 添加工料机的编号，用来标记，如果是子目换算中的单个人材机生成的，改成 调：...
}

export const stateSeq = {
  ass: 1,
  proportion: 2,
  replace: 3,
  coe: 4,
  add: 5,
  cusQuantity: 6,
  cusCoe: 7,
  area: 8,
  adjMak: 9,
};
// 处理多个辅助定额的情况
export const handleMulAss = (rationAssList: IRationAss[]) => {
  const assList = [];
  for (const ass of rationAssList) {
    // 处理多个辅助定额的情况
    if (ass.groupList && ass.groupList.length > 0) {
      const newAss = { ...ass };
      const newList = sortBy(ass.groupList, item => parseFloat(item.param || '0')); // 按参数排序
      let pre = 0;
      for (const tem of newList) {
        if (ass.actualValue) {
          if (ass.actualValue > pre && ass.actualValue <= parseFloat(tem.param || '0')) {
            // 落在中间，则用组里的这条定额
            newAss.param = tem.param;
            newAss.paramName = tem.paramName;
            newAss.assistCode = tem.assistCode;
            break;
          }
          pre = parseFloat(tem.param || '0');
        }
      }
      assList.push(newAss);
    } else {
      assList.push(ass);
    }
  }
  return assList;
};

export const calcAssTime = (ass: IRationAss) => {
  if (ass.isAdjust === false) return undefined;
  const actualValue = ass.actualValue ? ass.actualValue : 0;
  const stdValue = ass.stdValue ? parseFloat(ass.stdValue) : 0;
  const stepValue = ass.stepValue ? parseFloat(ass.stepValue) : 1;
  let times = (actualValue - stdValue) / stepValue;
  let r = false; // 是否负数
  if (times < 0) {
    r = true;
    times *= -1;
  }

  if (ass.carryBit === '四舍五入') {
    times = round(times, ass.decimal);
  } else if (ass.carryBit === '进一') {
    times = ceil(times, ass.decimal);
  } else if (ass.carryBit === '舍一') {
    times = floor(times, ass.decimal);
  }
  if (r) {
    times *= -1;
  }
  return roundForObj(times, 6);
};

export const calcRationAssTimes = (
  rationAssList?: IRationAss[],
  thirdRation?: IThirdRation,
  stateList?: IStateList[]
) => {
  const timeList: IAssTime[] = [];
  if (rationAssList) {
    const assList = handleMulAss(rationAssList);
    const temTimes = [];
    const thirdRationCodes = [];
    for (const rationAss of assList) {
      const times = calcAssTime(rationAss);
      if (times) {
        const { thirdRationCode } = rationAss;
        if (thirdRationCode) {
          temTimes.push(times);
          thirdRationCodes.push(thirdRationCode);
        }
        timeList.push({ times, ass: rationAss });
        if (stateList)
          stateList.push({
            index: stateSeq.ass,
            content: `${rationAss.name} ${rationAss.actualValue} : ${rationAss.assistCode}x${times}`,
          });
      }
    }
    if (thirdRation && temTimes.length === 2 && thirdRationCodes[0] === thirdRationCodes[1]) {
      // 说明有第三定额
      const timesT = temTimes[0] * temTimes[1];
      timeList.push({ times: timesT, ass: thirdRation });
      if (stateList) stateList.push({ index: stateSeq.ass, content: `+${thirdRationCodes[0]}x${timesT}` });
    }
  }

  return timeList;
};
