import { test } from "@/api";

class TestModel {
	/** 根据不同类型获取分类 */
	async getTopicClass(type: "df" | "fl" | "jx" | "sx", params: Partial<Test.getTopicClassParams>) {
		switch (type) {
			case "df": {
				const res = await test.selectDfQuestionInfo(params);
				return res.data;
			}
			case "fl": {
				const res = await test.selectFlQustionInfo(params);
				return res.data;
			}
			case "jx": {
				const res = await test.selectJxQustionInfo(params);
				return res.data;
			}
			case "sx": {
				const res = await test.selectSxQustionInfo(params);
				return res.data;
			}
		}
	}

	/**获取用户答案初始值 */
	private getUserAnswer = (type: string) => {
		switch (type) {
			case "判断题":
				return null;
			case "单选题":
				return null;
			case "多选题":
				return [];
		}
	};

	/**获取题目类型 */
	private getType = (answer: Array<string>) => {
		if (answer.length === 1) {
			if (["√", "×"].includes(answer[0])) {
				return "判断题";
			} else {
				return "单选题";
			}
		} else {
			return "多选题";
		}
	};

	/** 获取题目列表 */
	async getList(params: Test.listParams) {
		const res = await test.getList(params);

		const data = {
			total: res.data.total,
			list: res.data.rows.map((item) => {
				return {
					...item,
					explain: item.issue,
					opts: item.opts.split("-").filter((item) => !!item),
					image: item.image,
					type: this.getType(item.answer.split("-")),
					answer: item.answer,
					userAnswer: this.getUserAnswer(this.getType(item.answer.split("-"))),
					isTrue: null,
					isCollection: false,
				};
			}),
		};

		return data;
	}

	/** 获取免费题目列表 */
	async getFreeList(params: Test.listParams) {
		const res = await test.getFreeList(params);

		const data = {
			total: res.data.total,
			list: res.data.rows.map((item) => {
				return {
					...item,
					explain: item.issue,
					opts: item.opts.split("-").filter((item) => !!item),
					image: item.image,
					type: this.getType(item.answer.split("-")),
					answer: item.answer,
					userAnswer: this.getUserAnswer(this.getType(item.answer.split("-"))),
					isTrue: null,
					isCollection: false,
				};
			}),
		};

		return data;
	}

	/**获取模拟考试题列表 */
	async getMockList(params: Test.listParams) {
		const res = await test.getMockList(params);

		const data = {
			total: res.data.total,
			list: res.data.rows.map((item) => {
				return {
					...item,
					explain: item.issue,
					opts: item.opts.split("-").filter((item) => !!item),
					image: item.image,
					type: this.getType(item.answer.split("-")),
					answer: item.answer,
					userAnswer: this.getUserAnswer(this.getType(item.answer.split("-"))),
					isTrue: null,
					isCollection: false,
				};
			}),
		};

		return data;
	}
}

export default new TestModel();
