<script setup lang="ts">
import EvaluationSummary from "@/pages/cockpit/assignmentEvaluationPage/EvaluationSummary.vue";
import AssignmentIntroductionView from "@/pages/learningPath/learningContentPage/assignment/AssignmentIntroductionView.vue";
import AssignmentSubmissionView from "@/pages/learningPath/learningContentPage/assignment/AssignmentSubmissionView.vue";
import AssignmentTaskView from "@/pages/learningPath/learningContentPage/assignment/AssignmentTaskView.vue";
import LearningContentMultiLayout from "@/pages/learningPath/learningContentPage/layouts/LearningContentMultiLayout.vue";
import { useAssignmentStore } from "@/stores/assignmentStore";
import { useCourseSessionsStore } from "@/stores/courseSessions";
import { useUserStore } from "@/stores/user";
import type {
  Assignment,
  AssignmentTask,
  CourseSessionAssignmentDetails,
  CourseSessionUser,
  LearningContent,
} from "@/types";
import { useRouteQuery } from "@vueuse/router";
import dayjs from "dayjs";
import * as log from "loglevel";
import { computed, onMounted, reactive } from "vue";
import { useI18n } from "vue-i18n";

const { t } = useI18n();
const courseSessionsStore = useCourseSessionsStore();
const assignmentStore = useAssignmentStore();
const userStore = useUserStore();

interface State {
  assignment: Assignment | undefined;
  courseSessionAssignmentDetails: CourseSessionAssignmentDetails | undefined;
}

const state: State = reactive({
  assignment: undefined,
  courseSessionAssignmentDetails: undefined,
});

const props = defineProps<{
  assignmentId: number;
  learningContent: LearningContent;
}>();

// 0 = introduction, 1 - n = tasks, n+1 = submission
const stepIndex = useRouteQuery("step", "0", { transform: Number, mode: "push" });

const assignmentCompletion = computed(() => assignmentStore.assignmentCompletion);
const completionStatus = computed(() => {
  return assignmentCompletion.value?.completion_status ?? "in_progress";
});

onMounted(async () => {
  log.debug("AssignmentView mounted", props.assignmentId, props.learningContent);

  const courseSessionsStore = useCourseSessionsStore();

  try {
    state.assignment = await assignmentStore.loadAssignment(props.assignmentId);
    state.courseSessionAssignmentDetails = courseSessionsStore.findAssignmentDetails(
      props.learningContent.id
    );
    await assignmentStore.loadAssignmentCompletion(
      props.assignmentId,
      courseSessionId.value
    );

    if (
      stepIndex.value === 0 &&
      (completionStatus.value ?? "in_progress") !== "in_progress"
    ) {
      stepIndex.value = numPages.value - 1;
    }
  } catch (error) {
    log.error(error);
  }
});

const numTasks = computed(() => state.assignment?.tasks?.length ?? 0);
const numPages = computed(() => numTasks.value + 2);
const showPreviousButton = computed(() => stepIndex.value != 0);
const showNextButton = computed(() => stepIndex.value + 1 < numPages.value);
const showExitButton = computed(() => numPages.value === stepIndex.value + 1);
const dueDate = computed(() =>
  dayjs(state.courseSessionAssignmentDetails?.submissionDeadlineDateTimeUtc)
);
const courseSessionId = computed(
  () => courseSessionsStore.currentCourseSession?.id ?? 0
);
const currentTask = computed(() => {
  if (stepIndex.value > 0 && stepIndex.value <= numTasks.value) {
    return state.assignment?.tasks[stepIndex.value - 1];
  }
  return undefined;
});

const handleBack = () => {
  log.debug("handleBack");
  if (stepIndex.value > 0) {
    stepIndex.value -= 1;
  }
  log.debug(`pageIndex: ${stepIndex.value}`);
};

const handleContinue = () => {
  log.debug("handleContinue");
  if (stepIndex.value + 1 < numPages.value) {
    stepIndex.value += 1;
  }
  log.debug(`pageIndex: ${stepIndex.value}`);
};

const jumpToTask = (task: AssignmentTask) => {
  log.debug("jumpToTask", task);
  const index = state.assignment?.tasks.findIndex((t) => t.id === task.id);
  if (index && index >= 0) {
    stepIndex.value = index + 1;
  }
  log.debug(`pageIndex: ${stepIndex.value}`);
};

const getTitle = () => {
  if (0 === stepIndex.value) {
    return t("general.introduction");
  } else if (stepIndex.value === numPages.value - 1) {
    return t("general.submission");
  }
  return currentTask?.value?.value.title ?? "Unknown";
};

const assignmentUser = computed(() => {
  return (courseSessionsStore.currentCourseSession?.users ?? []).find(
    (user) => user.user_id === Number(userStore.id)
  ) as CourseSessionUser;
});
</script>

<template>
  <div v-if="state.assignment">
    <div class="flex">
      <LearningContentMultiLayout
        :current-step="stepIndex"
        :subtitle="state.assignment?.title ?? ''"
        :title="getTitle()"
        learning-content-type="assignment"
        :steps-count="numPages"
        :show-next-button="showNextButton"
        :show-exit-button="showExitButton"
        :show-start-button="false"
        :show-previous-button="showPreviousButton"
        :base-url="props.learningContent.frontend_url"
        step-query-param="step"
        start-badge-text="Einleitung"
        end-badge-text="Abgabe"
        close-button-variant="close"
        @previous="handleBack()"
        @next="handleContinue()"
      >
        <div class="flex">
          <div>
            <AssignmentIntroductionView
              v-if="stepIndex === 0 && state.assignment"
              :due-date="dueDate"
              :assignment="state.assignment!"
            ></AssignmentIntroductionView>
            <AssignmentTaskView
              v-if="currentTask"
              :task="currentTask"
              :assignment-id="props.assignmentId"
            ></AssignmentTaskView>
            <AssignmentSubmissionView
              v-if="stepIndex + 1 === numPages && state.assignment && courseSessionId"
              :due-date="dueDate"
              :assignment="state.assignment!"
              :assignment-completion-data="assignmentCompletion?.completion_data ?? {}"
              :course-session-id="courseSessionId!"
              @edit-task="jumpToTask($event)"
            ></AssignmentSubmissionView>
          </div>
        </div>
      </LearningContentMultiLayout>
      <div
        v-if="assignmentCompletion?.completion_status === 'evaluation_submitted'"
        class="min-w-2/5 mr-4 bg-gray-200 px-6 py-6"
      >
        <EvaluationSummary
          v-if="state.assignment"
          :assignment-user="assignmentUser"
          :assignment="state.assignment"
          :assignment-completion="assignmentCompletion"
          :show-evaluation-user="true"
        ></EvaluationSummary>
      </div>
    </div>
  </div>
</template>
