import { type UniqueIdentifier } from '@dnd-kit/core';
import { arrayMove } from '@dnd-kit/sortable';
import { type Theme } from '@material-hu/mui/styles';

import { type PillsProps } from '@material-hu/components/design-system/Pills';

import { type TCustomFunction } from 'src/utils/i18n';

import { type CourseListItem } from '../Courses/types';

import { InputsIds, MAX_COURSES_TO_SHOW } from './constants';
import {
  NotificationChannel,
  type PathCourse,
  type PathStatusType,
} from './types';

export const sortCourses = (
  sortedCourses: PathCourse[],
  activeId?: UniqueIdentifier,
  overId?: UniqueIdentifier,
) => {
  if (!activeId || !overId) return undefined;

  const oldIndex = sortedCourses.findIndex(
    course => course.courseId === activeId,
  );
  const newIndex = sortedCourses.findIndex(
    course => course.courseId === overId,
  );

  return arrayMove(sortedCourses, oldIndex, newIndex);
};

export const getSwitchersSx = (theme: Theme) => ({
  p: 2,
  backgroundColor: theme.palette.hugoBackground?.neutralBg,
  borderRadius: theme.shape.borderRadiusL,
});

export const getAccordionTitleStyles = (theme: Theme) => ({
  '& .MuiAccordionSummary-root span': {
    color: theme.palette.textColors?.neutralTextLighter,
  },
});

export const getTableTooltipTitle = (
  courses: string[] | undefined,
  t: TCustomFunction,
) => {
  if (!courses || !courses.length) return '';

  const coursesWord = t('course.title_other').toLowerCase();
  const coursesRest = courses.length - MAX_COURSES_TO_SHOW;

  const coursesNames = courses
    .slice(0, MAX_COURSES_TO_SHOW)
    .map(course => course)
    .join(', ');

  return coursesRest > 0
    ? `${coursesNames} + ${coursesRest} ${coursesWord}.`
    : coursesNames;
};

export const getFormattedPathCourse = (course: CourseListItem): PathCourse => ({
  id: course.id,
  courseId: course.id,
  title: course.title,
  duration: course.duration,
  durationTime: course.durationTime,
  coverImage: course.coverImage,
  categoryName: course.category?.name,
});

export const handleScrollToInput = (elementId: keyof typeof InputsIds) => {
  const inputElement = document.getElementById(InputsIds[elementId]);

  if (!inputElement) return;

  inputElement.scrollIntoView({ behavior: 'smooth', block: 'end' });
};

export const pathStatusToPillType: Record<PathStatusType, PillsProps['type']> =
  {
    ACTIVE: 'success',
    INACTIVE: 'disabled',
    DRAFT: 'neutral',
  };

export const toNotificationChannel = (
  sendPushNotification: boolean,
  sendEmailNotification: boolean,
): NotificationChannel => {
  if (sendPushNotification && sendEmailNotification)
    return NotificationChannel.PUSH_AND_EMAIL;
  if (sendPushNotification) return NotificationChannel.PUSH;
  if (sendEmailNotification) return NotificationChannel.EMAIL;
  return NotificationChannel.NONE;
};

export const fromNotificationChannel = (
  value?: string | null,
): { sendPushNotification: boolean; sendEmailNotification: boolean } => {
  const channel = value?.toUpperCase();
  return {
    sendPushNotification:
      channel === NotificationChannel.PUSH ||
      channel === NotificationChannel.PUSH_AND_EMAIL,
    sendEmailNotification:
      channel === NotificationChannel.EMAIL ||
      channel === NotificationChannel.PUSH_AND_EMAIL,
  };
};

export const getNotificationChannelLabelKey = (
  sendPushNotification: boolean,
  sendEmailNotification: boolean,
): string => {
  if (sendPushNotification && sendEmailNotification)
    return 'notifications_center:notification_channels.push_and_email';
  if (sendPushNotification)
    return 'notifications_center:notification_channels.push_notifications';
  if (sendEmailNotification)
    return 'notifications_center:notification_channels.email_notifications';
  return 'general:notification.none';
};
