import { Module } from "@/interfaces"
import { Difficulty } from "@/interfaces/exam"
import { sample } from "lodash"
import { ExamPart, ModuleState, SectionState } from "./types"
import { levelPart, listeningSection, readingPart, speakingTask, writingTask } from "@/stores/examEditor/sections"


export const defaultSettings = (module: Module) => {
	const baseSettings = {
		category: '',
		introOption: { label: 'None', value: 'None' },
		customIntro: '',
		currentIntro: '',
		topic: '',
		isCategoryDropdownOpen: false,
		isIntroDropdownOpen: false,
		isExerciseDropdownOpen: false,
		isTypeDropdownOpen: false,
	}

	switch (module) {
		case 'writing':
			return {
				...baseSettings,
				writingTopic: '',
				isWritingTopicOpen: false,
				isImageUploadOpen: false,
			}
		case 'reading':
			return {
				...baseSettings,
				isPassageOpen: false,
				readingTopic: '',
				isReadingTopicOpean: false,
			}
		case 'listening':
			return {
				...baseSettings,
				isAudioContextOpen: false,
				isAudioGenerationOpen: false,
				listeningTopic: '',
				isListeningTopicOpen: false,
			}
		case 'speaking':
			return {
				...baseSettings,
				speakingTopic: '',
				speakingSecondTopic: '',
				isSpeakingTopicOpen: false,
				isGenerateVideoOpen: false,
			}
		case 'level':
			return {
				...baseSettings,
				isReadingDropdownOpen: false,
				isWritingDropdownOpen: false,
				isSpeakingDropdownOpen: false,
				isListeningDropdownOpen: false,

				isWritingTopicOpen: false,
				isImageUploadOpen: false,
				writingTopic: '',

				isPassageOpen: false,
				readingTopic: '',
				isReadingTopicOpean: false,

				isAudioContextOpen: false,
				isAudioGenerationOpen: false,
				listeningTopic: '',
				isListeningTopicOpen: false,

				speakingTopic: '',
				speakingSecondTopic: '',
				isSpeakingTopicOpen: false,
				isGenerateVideoOpen: false,
			}
		default:
			return baseSettings;
	}
}

export const sectionLabels = (module: Module, levelParts?: number) => {
	switch (module) {
		case 'reading':
			return Array.from({ length: 3 }, (_, index) => ({
				id: index + 1,
				label: `Passage ${index + 1}`
			}));
		case 'writing':
			return [{ id: 1, label: "Task 1" }, { id: 2, label: "Task 2" }];
		case 'speaking':
			return [{ id: 1, label: "Speaking 1" }, { id: 2, label: "Speaking 2" }, { id: 3, label: "Interactive Speaking" }];
		case 'listening':
			return Array.from({ length: 4 }, (_, index) => ({
				id: index + 1,
				label: `Section ${index + 1}`
			}));
		case 'level':
			return levelParts !== undefined ?
				Array.from({ length: levelParts }, (_, index) => ({
					id: index + 1,
					label: `Part ${index + 1}`
				}))
				:
				[{ id: 1, label: "Part 1" }];
	}
}

const defaultExamLabel = (module: Module) => {
	switch (module) {
		case 'reading':
			return "Reading Exam";
		case 'writing':
			return "Writing Exam";
		case 'speaking':
			return "Speaking Exam";
		case 'listening':
			return "Listening Exam";
		case 'level':
			return "Placement Test";
	}
}

const defaultSection = (module: Module, sectionId: number) => {
	switch (module) {
		case 'reading':
			return readingPart(sectionId);
		case 'writing':
			return writingTask(sectionId);
		case 'listening':
			return listeningSection(sectionId)
		case 'speaking':
			return speakingTask(sectionId)
		case 'level':
			return levelPart(sectionId)
	}
}

export const defaultSectionSettings = (module: Module, sectionId: number, part?: ExamPart) => {
	return {
		sectionId: sectionId,
		settings: defaultSettings(module),
		state: part !== undefined ? part : defaultSection(module, sectionId),
		generating: undefined,
		genResult: undefined,
		focusedExercise: undefined,
		expandedSubSections: [],
		levelGenerating: [],
		levelGenResults: [],
		scriptLoading: false,
	}
}


const defaultModuleSettings = (module: Module, minTimer: number, reset: boolean = false): ModuleState => {
	const state: ModuleState = {
		examLabel: defaultExamLabel(module),
		minTimer,
		difficulty: [sample(["A1", "A2", "B1", "B2", "C1", "C2"] as Difficulty[])!],
		access: "private",
		sectionLabels: sectionLabels(module),
		expandedSections: [(reset && (module === "writing" || module === "speaking")) ? 0 : 1],
		focusedSection: 1,
		sections: [defaultSectionSettings(module, 1)],
		importModule: true,
		importing: false,
		edit: [],
		instructionsState: {
			isInstructionsOpen: false,
			chosenOption: { value: "Automatic", label: "Automatic" },
			currentInstructions: "",
			presetInstructions: "",
			customInstructions: "",
			currentInstructionsURL: "",
			presetInstructionsURL: "",
			customInstructionsURL: "",
		},
	};
	if (["reading", "writing"].includes(module)) {
		state["type"] = "general";
	}
	return state;
}

export default defaultModuleSettings;
