import { Module } from "@/interfaces";
import { defaultSectionSettings } from "../defaults";
import { reorderModule } from "../reorder/global";
import ExamEditorStore, { ModuleState } from "../types";

export type ModuleActions =
    | { type: 'UPDATE_MODULE'; payload: { updates: Partial<ModuleState>, module?: Module } }
    | { type: 'TOGGLE_SECTION'; payload: { sectionId: number; } }
    | { type: 'RESET_FOCUS'; }
    | { type: 'REORDER_EXERCISES' };

export const MODULE_ACTIONS = [
    'UPDATE_MODULE',
    'TOGGLE_SECTION',
    'RESET_FOCUS',
    'REORDER_EXERCISES'
];

export const moduleReducer = (
    state: ExamEditorStore,
    action: ModuleActions
): Partial<ExamEditorStore> => {
    const currentModule = state.currentModule;
    const currentModuleState = state.modules[currentModule];
    const expandedSections = currentModuleState.expandedSections;

    switch (action.type) {
        case 'UPDATE_MODULE':
            const { updates, module } = action.payload;
            if (module === undefined) {
                return {
                    modules: {
                        ...state.modules,
                        [currentModule]: {
                            ...currentModuleState,
                            ...updates
                        }
                    }
                };
            } else {
                return {
                    modules: {
                        ...state.modules,
                        [module]: {
                            ...state.modules[module],
                            ...updates
                        }
                    }
                };
            }

        case 'TOGGLE_SECTION':
            const { sectionId } = action.payload;

            const prev = currentModuleState.sections;
            const updatedSections = prev.some(section => section.sectionId === sectionId)
                ? prev.filter(section => section.sectionId !== sectionId)
                : [
                    ...prev,
                    defaultSectionSettings(currentModule, sectionId)
                ];

            const updatedCheckedSections = expandedSections.includes(sectionId)
                ? expandedSections.filter(i => i !== sectionId)
                : [...expandedSections, sectionId];

            return {
                modules: {
                    ...state.modules,
                    [currentModule]: {
                        ...currentModuleState,
                        sections: updatedSections,
                        expandedSections: updatedCheckedSections
                    }
                }
            };

        case 'RESET_FOCUS': {
            const currentFocus = currentModuleState.focusedSection;
            const updatedSections = expandedSections?.filter(id => id != currentFocus);

            return {
                modules: {
                    ...state.modules,
                    [currentModule]: {
                        ...currentModuleState,
                        expandedSections: updatedSections,
                        focusedSection: updatedSections[0]
                    }
                }
            }
        }

        case 'REORDER_EXERCISES': {
            if (currentModule === "writing" || currentModule === "speaking") return state;
            return {
                ...state,
                modules: {
                    ...state.modules,
                    [currentModule]: reorderModule(currentModuleState)
                }
            };
        }
        default:
            return {};
    }
};