import AutoStoriesOutlined from '@material-hu/icons/material/AutoStoriesOutlined';
import BookOutlined from '@material-hu/icons/material/BookOutlined';
import EmojiEventsOutlined from '@material-hu/icons/material/EmojiEventsOutlined';
import FolderZipOutlined from '@material-hu/icons/material/FolderZipOutlined';
import NoteAddOutlined from '@material-hu/icons/material/NoteAddOutlined';
import NoteAltOutlined from '@material-hu/icons/material/NoteAltOutlined';
import VideoLibraryOutlined from '@material-hu/icons/material/VideoLibraryOutlined';
import {
  IconBrandYoutube,
  IconListCheck,
  IconNotes,
  IconPuzzle,
  IconTemplate,
} from '@material-hu/icons/tabler';

import { type FileCardType } from '@material-hu/components/design-system/FileCard/types';

import {
  CourseStatus,
  CourseTypes,
  DeadlineTypes,
  type Question,
  type QuestionFormValues,
  ReadingTypes,
  Steps,
  type StepsTypes,
  TaskTypes,
  VideoTypes,
} from 'src/pages/dashboard/Learning/Courses/types';
import { IconType } from 'src/types/icons';
import { QuestionType } from 'src/types/surveys';
import { megabytesToBytes, stringSizeToBytes } from 'src/utils/filesUtils';
import { type TCustomFunction } from 'src/utils/i18n';

// getInternalId function moved here to avoid circular dependency
export const getInternalId = (id: number) => `internal_${id}`;

export const LOADING_MODAL_DELAY = 3000;
export const CATEGORY_MAX_LENGTH = 50;
export const STEPPER_HEIGHT = '149px';
export const COVER_PICTURE_WIDTH = 800;
export const COVER_PICTURE_HEIGHT = 400;
export const QUESTION_EXPLANATION_MAX_LENGTH = 255;
export const QUESTION_MAX_LENGTH = 800;
export const QUESTION_MIN_LENGTH = 2;
export const QUESTION_MIN_CHECKED = 1;
export const FILE_MAX_SIZE = megabytesToBytes(500); // 500MB
export const VIDEO_LESSON_MAX_SIZE = megabytesToBytes(5120); // 5 GB
export const SCORM_MAX_SIZE = megabytesToBytes(1536); // 1.5 GB
export const SATISFACTION_SURVEY_COMMENTS_MAX_LENGTH = 250;
export const QUESTION_DESCRIPTION_MAX_LENGTH = 250;
export const TASK_DESCRIPTION_MAX_LENGTH = 500;
export const BODY_MAX_LENGTH = 1000;
export const TITLE_MAX_LENGTH = 140;
export const TITLE_MIN_LENGTH = 3;
export const INSTRUCTOR_MAX_LENGTH = 100;
export const DURATION_MAX = 9999;
export const DURATION_MAX_LENGTH = 5;

export const QUESTION_MIN_OPTIONS: Partial<Record<QuestionType, number>> = {
  [QuestionType.CHECKBOX]: 2,
  [QuestionType.MULTIPLE_CHOICE]: 2,
  [QuestionType.TEXT]: 0,
};

export const SIDEBAR_WIDTH = {
  xs: '257px',
  lg: '316px',
  xl: '361px',
};

export const OPTIONS_QUESTION_TYPES = [
  QuestionType.CHECKBOX,
  QuestionType.MULTIPLE_CHOICE,
];

export const MODULE_DEFAULT_ICON = {
  custom: true,
  type: IconType.EMOJI,
  value: '😀',
};

export const TASKS_ICONS = {
  [TaskTypes.NEW]: {
    Icon: NoteAddOutlined,
    color: '#38425080',
  },
  [TaskTypes.TASK]: {
    Icon: BookOutlined,
    color: '#38425080',
  },
  [TaskTypes.VIDEO]: {
    Icon: VideoLibraryOutlined,
    color: '#56C95F',
  },
  [TaskTypes.READING]: {
    Icon: AutoStoriesOutlined,
    color: '#91A9FF',
  },
  [TaskTypes.QUESTIONNAIRE]: {
    Icon: EmojiEventsOutlined,
    color: '#FB7D68',
  },
  [TaskTypes.BLANK]: {
    Icon: NoteAltOutlined,
    color: '#06AED4',
  },
  [TaskTypes.SCORM]: {
    Icon: FolderZipOutlined,
    color: '#16B364',
  },
};

