import {
  stemTypeList,
  scoreTypeList,
  questionNumberTypeList,
  optionTypeList,
  switchOption,
  fontSizeList,
} from './common';
import { getRandomNumber } from '@/utils/index';
import { handleChineseStrokes, handleItemPinyin } from './chinese';

export function getOption(content = '') {
  return {
    content,
    mark: getRandomNumber(),
    audio_file_id: '',
    pinyin: '',
    definition: '',
    collocation: '',
    example_sentence: ['', ''],
    picture_file_id: '',
    hz_strokes_list: [],
  };
}

// 音频生成方式类型
export const audioGenerationMethodList = [
  {
    value: 'upload',
    label: '上传',
  },
  {
    value: 'auto',
    label: '自动生成',
  },
  {
    value: 'record',
    label: '录音',
  },
];

// 修改拼音
function changePinyin(item) {
  let index = item.pinyin.search(/0|1|2|3|4/);
  if (index > -1) {
    let matically_pinyin_obj = {};
    handleItemPinyin(item.pinyin, item.mark, matically_pinyin_obj);
    setTimeout(() => {
      item.pinyin = matically_pinyin_obj[item.mark];
    }, 100);
  }
}

/**
 * 解析智能识别数据
 * @param {array} arr 智能识别数据
 * @returns object
 */
export async function analysisRecognitionWordCardData(arr) {
  let option_list = await Promise.all(
    arr.map(async (content) => {
      let content_item = content.split('/');
      let option = {
        content: content_item[0] ? content_item[0] : '',
        mark: getRandomNumber(),
        audio_file_id: '',
        pinyin: content_item[1] ? content_item[1] : '',
        definition: content_item[2] ? content_item[2] : '',
        collocation: content_item[3] ? content_item[3] : '',
        example_sentence: content_item.length > 3 ? content_item.slice(4) : [''],
        picture_file_id: '',
        hz_strokes_list: [],
      };

      await handleChineseStrokes(option);
      changePinyin(option);
      return option;
    }),
  );

  return {
    'data.option_list': option_list,
  };
}

// 字词卡片数据模板
export const wordCardData = {
  type: 'word_card', // 题型
  stem: '', // 题干
  description: '', // 描述
  option_number_show_mode: optionTypeList[1].value, // 选项类型
  answer: { score: 1, score_type: scoreTypeList[0].value }, // 答案
  option_list: [getOption()], // 选项
  file_id_list: [],
  // 题型属性
  property: {
    stem_type: stemTypeList[1].value, // 题干类型
    question_number: '1', // 题号
    stem_question_number_font_size: fontSizeList[6], // 题干题号
    is_enable_description: switchOption[1].value, // 描述
    score: 1, // 分值
    score_type: scoreTypeList[0].value, // 分值类型
  },
  // 其他属性
  other: {
    question_number_type: questionNumberTypeList[1].value, // 题号类型
    audio_generation_method: audioGenerationMethodList[0].value, // 音频生成方式
  },
};
