import { getMap, formatTreeData } from '@src/services/Tools';
import detail from './detail';
import search from './search';
import searchHistory from './searchHistory';
import { CourseModule } from '../../../Constant';
import { Main } from '../../stores/main';

export default [detail, search, searchHistory];


export function refreshQuestionType(compontent, subject, questionType, { all, needSentence, allSubject, excludeAwa }) {
  return Main.getExercise().then(result => {
    const list = result.filter(row => (needSentence ? true : row.isExamination)).map(row => {
      row.title = `${row.titleZh}${row.titleEn}`;
      row.key = row.extend;
      return row;
    });
    const tree = formatTreeData(list, 'id', 'title', 'parentId');
    compontent.questionSubjectMap = getMap(tree, 'key', 'children');
    compontent.questionSubjectSelect = tree.filter(row => row.level === 1 && (allSubject ? true : row.children.length > 1) && (excludeAwa ? row.extend !== 'awa' : true));
    if (all) {
      compontent.questionSubjectSelect.forEach(row => {
        row.children.unshift({
          title: '全部',
          key: '',
        });
      });
      compontent.questionSubjectSelect.unshift({
        title: '全部',
        key: '',
      });
    }
    compontent.setState({
      questionSubjectSelect: compontent.questionSubjectSelect,
      questionSubjectMap: compontent.questionSubjectMap,
    });
    return {
      questionTypes: questionType || (subject ? compontent.questionSubjectMap[subject].map(row => row.key).filter(row => row) : null),
    };
  });
}

export function refreshStruct(compontent, module, one, two, { all }) {
  switch (module) {
    case 'exercise':
      return Main.getExerciseAll().then(result => {
        const tmp = result.filter(row => row.level > 2).map(row => {
          row.title = `${row.titleZh}`;
          row.key = row.titleEn;
          return row;
        });
        const idsMap = getMap(tmp, 'id', 'key');
        const map = {};
        tmp.forEach(row => {
          if (!map[row.key]) {
            map[row.key] = {
              title: row.title,
              key: row.key,
              structIds: [],
              parentId: row.level > 3 ? idsMap[row.parentId] : null,
              subject: [],
              questionType: [],
            };
          }
          const item = map[row.key];
          item.structIds.push(row.id);
          if (item.subject.indexOf(row.subject) < 0) {
            item.subject.push(row.subject);
          }
          if (item.questionType.indexOf(row.questionType) < 0) {
            item.questionType.push(row.questionType);
          }
        });
        const list = Object.values(map);
        let courseModules = null;
        let structIds = null;
        if (one === 'preview') {
          if (!two) {
            courseModules = CourseModule.map(row => row.value);
          } else {
            courseModules = [two];
          }
        } else if (one) {
          const resultMap = getMap(list, 'key', 'structIds');
          if (!two) {
            structIds = resultMap[one];
          } else {
            structIds = resultMap[two];
          }
        }

        const tree = formatTreeData(list, 'key', 'title', 'parentId');
        const oneSelect = tree;
        const twoSelectMap = getMap(tree, 'key', 'children');
        if (all) {
          oneSelect.forEach(row => {
            row.children.unshift({
              title: '全部',
              key: '',
            });
          });
          oneSelect.unshift({
            title: '全部',
            key: '',
          });
        }
        compontent.setState({ oneSelect, twoSelectMap });

        return {
          structIds,
          courseModules,
        };
      });
    default:
      return Promise.resolve({});
  }
}