// 数据的操作，加减乘除
import { IRptTpl } from '../interface/basic';
import { get } from 'lodash';

import * as stringUtil from './stringUtil';
import commonConstants from './commonConstants';

// import StringUtil from './stringUtil';
// const stringUtil = new StringUtil();
// 为Date对象绑定转换日期的方法
stringUtil.setupDateFormat();
export class RptCommon {
  template!: IRptTpl;

  currentDataObj: any;

  initialize(rptTpl: IRptTpl, currentDataObj: any) {
    this.template = rptTpl;
    this.currentDataObj = currentDataObj;
  }

  multiply(val1: number[], val2: number[], fixFormat: number) {
    const rst = [];
    let maxLen = val1.length;
    let minLen = val2.length;
    if (minLen > maxLen) {
      maxLen += minLen;
      minLen = maxLen - minLen;
      maxLen -= minLen;
    }
    for (let i = 0; i < maxLen; i++) {
      let value =
        (i < val1.length ? val1[i] : val1[minLen - 1]) *
        (i < val2.length ? val2[i] : val2[minLen - 1]);
      if (value === null || value === undefined) {
        value = 0;
      }
      if (fixFormat) value = parseFloat(value.toFixed(fixFormat));
      rst.push(value);
    }
    return rst;
  }

  divide(val1: number[], val2: number[], fixFormat: number) {
    const rst = [];
    let maxLen = val1.length;
    let minLen = val2.length;
    if (minLen > maxLen) {
      maxLen += minLen;
      minLen = maxLen - minLen;
      maxLen -= minLen;
    }
    for (let i = 0; i < maxLen; i++) {
      let value =
        (i < val1.length ? val1[i] : val1[minLen - 1]) /
        (i < val2.length ? val2[i] : val2[minLen - 1]);
      if (fixFormat) value = parseFloat(value.toFixed(fixFormat));
      rst.push(value);
    }
    return rst;
  }

  plus(val1: number[], val2: number[], fixFormat: number) {
    const rst = [];
    let maxLen = val1.length;
    let minLen = val2.length;
    if (minLen > maxLen) {
      maxLen += minLen;
      minLen = maxLen - minLen;
      maxLen -= minLen;
    }
    for (let i = 0; i < maxLen; i++) {
      let value =
        (i < val1.length ? val1[i] : val1[minLen - 1]) +
        (i < val2.length ? val2[i] : val2[minLen - 1]);
      if (fixFormat) value = parseFloat(value.toFixed(fixFormat));
      rst.push(value);
    }
    return rst;
  }

  multiPlus(arrVal: number[][], fixFormat: number) {
    const rst = [];
    for (let i = 0; i < arrVal.length; i++) {
      const valItem = arrVal[i];
      if (i === 0) {
        for (const dtl of valItem) {
          let value = dtl;
          if (fixFormat) value = parseFloat(value.toFixed(fixFormat));
          rst.push(value);
        }
      } else {
        for (let j = 0; j < valItem.length; j++) {
          if (j < rst.length) {
            let value: number = rst[j] + valItem[j];
            if (fixFormat) value = parseFloat(value.toFixed(fixFormat));
            rst[j] = value;
          } else {
            let value = valItem[j];
            if (fixFormat) value = parseFloat(value.toFixed(fixFormat));
            rst.push(value);
          }
        }
      }
    }
    return rst;
  }

  minus(val1: number[], val2: number[], fixFormat: number) {
    const rst = [];
    let maxLen = val1.length;
    let minLen = val2.length;
    if (minLen > maxLen) {
      maxLen += minLen;
      minLen = maxLen - minLen;
      maxLen -= minLen;
    }
    for (let i = 0; i < maxLen; i++) {
      let value =
        (i < val1.length ? val1[i] : val1[minLen - 1]) -
        (i < val2.length ? val2[i] : val2[minLen - 1]);
      if (fixFormat) value = parseFloat(value.toFixed(fixFormat));
      rst.push(value);
    }
    return rst;
  }

  formatString(arrVal: string[], formatStr: string) {
    const rst = [];
    for (const val of arrVal) {
      rst.push(stringUtil.replaceAll(formatStr, '%S', val));
    }
    return rst;
  }

  getSupplyType(srcValArr: Record<string, number>[]) {
    const rst = [];
    for (const val of srcValArr) {
      let hasKey = false;
      const { supplyType } = commonConstants;
      for (const propKey in supplyType) {
        if (get(supplyType, propKey) === val) {
          hasKey = true;
          rst.push(get(supplyType, propKey));
          break;
        }
      }
      if (!hasKey) {
        rst.push('未知方式');
      }
    }
    return rst;
  }
}