export const HUGO_TASKS_ICONS = {
  [TaskTypes.BLANK]: IconTemplate,
  [TaskTypes.TASK]: IconTemplate,
  [TaskTypes.NEW]: IconTemplate,
  [TaskTypes.VIDEO]: IconBrandYoutube,
  [TaskTypes.READING]: IconNotes,
  [TaskTypes.QUESTIONNAIRE]: IconListCheck,
  [TaskTypes.SCORM]: IconPuzzle,
};

export const TASK_DEFAULT_VALUES = {
  [TaskTypes.NEW]: {},
  [TaskTypes.TASK]: {},
  [TaskTypes.VIDEO]: {
    urlType: VideoTypes.EXTERNAL,
    video: {
      url: '',
      file: '',
    },
  },
  [TaskTypes.READING]: {
    reading: {
      type: ReadingTypes.MANUAL,
    },
  },
  [TaskTypes.SCORM]: {
    scorm: {
      zip: {
        url: '',
        file: '',
      },
    },
  },
  [TaskTypes.QUESTIONNAIRE]: {
    passingGrade: 60,
    maximumAttempts: -1,
    duration: -1,
    showCorrectAnswers: true,
    form: {
      id: getInternalId(0),
    },
  },
  [TaskTypes.BLANK]: {},
};

export const defaultCourseValues = (t: TCustomFunction): StepsTypes => ({
  [Steps.BASIC_INFORMATION]: {
    title: '',
    body: '',
    status: CourseStatus.DRAFT,
    isCourseStarted: false,
    image: undefined,
    category: null,
    duration: '',
  },
  [Steps.CONTENT]: {
    modules: [
      {
        id: getInternalId(1),
        title: t('common.module_empty'),
        icon: MODULE_DEFAULT_ICON,
        tasks: [],
      },
    ],
  },
  [Steps.CONFIGURATION]: {
    type: CourseTypes.REQUIRED,
    sequentiality: false,
    deadline: false,
    deadlineType: DeadlineTypes.FROM_ASSIGNMENT,
    deadlineDays: '',
    segmentation: undefined,
    sendPushNotification: true,
    sendEmailNotification: false,
    hasSurvey: true,
    issueCertificate: false,
    digitalSignature: false,
    refreshFinishedCourseUsers: false,
    instructor: '',
  },
});

export const getQuestionDrawerDefaultValues = async (
  values: Question | undefined,
  questionCount: number,
  t: TCustomFunction,
): Promise<QuestionFormValues> => {
  if (!values) {
    return {
      id: getInternalId(questionCount + 1),
      title: '',
      description: '',
      type: QuestionType.TEXT,
      choices: [
        { value: t('common.option', { count: 1 }), checked: false },
        { value: t('common.option', { count: 2 }), checked: false },
      ],
      hasExplanation: false,
      explanation: '',
      points: 0,
      attachments: [],
    };
  }

  const formattedAttachments = values?.attachments?.length
    ? await Promise.all(
        values.attachments.map(async fileAsset => {
          return {
            attachment: {
              url: fileAsset.url,
              name: fileAsset.name,
              size: fileAsset.size.toString(),
              bytes: stringSizeToBytes(fileAsset.size),
              type: fileAsset.contentType,
            },
            fileAsset,
            status: 'success',
          } satisfies FileCardType;
        }),
      )
    : [];

  return {
    id: values.id,
    title: values.title,
    description: values.description,
    type: values.type,
    choices: values.choices,
    hasExplanation: values.hasExplanation,
    explanation: values.explanation,
    points: values.points,
    attachments: formattedAttachments,
  };
};
