import { IJobContent, IItemCharacter, IStdBill, IStdJobContent, IStdItemCharacter } from '@sc/types';

interface IJobsAndCharacterText {
  jobContentText: string;
  itemCharacterText: string;
  onlyItemCharacterText: string;
  onlyJobContentText: string;
}

interface IJobContentMap {
  [id: string]: IStdJobContent;
}
interface IItemCharacterMap {
  [id: string]: IStdItemCharacter;
}

export interface IJobAndCharacter extends IJobsAndCharacterText {
  jobContents: IJobContent[];
  itemCharacters: IItemCharacter[];
}

export const getJobsAndCharacterText = (
  jobContents: IJobContent[],
  itemCharacters: IItemCharacter[],
  divided = false // 分别对应
): IJobsAndCharacterText => {
  let jobContentText = '';
  let itemCharacterText = '';
  let onlyJobContentText = '';
  let onlyItemCharacterText = '';
  jobContents.sort((a, b) => a.seq - b.seq);
  itemCharacters.sort((a, b) => a.seq - b.seq);
  const characterTextArray: string[] = divided ? [] : ['[项目特征]'];
  let i = 1;
  for (const item of itemCharacters) {
    if (item.isChecked) characterTextArray.push(`${i}. ${item.character || ''}: ${item.eigenvalue || ''}`);
    i += 1;
  }
  onlyItemCharacterText = characterTextArray.join('\n');

  const jobTextArray = divided ? [] : ['[工作内容]'];
  i = 1;
  for (const job of jobContents) {
    if (job.isChecked) jobTextArray.push(`${i}. ${job.content || ''}`);
    i += 1;
  }
  onlyJobContentText = jobTextArray.join('\n');
  jobContentText = divided ? onlyJobContentText : '';
  itemCharacterText = divided ? onlyItemCharacterText : `${onlyItemCharacterText}\n${onlyJobContentText}`;
  return { jobContentText, itemCharacterText, onlyItemCharacterText, onlyJobContentText };
};

export const getJobContents = (stdBill: IStdBill, stdJobContentMap: IJobContentMap): IJobContent[] => {
  const jobs: IJobContent[] = [];
  let i = 1;
  for (const j of stdBill.jobs) {
    const job = stdJobContentMap[j.id];
    if (job) {
      jobs.push({ seq: i + 1, content: job.content, isChecked: true });
      i += 1;
    }
  }

  return jobs;
};

export const getItemCharacters = (stdBill: IStdBill, itemCharacterMap: IItemCharacterMap): IItemCharacter[] => {
  const characters: IItemCharacter[] = [];
  let i = 1;
  for (const item of stdBill.items) {
    const character = itemCharacterMap[item.id];
    if (character) {
      const newItem: IItemCharacter = {
        seq: i + 1,
        character: character.content,
        eigenvalueList: [],
        eigenvalue: '',
        isChecked: false,
      };
      const eigenvalues = character.itemValue;
      for (let j = 0; j < eigenvalues.length; j += 1) {
        newItem.eigenvalueList.push(eigenvalues[j].value);
      }
      characters.push(newItem);
      i += 1;
    }
  }

  return characters;
};

// 对于含有中文的清单编号，分离出中文和数字
export const separateBillCode = (code: string) => {
  const result = code.match(/^([\u4e00-\u9fa5]+)(\d+)/);
  if (result && result.length === 3) {
    const pre = result[1];
    const subCode = result[2];
    return { pre, code: subCode };
  }
  return { pre: '', code };
};

/**
 *
 * @param sourceCode 清单ID
 *
 * 返回： isStd是否来自标准清单  code:标准清单编号
 */
export const matchAndGetStdBillCode = (sourceCode: string) => {
  const { pre, code } = separateBillCode(sourceCode);
  if (code.length === 12) {
    return { isStd: true, code: pre + code.substring(0, 9) };
  }
  if (code.length === 9) {
    return { isStd: true, code: pre + code };
  }
  return { isStd: false, code: sourceCode };
};

// 取标准清单编号
export const getStdBillCode = (sourceCode: string) => {
  const { isStd, code } = matchAndGetStdBillCode(sourceCode);
  if (isStd) {
    return code;
  }
  return '';
};
