import { type ReactNode } from 'react';

import { type FileAsset } from '@material-hu/types/attachments';
import { type IconInterface } from '@material-hu/types/icons';

import { type TaskTypes } from '@material-hu/components/composed-components/learning';

import { type Attachment } from 'src/types/attachments';
import { type PaginationParams } from 'src/utils/pagination';

import { type LearningDuration } from '../common/types';

export enum FinishTaskErrors {
  SCORM_IN_PROGRESS = 'SCORM_TASK_STILL_IN_PROGRESS_ERROR',
}

export enum CourseTypes {
  REQUIRED = 'required',
  OPTIONAL = 'optional',
}

export enum CourseOrderBy {
  TITLE = 'TITLE',
  DUE_DATE = 'DUE_DATE',
  CREATED_AT = 'CREATED_AT',
  PROGRESS = 'PROGRESS',
  FINISHED_AT = 'FINISHED_AT',
  DEFAULT = 'DEFAULT_ORDER',
}

export enum CourseCategories {
  ALL = '-1',
}

export enum CourseSort {
  DUE_DATE_ASC = 'DUE_DATE/ASC',
  CREATED_AT_ASC = 'CREATED_AT/ASC',
  PROGRESS_DESC = 'PROGRESS/DESC',
}

// TYPES
export type CourseFilters = {
  type?: CourseTypes | '';
  sort?: CourseSort | '';
};

export type TaskLayoutForm = {
  finished: boolean;
  multimediaFinished: boolean;
};

export type Course = {
  id: number;
  instanceId: number;
  userId: number;
  title: string;
  body?: string;
  instructor?: string;
  modules: Module[];
  pictureUrl?: string;
  hasNewChanges: boolean;
  dueDate?: string;
  categoryTree: CourseCategory[];
  status: CourseStatus;
  progress: number;
  duration: number;
  durationTime: LearningDuration;
  category: CourseCategory;
  isObligatory: boolean;
  isSequential: boolean;
  hasSurvey: boolean;
  hasCompletedSurvey?: boolean;
  issueCertificate: boolean;
  passed?: boolean;
  certUrl?: string;
  averageGrade?: number;
  digitalSignature: boolean;
  learningPathIds: number[];
};

export enum CategoryStatus {
  ACTIVE = 'ACTIVE',
  INACTIVE = 'INACTIVE',
  DRAFT = 'DRAFT',
}

export type GetCourseCategoriesParams = PaginationParams & {
  status: CategoryStatus;
  includeCourses: boolean;
  sortingOption?: string;
};

export type CourseCategory = {
  id: number;
  name: string;
  status: CategoryStatus;
  children?: CourseCategory[];
};

export type CategoryFilterItem = {
  value: string;
  text: {
    title: string;
    withEllipsis: true;
    overflow: 'tooltip';
  };
  items?: CategoryFilterItem[];
  sideContent?: ReactNode;
};

export enum CourseCategorySortingOrder {
  NAME_ASC = 'NAME_ASC',
  NAME_DESC = 'NAME_DESC',
}

export type CategoryFilter = {
  categoryIds?: string;
  includeNoCategory?: boolean;
};

export type CourseListItem = {
  id: number;
  title: string;
  dueDate?: string;
  instanceId: number;
  isObligatory: boolean;
  duration: number;
  durationTime: LearningDuration;
  category: CourseCategory;
  pictureUrl?: string;
  progress: number;
  status: CourseStatus;
};

export type CourseList = PaginationParams &
  CategoryFilter &
  CourseFilters & {
    status: CourseStatus;
  };

export enum CourseStatus {
  ALL = 'ALL',
  PENDING = 'PENDING',
  FINISHED = 'FINISHED',
  IN_PROGRESS = 'IN_PROGRESS',
  PENDING_SIGNATURE = 'PENDING_SIGNATURE',
}

export type Module = {
  id: number;
  title: string;
  position: number;
  icon: IconInterface;
  tasks: Task[];
};

export enum TaskType {
  VIDEO = 'VIDEO',
  READING = 'READING',
  EVALUATION = 'EVALUATION',
  LESSON = 'LESSON',
  SCORM = 'SCORM',
}

