// Copyright 2025 Signal Messenger, LLC
// SPDX-License-Identifier: AGPL-3.0-only
import { useCallback, type ReactNode, type JSX } from 'react';
import { action } from '@storybook/addon-actions';
import type { PropsType } from '../state/smart/MediaItem.preload.tsx';
import { getSafeDomain } from '../types/LinkPreview.std.ts';
import type { AttachmentStatusType } from '../hooks/useAttachmentStatus.std.ts';
import { missingCaseError } from '../util/missingCaseError.std.ts';
import { isVoiceMessagePlayed } from '../util/isVoiceMessagePlayed.std.ts';
import { LinkPreviewItem } from '../components/conversation/media-gallery/LinkPreviewItem.dom.tsx';
import { MediaContextMenu } from '../components/conversation/media-gallery/MediaContextMenu.dom.tsx';
import { MediaGridItem } from '../components/conversation/media-gallery/MediaGridItem.dom.tsx';
import { DocumentListItem } from '../components/conversation/media-gallery/DocumentListItem.dom.tsx';
import { ContactListItem } from '../components/conversation/media-gallery/ContactListItem.dom.tsx';
import { AudioListItem } from '../components/conversation/media-gallery/AudioListItem.dom.tsx';

const { i18n } = window.SignalContext;

function renderContextMenu(
  _mediaItem: unknown,
  children: ReactNode
): JSX.Element {
  return (
    <MediaContextMenu
      i18n={i18n}
      showMessage={action('showMessage')}
      removeAttachment={action('removeAttachment')}
      saveAttachment={action('saveAttachment')}
      forwardAttachment={action('forwardAttachment')}
      copyLink={action('copyLink')}
      messageContact={action('messageContact')}
    >
      {children}
    </MediaContextMenu>
  );
}

export function MediaItem({ mediaItem, onItemClick }: PropsType): JSX.Element {
  const onClick = useCallback(
    (state: AttachmentStatusType['state']) => {
      onItemClick({ mediaItem, state });
    },
    [mediaItem, onItemClick]
  );

  const actions = {
    onClick,
    renderContextMenu,
    showMessage: action('showMessage'),
  };

  switch (mediaItem.type) {
    case 'audio':
      return (
        <AudioListItem
          i18n={i18n}
          authorTitle="Alice"
          isPlayed={isVoiceMessagePlayed(mediaItem.message, undefined)}
          mediaItem={mediaItem}
          {...actions}
        />
      );
    case 'media':
      return (
        <MediaGridItem
          mediaItem={mediaItem}
          i18n={i18n}
          showSize={false}
          {...actions}
        />
      );
    case 'document':
      return (
        <DocumentListItem
          i18n={i18n}
          authorTitle="Alice"
          mediaItem={mediaItem}
          {...actions}
        />
      );
    case 'contact':
      return (
        <ContactListItem
          i18n={i18n}
          authorTitle="Alice"
          mediaItem={mediaItem}
          {...actions}
        />
      );
    case 'link': {
      const hydratedMediaItem = {
        ...mediaItem,
        preview: {
          ...mediaItem.preview,
          domain: getSafeDomain(mediaItem.preview.url),
        },
      };

      return (
        <LinkPreviewItem
          i18n={i18n}
          authorTitle="Alice"
          mediaItem={hydratedMediaItem}
          {...actions}
        />
      );
    }
    default:
      throw missingCaseError(mediaItem);
  }
}
