import { DragEndEvent } from "@dnd-kit/core";
import { WriteBlanksExercise, TrueFalseExercise, MatchSentencesExercise, MultipleChoiceExercise } from "@/interfaces/exam";
import getMinExerciseId from "./minIds";
import { parseText, reconstructText } from "@/components/ExamEditor/Exercises/WriteBlanks/parsing";
import { arrayMove } from "@dnd-kit/sortable";



const handleWriteBlanksReorder = (event: DragEndEvent, exercise: WriteBlanksExercise) => {
    const { active, over } = event;
    if (!over || active.id === over.id) return exercise;

    const oldIndex = exercise.solutions.findIndex(s => s.id === active.id);
    const newIndex = exercise.solutions.findIndex(s => s.id === over.id);

    const newSolutions = [...exercise.solutions];
    const [movedItem] = newSolutions.splice(oldIndex, 1);
    newSolutions.splice(newIndex, 0, movedItem);

    const questions = parseText(exercise.text);

    const reorderedQuestions = questions.map((q, i) => {
        const solutionIndex = questions.findIndex(origQ => origQ.id === newSolutions[i].id);
        return questions[solutionIndex];
    });

    const minId = getMinExerciseId(exercise);
    const finalSolutions = newSolutions.map((solution, index) => ({
        ...solution,
        id: (minId + index).toString()
    }));

    const finalQuestions = reorderedQuestions.map((q, index) => ({
        ...q,
        id: (minId + index).toString()
    }));

    const newText = reconstructText(finalQuestions);

    return {
        ...exercise,
        solutions: finalSolutions,
        text: newText
    };
};


const handleTrueFalseReorder = (event: DragEndEvent, exercise: TrueFalseExercise) => {
    const { active, over } = event;
    if (!over || active.id === over.id) return exercise;

    const oldIndex = exercise.questions.findIndex(q => q.id === active.id);
    const newIndex = exercise.questions.findIndex(q => q.id === over.id);

    const newQuestions = [...exercise.questions];
    const [movedItem] = newQuestions.splice(oldIndex, 1);
    newQuestions.splice(newIndex, 0, movedItem);

    const minId = getMinExerciseId(exercise);
    const reorderedQuestions = newQuestions.map((question, index) => ({
        ...question,
        id: (minId + index).toString()
    }));

    return {
        ...exercise,
        questions: reorderedQuestions
    };
};

const handleMatchSentencesReorder = (event: DragEndEvent, exercise: MatchSentencesExercise) => {
    const { active, over } = event;
    if (!over || active.id === over.id) return exercise;

    const oldIndex = exercise.sentences.findIndex(s => s.id === active.id);
    const newIndex = exercise.sentences.findIndex(s => s.id === over.id);

    const newSentences = [...exercise.sentences];
    const [movedItem] = newSentences.splice(oldIndex, 1);
    newSentences.splice(newIndex, 0, movedItem);

    const minId = getMinExerciseId(exercise);
    const reorderedSentences = newSentences.map((sentence, index) => ({
        ...sentence,
        id: (minId + index).toString()
    }));

    return {
        ...exercise,
        sentences: reorderedSentences
    };
};

const handleMultipleChoiceReorder = (event: DragEndEvent, exercise: MultipleChoiceExercise): MultipleChoiceExercise => {
    const { active, over } = event;

    if (!over || active.id === over.id) {
        return exercise;
    }

    const oldIndex = exercise.questions.findIndex(
        question => question.id === active.id
    );
    const overIndex = exercise.questions.findIndex(
        question => question.id === over.id
    );

    if (oldIndex === -1 || overIndex === -1) {
        return exercise;
    }

    const reorderedQuestions = arrayMove(exercise.questions, oldIndex, overIndex);
    const minId = Math.min(...exercise.questions.map(q => parseInt(q.id)));
    const updatedQuestions = reorderedQuestions.map((question, index) => ({
        ...question,
        id: (minId + index).toString()
    }));

    return {
        ...exercise,
        questions: updatedQuestions
    };
};

export {
    handleWriteBlanksReorder,
    handleTrueFalseReorder,
    handleMatchSentencesReorder,
    handleMultipleChoiceReorder,
};