export enum QuestionType {
  CHECKBOX = 'CHECKBOX',
  MULTIPLE_CHOICE = 'MULTIPLE_CHOICE',
  TEXT = 'TEXT',
  RATING = 'RATING',
}

export enum VideoTypes {
  YOUTUBE = 'EXTERNAL',
  INTERNAL = 'INTERNAL',
}

export enum ReadingTypes {
  MANUAL = 'MANUAL',
  PDF = 'PDF',
}

export enum EvaluationState {
  FINISHED = 'FINISHED',
  PENDING = 'PENDING',
  IN_PROGRESS = 'IN_PROGRESS',
}

export enum TaskStatus {
  PENDING = 'PENDING',
  IN_PROGRESS = 'IN_PROGRESS',
  FAILED = 'FAILED',
  FINISHED_OK = 'FINISHED_OK',
  FINISHED_FAIL = 'FINISHED_FAIL',
}

export enum TaskFinishedReason {
  NEXT = 'next',
  BACK = 'back',
  CLOSE = 'close',
  UNKNOWN = 'unknown',
}

export type PartialTask = {
  id: number;
  tag: string;
  title: string;
  iconUrl?: string;
  position: number;
  type: TaskTypes;
  description?: string;
  status: TaskStatus;
  attachments?: Attachment[];
  url?: string;
};

export type LessonTask = PartialTask & {
  body: string;
};

export type VideoTask = PartialTask & {
  urlType: VideoTypes;
};

export type ReadingTask = PartialTask & {
  body?: string;
};

export type ScormTask = PartialTask & {
  grade?: number;
};

export type EvaluationTask = PartialTask & {
  attempts?: number;
  maximumAttempts?: number;
  passingGrade?: number;
  grade?: number;
  duration: number;
  questionAmount: number;
  showCorrectAnswers: boolean;
  correctAnswerAmount?: number;
  wrongAnswerAmount?: number;
  questions: Question[];
  answers?: AnswerResult[];
  onlyTextQuestions?: boolean;
};

export type Choice = {
  value: string;
  checked: boolean;
};

export type Question = {
  id: string;
  title: string;
  description?: string;
  type: QuestionType;
  choices?: string[];
  attachments?: FileAsset[];
};

export type Task =
  | LessonTask
  | VideoTask
  | ReadingTask
  | EvaluationTask
  | ScormTask;

export type ToggleControls = {
  value: boolean;
  onChange: (value: boolean) => void;
  disabled: boolean;
};

export type EvaluationAnswer = {
  questionAnswers: Answer[];
};

export type Answer = {
  id: string;
  comment?: string;
  answer: string | string[];
};

export type AnswerResult = {
  id: string;
  title: string;
  answer: string | string[];
  choices: string[];
  correctAnswer: string[];
  desription?: string;
  isCorrectAnswers: boolean;
  type: QuestionType;
  explanation?: string;
  attachments?: FileAsset[];
};

export enum ChoiceResult {
  CORRECT = 'CORRECT',
  INCORRECT = 'INCORRECT',
  NOT_SELECTED = 'NOT_SELECTED',
  MISSING = 'MISSING',
}

export type InitCourseData = {
  courseId: number;
  task?: Task;
};

export type LessonNavigation = (
  courseId: number,
  taskId: number,
  tag: string,
) => void;

export enum StartCourseEventReason {
  NEW = 'new',
  RESUMED = 'resumed',
  FINISHED = 'finished',
}

export enum StartCourseEventAction {
  SELECT_TASK = 'selectTask',
  VIEW_COURSE = 'viewCourse',
}

export type FinishTaskResponse = {
  id: number;
  status: TaskStatus;
  taskId: number;
  taskTag: string;
};

export type SurveyFormResponse = Record<string, string | string[] | number>;

export type SatisfactionSurveyData = {
  contentRating: number;
  experienceRating: number;
  recommendation: boolean;
  comments?: string;
};

export type SurveyFormData = {
  questionAnswers: SurveyQuestionAnswer[];
};

export type SurveyQuestionAnswer = {
  id: string;
  comment?: string;
  answer: string | string[];
};

export type SurveyQuestion = {
  id: string;
  title: string;
  description?: string;
  type: QuestionType;
  required: boolean;
  choices: string[];
  points?: number;
};

export type SignCourseData = {
  signatureUrl: string;
};
